From: Stevan Little Date: Wed, 19 Dec 2007 05:28:59 +0000 (+0000) Subject: cleaning up stuff X-Git-Tag: 0_06~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=92cf56b7c3fd91568e4b964319a7862fc4f1d651;p=gitmo%2FMooseX-Daemonize.git cleaning up stuff --- diff --git a/Changes b/Changes index 5706f0b..a64b62b 100644 --- a/Changes +++ b/Changes @@ -8,6 +8,13 @@ Revision history for MooseX-Daemonize - Refactored to roles - removed Proc::Daemon dependency - removed File::Pid dependency + - added exit_code and status_message to capture the + exit code and a status message :) + - refactored start/stop/restart to use these + - added tests for this + - added a &status command which returns a bool telling + you if the daemon is running or not + - added tests for this * Test::MooseX::Daemonize - updated docs diff --git a/lib/MooseX/Daemonize.pm b/lib/MooseX/Daemonize.pm index 4ce6fd3..903832d 100644 --- a/lib/MooseX/Daemonize.pm +++ b/lib/MooseX/Daemonize.pm @@ -7,6 +7,9 @@ our $VERSION = 0.05; with 'MooseX::Daemonize::WithPidFile', 'MooseX::Getopt'; + +use constant OK => 0; +use constant ERROR => 1; has progname => ( metaclass => 'Getopt', @@ -112,7 +115,8 @@ sub start { $self->clear_exit_code; if ($self->pidfile->is_running) { - $self->status_message('Daemon is already running with pid (' . $self->pidfile->pid . ')'); + $self->exit_code(OK); + $self->status_message('Daemon is already running with pid (' . $self->pidfile->pid . ')'); return !($self->exit_code); } @@ -122,13 +126,14 @@ sub start { else { eval { $self->daemonize }; if ($@) { - $self->exit_code(1); + $self->exit_code(ERROR); $self->status_message('Start failed : ' . $@); return !($self->exit_code); } } unless ($self->is_daemon) { + $self->exit_code(OK); $self->status_message('Start succeeded'); return !($self->exit_code); } @@ -150,10 +155,11 @@ sub status { $self->clear_exit_code; if ($self->pidfile->is_running) { + $self->exit_code(OK); $self->status_message('Daemon is running with pid (' . $self->pidfile->pid . ')'); } else { - $self->exit_code(1); + $self->exit_code(ERROR); $self->status_message('Daemon is not running with pid (' . $self->pidfile->pid . ')'); } @@ -167,17 +173,19 @@ sub restart { $self->clear_exit_code; unless ($self->stop) { - $self->exit_code(1); + $self->exit_code(ERROR); $self->status_message('Restart (Stop) failed : ' . $@); } unless ($self->start) { - $self->exit_code(1); + $self->exit_code(ERROR); $self->status_message('Restart (Start) failed : ' . $@); } - $self->status_message("Restart successful") - if !$self->exit_code; + if ($self->exit_code == OK) { + $self->exit_code(OK); + $self->status_message("Restart successful"); + } return !($self->exit_code); } @@ -205,11 +213,12 @@ sub stop { eval { $self->$_kill($self->pidfile->pid) }; # and complain if we can't ... if ($@) { - $self->exit_code(1); + $self->exit_code(ERROR); $self->status_message('Stop failed : ' . $@); } # or gloat if we succeed .. else { + $self->exit_code(OK); $self->status_message('Stop succeeded'); } @@ -228,6 +237,7 @@ sub stop { # this just returns the OK # exit code for now, but # we should make this overridable + $self->exit_code(OK); $self->status_message("Not running"); } @@ -317,7 +327,7 @@ This document describes MooseX::Daemonize version 0.05 $daemon->restart if $command eq 'restart'; $daemon->stop if $command eq 'stop'; - warn($daemon->status); + warn($daemon->status_message); exit($daemon->exit_code); =head1 DESCRIPTION @@ -373,7 +383,7 @@ These are the internal attributes, which are not available through MooseX::Getop =item I -=item I +=item I =back @@ -407,6 +417,8 @@ Literally this is: =item B +=item B + =back @@ -488,10 +500,12 @@ L. L, L -=head1 AUTHOR +=head1 AUTHORS Chris Prather C<< >> +Stevan Little C<< >> + =head1 THANKS Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and the diff --git a/lib/MooseX/Daemonize/Pid.pm b/lib/MooseX/Daemonize/Pid.pm index bcfd3b5..67d8d88 100644 --- a/lib/MooseX/Daemonize/Pid.pm +++ b/lib/MooseX/Daemonize/Pid.pm @@ -85,7 +85,7 @@ L. =head1 AUTHOR -Stevan Little C<< >> +Stevan Little C<< >> =head1 LICENCE AND COPYRIGHT diff --git a/lib/MooseX/Daemonize/Pid/File.pm b/lib/MooseX/Daemonize/Pid/File.pm index dcb8697..06bd5dc 100644 --- a/lib/MooseX/Daemonize/Pid/File.pm +++ b/lib/MooseX/Daemonize/Pid/File.pm @@ -148,7 +148,7 @@ L. =head1 AUTHOR -Stevan Little C<< >> +Stevan Little C<< >> =head1 LICENCE AND COPYRIGHT diff --git a/t/01.filecreate.t b/t/01.filecreate.t index 8f108f8..d4e977a 100644 --- a/t/01.filecreate.t +++ b/t/01.filecreate.t @@ -5,8 +5,12 @@ use warnings; use Cwd; use File::Spec::Functions; -use Test::More tests => 9; -use MooseX::Daemonize; +use Test::More tests => 29; +use Test::Moose; + +BEGIN { + use_ok('MooseX::Daemonize'); +} use constant DEBUG => 0; @@ -41,41 +45,66 @@ my $app = FileMaker->new( pidbase => $CWD, filename => $FILENAME, ); +isa_ok($app, 'FileMaker'); +does_ok($app, 'MooseX::Daemonize'); +does_ok($app, 'MooseX::Daemonize::WithPidFile'); +does_ok($app, 'MooseX::Daemonize::Core'); + +isa_ok($app->pidfile, 'MooseX::Daemonize::Pid::File'); + +is($app->pidfile->file, "$CWD/filemaker.pid", '... got the right PID file path'); +ok(not(-e $app->pidfile->file), '... our pidfile does not exist'); ok(!$app->status, '... the daemon is running'); +is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code'); + +ok($app->stop, '... the app will stop cause its not running'); +is($app->status_message, "Not running", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); diag $$ if DEBUG; ok($app->start, '... daemon started'); +is($app->status_message, "Start succeeded", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); + sleep(1); # give it a second ... -ok($app->status, '... the daemon is running'); +ok(-e $app->pidfile->file, '... our pidfile exists' ); my $pid = $app->pidfile->pid; isnt($pid, $$, '... the pid in our pidfile is correct (and not us)'); +ok($app->status, '... the daemon is running'); +is($app->status_message, "Daemon is running with pid ($pid)", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); + if (DEBUG) { diag `ps $pid`; diag "Status is: " . $app->status_message; } ok( -e $app->filename, "file exists" ); -ok($app->status, '... the daemon is still running'); if (DEBUG) { diag `ps $pid`; diag "Status is: " . $app->status_message; } -ok( $app->stop, 'app stopped' ); +ok( $app->stop, '... app stopped' ); +is($app->status_message, "Stop succeeded", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); + ok(!$app->status, '... the daemon is no longer running'); +is($app->status_message, "Daemon is not running with pid ($pid)", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code'); if (DEBUG) { diag `ps $pid`; diag "Status is: " . $app->status_message; } -ok( not(-e $app->pidfile->file) , 'pidfile gone' ); +ok( not(-e $app->pidfile->file) , '... pidfile gone' ); unlink $FILENAME; unlink $ENV{MX_DAEMON_STDOUT}; diff --git a/t/30.with_pid_file.t b/t/30.with_pid_file.t index 2e6f718..135673b 100644 --- a/t/30.with_pid_file.t +++ b/t/30.with_pid_file.t @@ -36,13 +36,13 @@ $ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); # this tests our bad PID # cleanup functionality. - print "Our parent PID is " . $self->pidfile->pid . "\n"; + print "Our parent PID is " . $self->pidfile->pid . "\n" if ::DEBUG; $self->daemonize; return unless $self->is_daemon; # make it easy to find with ps - $0 = 'test-app'; + $0 = 'test-app-2'; $SIG{INT} = sub { print "Got INT! Oh Noes!"; $self->pidfile->remove;