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
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;
$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' );
+ <NULL> if (0);
# Change to basedir
chdir $self->basedir;
1;
__END__
+=pod
+
=head1 NAME
MooseX::Daemonize - provides a Role that daemonizes your Moose based
=item daemonize()
-Calls C<Proc::Daemon::Init> to daemonize this process.
+Calls daemonize from MooseX::Daemonize::Core.
=item setup_signals()
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
open(STDIN, "+>/dev/null");
- # Avoid 'stdin reopened for output'
- # warning with newer perls
- open( NULL, '/dev/null' );
- <NULL> if (0);
-
if (my $stdout_file = $ENV{MX_DAEMON_STDOUT}) {
open STDOUT, ">", $stdout_file
or confess "Could not redirect STDOUT to $stdout_file : $!";
}
else {
open(STDERR, "+>&STDIN");
- }
+ }
}
sub daemonize {
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 }
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';
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 {
=over
-=item pid Int
-
=item file Path::Class::File | Str
=back
+++ /dev/null
-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
use strict;
use Moose::Role;
-use MooseX::Daemonize::Types;
use MooseX::Daemonize::Pid::File;
our $VERSION = 0.01;
1;
-__END__
\ No newline at end of file
+__END__
+
+=pod
+
+=cut
\ No newline at end of file
}
{
- my $PID = 2001;
+ my $PID = 9999;
my $f = MooseX::Daemonize::Pid::File->new(
file => [ 't', 'baz.pid' ],
use warnings;
use Cwd;
+use File::Spec::Functions;
use Test::More no_plan => 1;
use Test::Exception;
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;
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 . ')');
--- /dev/null
+#!/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');
+