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 $pid exists. Perhaps it is stale?";
73 return if $self->check;
75 $self->daemonize unless $self->foreground;
77 # Avoid 'stdin reopened for output' warning with newer perls
78 open( NULL, '/dev/null' );
88 my $pidfile = $self->pidfile;
89 lock( $pidfile, undef, 'nonblocking' )
90 or croak "Could not lock PID file $pidfile: $!";
91 write_file( $pidfile, "$$\n" );
98 my $pidfile = $self->pidfile;
99 lock( $pidfile, undef, 'nonblocking' )
100 or croak "Could not lock PID file $pidfile: $!";
108 my $pidfile = $self->pidfile;
109 return unless -e $pidfile;
110 chomp( my $pid = read_file($pidfile) );
115 my ( $self, %args ) = @_;
116 my $pid = $self->get_pid;
117 $self->kill($pid) unless $self->foreground();
119 return 1 if $args{no_exit};
125 $self->stop( noexit => 1 );
131 $SIG{INT} = sub { $self->handle_sigint; };
132 $SIG{HUP} = sub { $self->handle_sighup };
135 sub handle_sigint { $_[0]->stop; }
136 sub handle_sighup { $_[0]->restart; }
139 my ( $self, $pid ) = @_;
141 unless ( CORE::kill 0 => $pid ) {
143 # warn "$pid already appears dead.";
149 # warn "$pid is us! Can't commit suicied.";
153 CORE::kill( 2, $pid ); # Try SIGINT
154 sleep(2) if CORE::kill( 0, $pid );
156 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
157 CORE::kill( 15, $pid ); # try SIGTERM
158 sleep(2) if CORE::kill( 0, $pid );
161 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
162 CORE::kill( 9, $pid ); # finally try SIGKILL
163 sleep(2) if CORE::kill( 0, $pid );
166 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
167 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
178 MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
183 This document describes MooseX::Daemonize version 0.0.1
190 with qw(MooseX::Daemonize);
193 my ( $self, $file ) = @_;
194 open( FILE, ">$file" ) || die;
200 # then in the main package ...
202 my $daemon = FileMaker->new();
204 $daemon->create_file($file);
209 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
210 This module helps provide the basic infrastructure to do that.
218 The name of our daemon, defaults to $0
222 The base for our bid, defaults to /var/run/$progname
226 The file we store our PID in, defaults to /var/run/$progname/
228 =item foreground Bool
230 If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
240 Check to see if an instance is already running.
244 Setup a pidfile, fork, then setup the signal handlers.
248 Stop the process matching the pidfile, and unlinks the pidfile.
259 Calls C<Proc::Daemon::Init> to daemonize this process.
263 Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
265 =item setup_signals()
267 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
269 =item handle_sigint()
271 Handle a INT signal, by default calls C<$self->stop()>;
273 =item handle_sighup()
275 Handle a HUP signal. Nothing is done by default.
285 the C<meta()> method from L<Class::MOP::Class>
291 =for author to fill in:
292 A list of all the other modules that this module relies upon,
293 including any restrictions on versions, and an indication whether
294 the module is part of the standard Perl distribution, part of the
295 module's distribution, or must be installed separately. ]
297 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
299 =head1 INCOMPATIBILITIES
301 =for author to fill in:
302 A list of any modules that this module cannot be used in conjunction
303 with. This may be due to name conflicts in the interface, or
304 competition for system or program resources, or due to internal
305 limitations of Perl (for example, many modules that use source code
306 filters are mutually incompatible).
311 =head1 BUGS AND LIMITATIONS
313 =for author to fill in:
314 A list of known problems with the module, together with some
315 indication Whether they are likely to be fixed in an upcoming
316 release. Also a list of restrictions on the features the module
317 does provide: data types that cannot be handled, performance issues
318 and the circumstances in which they may arise, practical
319 limitations on the size of data sets, special cases that are not
322 No bugs have been reported.
324 Please report any bugs or feature requests to
325 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
326 L<http://rt.cpan.org>.
330 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
334 Chris Prather C<< <perigrin@cpan.org> >>
337 =head1 LICENCE AND COPYRIGHT
339 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
341 This module is free software; you can redistribute it and/or
342 modify it under the same terms as Perl itself. See L<perlartistic>.
345 =head1 DISCLAIMER OF WARRANTY
347 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
348 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
349 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
350 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
351 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
352 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
353 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
354 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
355 NECESSARY SERVICING, REPAIR, OR CORRECTION.
357 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
358 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
359 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
360 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
361 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
362 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
363 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
364 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
365 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF