7 use File::Temp qw(tempdir);
8 use File::Spec::Functions;
10 my $dir = tempdir( CLEANUP => 1 );
13 use_ok('MooseX::Daemonize::Core');
14 use_ok('MooseX::Daemonize::Pid');
17 use constant DEBUG => 0;
19 $ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
20 $ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
26 with 'MooseX::Daemonize::Core';
28 has 'daemon_pid' => (is => 'rw', isa => 'MooseX::Daemonize::Pid');
30 # capture the PID from the fork
31 around 'daemon_fork' => sub {
34 if (my $pid = $self->$next(@_)) {
36 MooseX::Daemonize::Pid->new(pid => $pid)
43 # tell it to ignore zombies ...
44 $self->ignore_zombies( 1 );
45 $self->no_double_fork( 1 );
47 return unless $self->is_daemon;
48 # change to our local dir
49 # so that we can debug easier
51 # make it easy to find with ps
54 print "Got INT! Oh Noes!";
58 print "Hello from $$\n";
65 my $d = MyFooDaemon->new;
66 isa_ok($d, 'MyFooDaemon');
67 does_ok($d, 'MooseX::Daemonize::Core');
70 exception { $d->start },
72 '... successfully daemonized from (' . $$ . ')',
75 my $p = $d->daemon_pid;
76 isa_ok($p, 'MooseX::Daemonize::Pid');
78 ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
84 diag `ps -x | grep test-app`;
89 diag "killed $pid" if DEBUG;
91 # give the process time to be killed on slow/loaded systems
93 last unless kill 0 => $pid;
94 # sleep a little before retrying
101 diag `ps -x | grep test-app`;
104 ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
106 unlink $ENV{MX_DAEMON_STDOUT};
107 unlink $ENV{MX_DAEMON_STDERR};