1 package MooseX::Daemonize;
2 use strict; # because Kwalitee is pedantic
12 with qw(MooseX::Getopt);
20 ( my $name = lc $_[0]->meta->name ) =~ s/::/_/g;
31 default => sub { return '/var/run' },
40 die 'Cannot write to ' . $_[0]->pidbase unless -w $_[0]->pidbase;
41 $_[0]->pidbase . '/' . $_[0]->progname . '.pid';
46 metaclass => 'Getopt',
55 if ( my $pid = $self->get_pid ) {
56 my $prog = $self->progname;
57 if ( CORE::kill 0 => $pid ) {
58 croak "$prog already running ($pid).";
60 carp "$prog not running but found pid ($pid)."
61 . "Perhaps the pid file (@{ [$self->pidfile] }) is stale?";
74 return if $self->check;
76 $self->daemonize unless $self->foreground;
78 # Avoid 'stdin reopened for output' warning with newer perls
80 open( NULL, '/dev/null' );
91 my $pidfile = $self->pidfile;
92 lock( $pidfile, undef, 'nonblocking' )
93 or croak "Could not lock PID file $pidfile: $!";
94 write_file( $pidfile, "$$\n" );
101 my $pidfile = $self->pidfile;
102 lock( $pidfile, undef, 'nonblocking' )
103 or croak "Could not lock PID file $pidfile: $!";
111 my $pidfile = $self->pidfile;
112 return unless -e $pidfile;
113 chomp( my $pid = read_file($pidfile) );
118 my ( $self, %args ) = @_;
119 my $pid = $self->get_pid;
120 $self->kill($pid) unless $self->foreground();
122 return 1 if $args{no_exit};
128 $self->stop( noexit => 1 );
134 $SIG{INT} = sub { $self->handle_sigint; };
135 $SIG{HUP} = sub { $self->handle_sighup };
138 sub handle_sigint { $_[0]->stop; }
139 sub handle_sighup { $_[0]->restart; }
142 my ( $self, $pid ) = @_;
144 unless ( CORE::kill 0 => $pid ) {
146 # warn "$pid already appears dead.";
152 # warn "$pid is us! Can't commit suicied.";
156 CORE::kill( 2, $pid ); # Try SIGINT
157 sleep(2) if CORE::kill( 0, $pid );
159 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
160 CORE::kill( 15, $pid ); # try SIGTERM
161 sleep(2) if CORE::kill( 0, $pid );
164 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
165 CORE::kill( 9, $pid ); # finally try SIGKILL
166 sleep(2) if CORE::kill( 0, $pid );
169 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
170 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
181 MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
186 This document describes MooseX::Daemonize version 0.0.1
193 with qw(MooseX::Daemonize);
196 my ( $self, $file ) = @_;
197 open( FILE, ">$file" ) || die;
203 # then in the main package ...
205 my $daemon = FileMaker->new();
207 $daemon->create_file($file);
212 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
213 This module helps provide the basic infrastructure to do that.
221 The name of our daemon, defaults to $0
225 The base for our bid, defaults to /var/run/$progname
229 The file we store our PID in, defaults to /var/run/$progname/
231 =item foreground Bool
233 If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
243 Check to see if an instance is already running.
247 Setup a pidfile, fork, then setup the signal handlers.
251 Stop the process matching the pidfile, and unlinks the pidfile.
262 Calls C<Proc::Daemon::Init> to daemonize this process.
266 Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
268 =item setup_signals()
270 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
272 =item handle_sigint()
274 Handle a INT signal, by default calls C<$self->stop()>;
276 =item handle_sighup()
278 Handle a HUP signal. Nothing is done by default.
288 the C<meta()> method from L<Class::MOP::Class>
294 =for author to fill in:
295 A list of all the other modules that this module relies upon,
296 including any restrictions on versions, and an indication whether
297 the module is part of the standard Perl distribution, part of the
298 module's distribution, or must be installed separately. ]
300 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
302 =head1 INCOMPATIBILITIES
304 =for author to fill in:
305 A list of any modules that this module cannot be used in conjunction
306 with. This may be due to name conflicts in the interface, or
307 competition for system or program resources, or due to internal
308 limitations of Perl (for example, many modules that use source code
309 filters are mutually incompatible).
314 =head1 BUGS AND LIMITATIONS
316 =for author to fill in:
317 A list of known problems with the module, together with some
318 indication Whether they are likely to be fixed in an upcoming
319 release. Also a list of restrictions on the features the module
320 does provide: data types that cannot be handled, performance issues
321 and the circumstances in which they may arise, practical
322 limitations on the size of data sets, special cases that are not
325 No bugs have been reported.
327 Please report any bugs or feature requests to
328 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
329 L<http://rt.cpan.org>.
333 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
337 Chris Prather C<< <perigrin@cpan.org> >>
340 =head1 LICENCE AND COPYRIGHT
342 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
344 This module is free software; you can redistribute it and/or
345 modify it under the same terms as Perl itself. See L<perlartistic>.
348 =head1 DISCLAIMER OF WARRANTY
350 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
351 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
352 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
353 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
354 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
355 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
356 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
357 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
358 NECESSARY SERVICING, REPAIR, OR CORRECTION.
360 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
361 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
362 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
363 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
364 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
365 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
366 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
367 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
368 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF