Thanks for the advise. So I took the signal code from XML::RPC::Server and adapted it to JSON::RPC::Server::Daemon but the only way I could see this to work was to create a class called JSON::RPC::Server::AltDaemon which is based on JSON::RPC::Server::Daemon. This works perfect and the signal is sent and handled when this code is running in a thread using $thread->kill('INT'). But is there a better way to do this. Obviously this could break if a future update to JSON::RPC::Server implements things differently.
=begin nd Provided an alternate version of JSON::RPC::Server::Daemon that can be + inturpted using signals. =cut package JSON::RPC::Server::AltDaemon; use strict; use JSON::RPC::Server; # for old Perl 5.005 use base qw(JSON::RPC::Server); $JSON::RPC::Server::AltDaemon::VERSION = '0.03'; use Data::Dumper; =begin nd Create a new instance of JSON::RPC::Server::Daemon::Alt @param $class ref Object reference/static class @param %args hash Arguments (timeout => '10') @return undef =cut sub new { my $class = shift; my %args = @_; my $self = $class->SUPER::new(); my $pkg; if( grep { $_ =~ /^SSL_/ } @_ ){ $self->{_daemon_pkg} = $pkg = 'HTTP::Daemon::SSL'; } else{ $self->{_daemon_pkg} = $pkg = 'HTTP::Daemon'; } eval qq| require $pkg; |; if($@){ die $@ } $self->{_daemon} ||= $pkg->new(@_) or die; $self->{__timeout} = delete $args{timeout} || 10; return $self; } =begin nd Starts the server @param $self ref Object reference/static class @param %args hash Arguments (signal => 'INT') @return undef =cut sub handle { my $self = shift; my %args = @_; my $d = $self->{_daemon} ||= $self->{_daemon_pkg}->new(@_) or d +ie; # Localize and set the signal handler as an exit route my @exit_signals; if (exists $args{signal} and $args{signal} ne 'NONE') { @exit_signals = (ref $args{signal}) ? @{$args{signal}} : $args{signal} } else { push @exit_signals, 'INT'; } my $exit_now; local @SIG{@exit_signals} = (sub { $exit_now++;}); my $timeout = $d->timeout(1); while (! $exit_now) { my $c = $d->accept; if ($exit_now) { last; } if (! $c) { next; } $c->timeout($self->timeout); $self->{con} = $c; while (my $r = $c->get_request) { $self->request($r); $self->path_info($r->url->path); $self->SUPER::handle(); last; } $c->close; undef $c; } return; } sub retrieve_json_from_post { return $_[0]->request->content; } sub retrieve_json_from_get { } sub response { my ($self, $response) = @_; $self->{con}->send_response($response); } =begin nd This sets the timeout for processing connections after a new connectio +n has been accepted. It returns the old timeout value. If you pass in no value, it returns + the current timeout. @param $self ref Object reference/static class @param $timeout int New timeout value @return integer Current timeout value =cut sub timeout { my ($self, $timeout) = @_; my $old_timeout = $self->{__timeout}; if ($timeout) { $self->{__timeout} = $timeout; } return $old_timeout; } 1;

In reply to Re^2: Trying to exit an HTTP::Daemon based on JSON::RPC::Server::Daemon by carcus88
in thread Trying to exit an HTTP::Daemon based on JSON::RPC::Server::Daemon by carcus88

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.