From: Stevan Little Date: Mon, 3 Dec 2007 02:16:25 +0000 (+0000) Subject: removed signal handling, cause it was not very useful, and then made WithPidFile... X-Git-Tag: 0_06~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d02fc7049ca4136768336ba4bfe42e361924f674;p=gitmo%2FMooseX-Daemonize.git removed signal handling, cause it was not very useful, and then made WithPidFile do Core, as it is more useful as an extension then a seperate role --- diff --git a/lib/MooseX/Daemonize.pm b/lib/MooseX/Daemonize.pm index 3eb25b8..35c2afe 100644 --- a/lib/MooseX/Daemonize.pm +++ b/lib/MooseX/Daemonize.pm @@ -6,8 +6,6 @@ use MooseX::Types::Path::Class; our $VERSION = 0.05; with qw[ - MooseX::Daemonize::Core - MooseX::Daemonize::WithSignalHandling MooseX::Daemonize::WithPidFile MooseX::Getopt ]; @@ -68,11 +66,6 @@ sub start { confess "instance already running" if $self->pidfile->is_running; $self->daemonize unless $self->foreground; - - # make sure to clear the PID - # so that a bad value doesn't - # stick around in the parent - $self->pidfile->clear_pid; return unless $self->is_daemon; @@ -109,10 +102,10 @@ sub restart { $self->start(); } -sub handle_signal { - my ($self, $signal) = @_; - return $self->handle_sigint if $signal eq 'INT'; - return $self->handle_sighup if $signal eq 'HUP'; +sub setup_signals { + my $self = shift; + $SIG{'INT'} = sub { $self->handle_sigint }; + $SIG{'HUP'} = sub { $self->handle_sighup }; } sub handle_sigint { $_[0]->stop; } diff --git a/lib/MooseX/Daemonize/WithPidFile.pm b/lib/MooseX/Daemonize/WithPidFile.pm index b5aa510..7bc0de8 100644 --- a/lib/MooseX/Daemonize/WithPidFile.pm +++ b/lib/MooseX/Daemonize/WithPidFile.pm @@ -6,6 +6,8 @@ use MooseX::Daemonize::Pid::File; our $VERSION = 0.01; +with 'MooseX::Daemonize::Core'; + requires 'init_pidfile'; has pidfile => ( @@ -18,6 +20,14 @@ has pidfile => ( builder => 'init_pidfile', ); +after 'daemonize' => sub { + # NOTE: + # make sure that we do not have + # any bad PID values stashed around + # - SL + (shift)->pidfile->clear_pid +}; + 1; __END__ diff --git a/lib/MooseX/Daemonize/WithSignalHandling.pm b/lib/MooseX/Daemonize/WithSignalHandling.pm deleted file mode 100644 index f1b58bf..0000000 --- a/lib/MooseX/Daemonize/WithSignalHandling.pm +++ /dev/null @@ -1,28 +0,0 @@ -package MooseX::Daemonize::WithSignalHandling; -use strict; # because Kwalitee is pedantic -use Moose::Role; - -our $VERSION = 0.01; - -# NOTE: -# this would be an excellent canidate for -# a parameterized role, since we would want -# to have the ability to specify which -# signals we want handled - -requires 'handle_signal'; - -sub setup_signals { - my $self = shift; - foreach my $signal (qw[ INT HUP ]) { - $SIG{$signal} = sub { $self->handle_signal($signal) }; - } -} - -1; - -__END__ - -=pod - -=cut \ No newline at end of file diff --git a/lib/Test/MooseX/Daemonize.pm b/lib/Test/MooseX/Daemonize.pm index a9c880f..8488621 100644 --- a/lib/Test/MooseX/Daemonize.pm +++ b/lib/Test/MooseX/Daemonize.pm @@ -27,7 +27,7 @@ our $Test = Test::Builder->new; sub daemonize_ok { my ( $daemon, $msg ) = @_; - unless ( my $pid = Proc::Daemon::Fork ) { + unless ( my $pid = fork ) { $daemon->start(); exit; } diff --git a/t/20.core.t b/t/20.core.t index 49ca097..0159b57 100644 --- a/t/20.core.t +++ b/t/20.core.t @@ -15,6 +15,8 @@ BEGIN { use_ok('MooseX::Daemonize::Pid'); } +use constant DEBUG => 0; + my $CWD = Cwd::cwd; $ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt'); $ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); @@ -77,17 +79,21 @@ isa_ok($p, 'MooseX::Daemonize::Pid'); ok($p->is_running, '... the daemon process is running (' . $p->pid . ')'); my $pid = $p->pid; -diag `ps $pid`; -diag "-------"; -diag `ps -x | grep test-app`; -diag "-------"; -diag "killing $pid"; +if (DEBUG) { + diag `ps $pid`; + diag "-------"; + diag `ps -x | grep test-app`; + diag "-------"; + diag "killing $pid"; +} kill INT => $p->pid; -diag "killed $pid"; +diag "killed $pid" if DEBUG; sleep(2); -diag `ps $pid`; -diag "-------"; -diag `ps -x | grep test-app`; +if (DEBUG) { + diag `ps $pid`; + diag "-------"; + diag `ps -x | grep test-app`; +} ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')'); diff --git a/t/30.with_pid_file.t b/t/30.with_pid_file.t index 30bf176..61414a7 100644 --- a/t/30.with_pid_file.t +++ b/t/30.with_pid_file.t @@ -14,6 +14,8 @@ BEGIN { use_ok('MooseX::Daemonize::Core'); } +use constant DEBUG => 0; + my $CWD = Cwd::cwd; my $PIDFILE = catfile($CWD, 'test-app.pid'); $ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt'); @@ -23,8 +25,7 @@ $ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); package MyFooDaemon; use Moose; - with 'MooseX::Daemonize::Core', - 'MooseX::Daemonize::WithPidFile'; + with 'MooseX::Daemonize::WithPidFile'; sub init_pidfile { MooseX::Daemonize::Pid::File->new( file => $PIDFILE ) @@ -33,6 +34,10 @@ $ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); sub start { my $self = shift; + # this tests our bad PID + # cleanup functionality. + print "Our parent PID is " . $self->pidfile->pid . "\n"; + $self->daemonize; return unless $self->is_daemon; @@ -82,17 +87,21 @@ ok($p->does_file_exist, '... the PID file exists'); ok($p->is_running, '... the daemon process is running (' . $p->pid . ')'); my $pid = $p->pid; -diag `ps $pid`; -diag "-------"; -diag `ps -x | grep test-app`; -diag "-------"; -diag "killing $pid"; +if (DEBUG) { + diag `ps $pid`; + diag "-------"; + diag `ps -x | grep test-app`; + diag "-------"; + diag "killing $pid"; +} kill INT => $p->pid; -diag "killed $pid"; +diag "killed $pid" if DEBUG; sleep(2); -diag `ps $pid`; -diag "-------"; -diag `ps -x | grep test-app`; +if (DEBUG) { + diag `ps $pid`; + diag "-------"; + diag `ps -x | grep test-app`; +} ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')'); ok(!(-e $PIDFILE), '... the PID file has been removed');