our $VERSION = 0.05;
with qw[
- MooseX::Daemonize::Core
- MooseX::Daemonize::WithSignalHandling
MooseX::Daemonize::WithPidFile
MooseX::Getopt
];
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;
$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; }
our $VERSION = 0.01;
+with 'MooseX::Daemonize::Core';
+
requires 'init_pidfile';
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__
+++ /dev/null
-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
sub daemonize_ok {
my ( $daemon, $msg ) = @_;
- unless ( my $pid = Proc::Daemon::Fork ) {
+ unless ( my $pid = fork ) {
$daemon->start();
exit;
}
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');
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 . ')');
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');
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 )
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;
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');