1 package MooseX::Daemonize;
2 use strict; # because Kwalitee is pedantic
4 use MooseX::Types::Path::Class;
5 use Moose::Util::TypeConstraints;
11 use MooseX::Daemonize::PidFile;
14 MooseX::Daemonize::Core
15 MooseX::Daemonize::WithSignalHandling
25 ( my $name = lc $_[0]->meta->name ) =~ s/::/_/g;
31 isa => 'Path::Class::Dir',
36 default => sub { Path::Class::Dir->new('/') },
40 isa => 'Path::Class::Dir',
45 default => sub { Path::Class::Dir->new('var', 'run') },
48 coerce 'MooseX::Daemonize::PidFile'
50 => via { MooseX::Daemonize::PidFile->new( file => $_ ) };
53 isa => 'MooseX::Daemonize::PidFile',
58 predicate => 'has_pidfile',
60 my $file = $_[0]->pidbase . '/' . $_[0]->progname . '.pid';
61 confess "Cannot write to $file" unless (-e $file ? -w $file : -w $_[0]->pidbase);
62 MooseX::Daemonize::PidFile->new( file => $file );
67 metaclass => 'Getopt',
84 confess "instance already running" if $self->pidfile->running;
86 $self->daemonize unless $self->foreground;
88 return unless $self->is_daemon;
90 $self->pidfile->pid($$);
92 # Avoid 'stdin reopened for output' warning with newer perls
94 open( NULL, '/dev/null' );
101 $self->pidfile->write;
102 $self->setup_signals;
106 # Make _kill *really* private
110 my ( $self, %args ) = @_;
111 my $pid = $self->pidfile->pid;
112 $self->$_kill($pid) unless $self->foreground();
113 $self->pidfile->remove;
114 return 1 if $args{no_exit};
120 $self->stop( no_exit => 1 );
125 my ($self, $signal) = @_;
126 return $self->handle_sigint if $signal eq 'INT';
127 return $self->handle_sighup if $signal eq 'HUP';
130 sub handle_sigint { $_[0]->stop; }
131 sub handle_sighup { $_[0]->restart; }
134 my ( $self, $pid ) = @_;
136 unless ( CORE::kill 0 => $pid ) {
138 # warn "$pid already appears dead.";
144 # warn "$pid is us! Can't commit suicide.";
148 my $timeout = $self->stop_timeout;
150 # kill 0 => $pid returns 0 if the process is dead
151 # $!{EPERM} could also be true if we cant kill it (permission error)
153 # Try SIGINT ... 2s ... SIGTERM ... 2s ... SIGKILL ... 3s ... UNDEAD!
154 for ( [ 2, $timeout ], [15, $timeout], [9, $timeout * 1.5] ) {
155 my ($signal, $timeout) = @$_;
156 $timeout = int $timeout;
158 CORE::kill($signal, $pid);
160 last unless CORE::kill 0 => $pid or $!{EPERM};
164 last unless CORE::kill 0 => $pid or $!{EPERM};
169 return unless ( CORE::kill 0 => $pid or $!{EPERM} );
171 # IF it is still running
172 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
180 MooseX::Daemonize - provides a Role that daemonizes your Moose based
185 This document describes MooseX::Daemonize version 0.04
191 with qw(MooseX::Daemonize);
194 my ( $self, $file ) = @_;
195 open( FILE, ">$file" ) || die;
201 # then in the main package ...
203 my $daemon = FileMaker->new();
205 $daemon->create_file($file);
210 Often you want to write a persistant daemon that has a pid file, and responds
211 appropriately to Signals. This module helps provide the basic infrastructure
218 =item progname Path::Class::Dir | Str
220 The name of our daemon, defaults to $self->meta->name =~ s/::/_/;
222 =item pidbase Path::Class::Dir | Str
224 The base for our bid, defaults to /var/run/$progname
226 =item pidfile MooseX::Daemonize::PidFile | Str
228 The file we store our PID in, defaults to /var/run/$progname
230 =item foreground Bool
232 If true, the process won't background. Useful for debugging. This option can
233 be set via Getopt's -f.
237 If true, the process is the backgrounded process. This is useful for example
238 in an after 'start' => sub { } block
242 Number of seconds to wait for the process to stop, before trying harder to kill
243 it. Defaults to 2 seconds
253 Setup a pidfile, fork, then setup the signal handlers.
257 Stop the process matching the pidfile, and unlinks the pidfile.
268 Calls C<Proc::Daemon::Init> to daemonize this process.
270 =item setup_signals()
272 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
274 =item handle_sigint()
276 Handle a INT signal, by default calls C<$self->stop()>
278 =item handle_sighup()
280 Handle a HUP signal. By default calls C<$self->restart()>
284 The C<meta()> method from L<Class::MOP::Class>
290 =for author to fill in:
291 A list of all the other modules that this module relies upon,
292 including any restrictions on versions, and an indication whether
293 the module is part of the standard Perl distribution, part of the
294 module's distribution, or must be installed separately. ]
296 Obviously L<Moose>, and L<Proc::Daemon>
298 =head1 INCOMPATIBILITIES
300 =for author to fill in:
301 A list of any modules that this module cannot be used in conjunction
302 with. This may be due to name conflicts in the interface, or
303 competition for system or program resources, or due to internal
304 limitations of Perl (for example, many modules that use source code
305 filters are mutually incompatible).
310 =head1 BUGS AND LIMITATIONS
312 =for author to fill in:
313 A list of known problems with the module, together with some
314 indication Whether they are likely to be fixed in an upcoming
315 release. Also a list of restrictions on the features the module
316 does provide: data types that cannot be handled, performance issues
317 and the circumstances in which they may arise, practical
318 limitations on the size of data sets, special cases that are not
321 No bugs have been reported.
323 Please report any bugs or feature requests to
324 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
325 L<http://rt.cpan.org>.
329 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
333 Chris Prather C<< <perigrin@cpan.org> >>
337 Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and the
340 Some bug fixes sponsored by Takkle Inc.
342 =head1 LICENCE AND COPYRIGHT
344 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights
347 This module is free software; you can redistribute it and/or
348 modify it under the same terms as Perl itself. See L<perlartistic>.
351 =head1 DISCLAIMER OF WARRANTY
353 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
354 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
355 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
356 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
357 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
358 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
359 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
360 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
361 NECESSARY SERVICING, REPAIR, OR CORRECTION.
363 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
364 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
365 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
366 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
367 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
368 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
369 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
370 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
371 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF