From: Stevan Little Date: Mon, 3 Dec 2007 02:00:18 +0000 (+0000) Subject: ok,.. more tests and stuff X-Git-Tag: 0_06~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8ac4733fe6918fb1449262f56b5c371315c6cfb8;p=gitmo%2FMooseX-Daemonize.git ok,.. more tests and stuff --- diff --git a/Changes b/Changes index 60458f2..e7978e5 100644 --- a/Changes +++ b/Changes @@ -2,12 +2,28 @@ Revision history for MooseX-Daemonize 0.05 * MooseX::Daemonize - - Fix logic that kills process so it doens't always warn about undead - process + - Fix logic that kills process so it doens't always warn + about undead process - Added stop_timeout to allow user to control timings. - + - Refactored to roles + + * MooseX::Daemonize::Core + - the core daemonization methods are here + - added tests for this + + * MooseX::Daemonize::WithSignalHandling + - the SIG handling is here + + * MooseX::Daemonize::WithPidFile + - the PID file handling is here, and + delegates to the classes below + + * MooseX::Daemonize::Pid + - added this package to replace the File::Pid stuff + * MooseX::Daemonize::Pid::File - - added this package to replace the File::Pid stuff (stevan) + - added this package to replace the File::Pid stuff, it is a subclass + of MooseX::Daemonize::Pid (stevan) - added tests for this (stevan) 0.04 2007-11-11 diff --git a/lib/MooseX/Daemonize.pm b/lib/MooseX/Daemonize.pm index 52e6734..3eb25b8 100644 --- a/lib/MooseX/Daemonize.pm +++ b/lib/MooseX/Daemonize.pm @@ -1,8 +1,7 @@ package MooseX::Daemonize; use strict; # because Kwalitee is pedantic use Moose::Role; - -use MooseX::Daemonize::Types; +use MooseX::Types::Path::Class; our $VERSION = 0.05; @@ -70,9 +69,19 @@ sub start { $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; $self->pidfile->pid($$); + + # Avoid 'stdin reopened for output' + # warning with newer perls + open( NULL, '/dev/null' ); + if (0); # Change to basedir chdir $self->basedir; @@ -154,6 +163,8 @@ $_kill = sub { 1; __END__ +=pod + =head1 NAME MooseX::Daemonize - provides a Role that daemonizes your Moose based @@ -244,7 +255,7 @@ Litterally =item daemonize() -Calls C to daemonize this process. +Calls daemonize from MooseX::Daemonize::Core. =item setup_signals() @@ -349,3 +360,5 @@ RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +=cut diff --git a/lib/MooseX/Daemonize/Core.pm b/lib/MooseX/Daemonize/Core.pm index a30b729..cb1c202 100644 --- a/lib/MooseX/Daemonize/Core.pm +++ b/lib/MooseX/Daemonize/Core.pm @@ -53,11 +53,6 @@ sub daemon_detach { open(STDIN, "+>/dev/null"); - # Avoid 'stdin reopened for output' - # warning with newer perls - open( NULL, '/dev/null' ); - if (0); - if (my $stdout_file = $ENV{MX_DAEMON_STDOUT}) { open STDOUT, ">", $stdout_file or confess "Could not redirect STDOUT to $stdout_file : $!"; @@ -72,7 +67,7 @@ sub daemon_detach { } else { open(STDERR, "+>&STDIN"); - } + } } sub daemonize { diff --git a/lib/MooseX/Daemonize/Pid.pm b/lib/MooseX/Daemonize/Pid.pm index 0faaedb..60b9086 100644 --- a/lib/MooseX/Daemonize/Pid.pm +++ b/lib/MooseX/Daemonize/Pid.pm @@ -1,14 +1,21 @@ package MooseX::Daemonize::Pid; use strict; # because Kwalitee is pedantic use Moose; +use Moose::Util::TypeConstraints; + +coerce 'MooseX::Daemonize::Pid' + => from 'Int' + => via { MooseX::Daemonize::Pid->new( pid => $_ ) }; our $VERSION = '0.01'; has 'pid' => ( - is => 'rw', - isa => 'Int', - lazy => 1, - default => sub { $$ } + is => 'rw', + isa => 'Int', + lazy => 1, + clearer => 'clear_pid', + predicate => 'has_pid', + default => sub { $$ } ); sub is_running { kill(0, (shift)->pid) ? 1 : 0 } diff --git a/lib/MooseX/Daemonize/Pid/File.pm b/lib/MooseX/Daemonize/Pid/File.pm index 6df5733..2b3c88a 100644 --- a/lib/MooseX/Daemonize/Pid/File.pm +++ b/lib/MooseX/Daemonize/Pid/File.pm @@ -1,8 +1,14 @@ package MooseX::Daemonize::Pid::File; use strict; # because Kwalitee is pedantic use Moose; +use Moose::Util::TypeConstraints; +use MooseX::Types::Path::Class; -use MooseX::Daemonize::Types; +coerce 'MooseX::Daemonize::Pid::File' + => from 'Str' + => via { MooseX::Daemonize::Pid::File->new( file => $_ ) } + => from 'Path::Class::File' + => via { MooseX::Daemonize::Pid::File->new( file => $_ ) }; our $VERSION = '0.01'; @@ -29,7 +35,9 @@ sub does_file_exist { -s (shift)->file } sub write { my $self = shift; - $self->file->openw->print($self->pid); + my $fh = $self->file->openw; + $fh->print($self->pid); + $fh->close; } override 'is_running' => sub { @@ -55,8 +63,6 @@ MooseX::Daemonize::Pid::File - PID file management for MooseX::Daemonize =over -=item pid Int - =item file Path::Class::File | Str =back diff --git a/lib/MooseX/Daemonize/Types.pm b/lib/MooseX/Daemonize/Types.pm deleted file mode 100644 index eb17366..0000000 --- a/lib/MooseX/Daemonize/Types.pm +++ /dev/null @@ -1,17 +0,0 @@ -package MooseX::Daemonize::Types; - -use Moose::Util::TypeConstraints; -use MooseX::Types::Path::Class; -use MooseX::Daemonize::Pid::File; # need this for the coercion below - -our $VERSION = 0.01; - -coerce 'MooseX::Daemonize::Pid::File' - => from 'Str' - => via { MooseX::Daemonize::Pid::File->new( file => $_ ) } - => from 'Path::Class::File' - => via { MooseX::Daemonize::Pid::File->new( file => $_ ) }; - -1; - -__END__ \ No newline at end of file diff --git a/lib/MooseX/Daemonize/WithPidFile.pm b/lib/MooseX/Daemonize/WithPidFile.pm index 8a71485..b5aa510 100644 --- a/lib/MooseX/Daemonize/WithPidFile.pm +++ b/lib/MooseX/Daemonize/WithPidFile.pm @@ -2,7 +2,6 @@ package MooseX::Daemonize::WithPidFile; use strict; use Moose::Role; -use MooseX::Daemonize::Types; use MooseX::Daemonize::Pid::File; our $VERSION = 0.01; @@ -21,4 +20,8 @@ has pidfile => ( 1; -__END__ \ No newline at end of file +__END__ + +=pod + +=cut \ No newline at end of file diff --git a/t/10.pidfile.t b/t/10.pidfile.t index 7c3775a..a5a06c0 100644 --- a/t/10.pidfile.t +++ b/t/10.pidfile.t @@ -60,7 +60,7 @@ BEGIN { } { - my $PID = 2001; + my $PID = 9999; my $f = MooseX::Daemonize::Pid::File->new( file => [ 't', 'baz.pid' ], diff --git a/t/20.core.t b/t/20.core.t index 811f959..49ca097 100644 --- a/t/20.core.t +++ b/t/20.core.t @@ -4,6 +4,7 @@ use strict; use warnings; use Cwd; +use File::Spec::Functions; use Test::More no_plan => 1; use Test::Exception; @@ -14,7 +15,9 @@ BEGIN { use_ok('MooseX::Daemonize::Pid'); } -my $CWD = Cwd::cwd; +my $CWD = Cwd::cwd; +$ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt'); +$ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); { package MyFooDaemon; @@ -74,17 +77,17 @@ 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"; +diag `ps $pid`; +diag "-------"; +diag `ps -x | grep test-app`; +diag "-------"; +diag "killing $pid"; kill INT => $p->pid; -#diag "killed $pid"; +diag "killed $pid"; sleep(2); -#diag `ps $pid`; -#diag "-------"; -#diag `ps -x | grep test-app`; +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 new file mode 100644 index 0000000..30bf176 --- /dev/null +++ b/t/30.with_pid_file.t @@ -0,0 +1,99 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Cwd; +use File::Spec::Functions; + +use Test::More no_plan => 1; +use Test::Exception; +use Test::Moose; + +BEGIN { + use_ok('MooseX::Daemonize::Core'); +} + +my $CWD = Cwd::cwd; +my $PIDFILE = catfile($CWD, 'test-app.pid'); +$ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt'); +$ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); + +{ + package MyFooDaemon; + use Moose; + + with 'MooseX::Daemonize::Core', + 'MooseX::Daemonize::WithPidFile'; + + sub init_pidfile { + MooseX::Daemonize::Pid::File->new( file => $PIDFILE ) + } + + sub start { + my $self = shift; + + $self->daemonize; + return unless $self->is_daemon; + + $self->pidfile->write; + + # make it easy to find with ps + $0 = 'test-app'; + $SIG{INT} = sub { + print "Got INT! Oh Noes!"; + $self->pidfile->remove; + exit; + }; + while (1) { + print "Hello from $$\n"; + sleep(10); + } + exit; + } +} + +my $d = MyFooDaemon->new( pidfile => $PIDFILE ); +isa_ok($d, 'MyFooDaemon'); +does_ok($d, 'MooseX::Daemonize::Core'); +does_ok($d, 'MooseX::Daemonize::WithPidFile'); + +ok($d->has_pidfile, '... we have a pidfile value'); + +{ + my $p = $d->pidfile; + isa_ok($p, 'MooseX::Daemonize::Pid::File'); + #diag $p->dump; +} + +ok(!(-e $PIDFILE), '... the PID file does not exist yet'); + +lives_ok { + $d->start; +} '... successfully daemonized from (' . $$ . ')'; + +my $p = $d->pidfile; +isa_ok($p, 'MooseX::Daemonize::Pid::File'); +#diag $p->dump; + +sleep(2); + +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"; +kill INT => $p->pid; +diag "killed $pid"; +sleep(2); +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'); +