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;
29 default => sub { return '/' },
38 default => sub { return '/var/run' },
47 die 'Cannot write to ' . $_[0]->pidbase unless -w $_[0]->pidbase;
48 $_[0]->pidbase . '/' . $_[0]->progname . '.pid';
53 metaclass => 'Getopt',
62 if ( my $pid = $self->get_pid ) {
63 my $prog = $self->progname;
64 if ( CORE::kill 0 => $pid ) {
65 croak "$prog already running ($pid).";
67 carp "$prog not running but found pid ($pid)."
68 . "Perhaps the pid file (@{ [$self->pidfile] }) is stale?";
81 return if $self->check;
83 $self->daemonize unless $self->foreground;
85 # Avoid 'stdin reopened for output' warning with newer perls
87 open( NULL, '/dev/null' );
101 my $pidfile = $self->pidfile;
102 lock( $pidfile, undef, 'nonblocking' )
103 or croak "Could not lock PID file $pidfile: $!";
104 write_file( $pidfile, "$$\n" );
111 my $pidfile = $self->pidfile;
112 lock( $pidfile, undef, 'nonblocking' )
113 or croak "Could not lock PID file $pidfile: $!";
121 my $pidfile = $self->pidfile;
122 return unless -e $pidfile;
123 chomp( my $pid = read_file($pidfile) );
128 my ( $self, %args ) = @_;
129 my $pid = $self->get_pid;
130 $self->kill($pid) unless $self->foreground();
132 return 1 if $args{no_exit};
138 $self->stop( noexit => 1 );
144 $SIG{INT} = sub { $self->handle_sigint; };
145 $SIG{HUP} = sub { $self->handle_sighup };
148 sub handle_sigint { $_[0]->stop; }
149 sub handle_sighup { $_[0]->restart; }
152 my ( $self, $pid ) = @_;
154 unless ( CORE::kill 0 => $pid ) {
156 # warn "$pid already appears dead.";
162 # warn "$pid is us! Can't commit suicied.";
166 CORE::kill( 2, $pid ); # Try SIGINT
167 sleep(2) if CORE::kill( 0, $pid );
169 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
170 CORE::kill( 15, $pid ); # try SIGTERM
171 sleep(2) if CORE::kill( 0, $pid );
174 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
175 CORE::kill( 9, $pid ); # finally try SIGKILL
176 sleep(2) if CORE::kill( 0, $pid );
179 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
180 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
191 MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
196 This document describes MooseX::Daemonize version 0.0.1
203 with qw(MooseX::Daemonize);
206 my ( $self, $file ) = @_;
207 open( FILE, ">$file" ) || die;
213 # then in the main package ...
215 my $daemon = FileMaker->new();
217 $daemon->create_file($file);
222 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
223 This module helps provide the basic infrastructure to do that.
231 The name of our daemon, defaults to $0
235 The base for our bid, defaults to /var/run/$progname
239 The file we store our PID in, defaults to /var/run/$progname/
241 =item foreground Bool
243 If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
253 Check to see if an instance is already running.
257 Setup a pidfile, fork, then setup the signal handlers.
261 Stop the process matching the pidfile, and unlinks the pidfile.
272 Calls C<Proc::Daemon::Init> to daemonize this process.
276 Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
278 =item setup_signals()
280 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
282 =item handle_sigint()
284 Handle a INT signal, by default calls C<$self->stop()>;
286 =item handle_sighup()
288 Handle a HUP signal. Nothing is done by default.
298 the C<meta()> method from L<Class::MOP::Class>
304 =for author to fill in:
305 A list of all the other modules that this module relies upon,
306 including any restrictions on versions, and an indication whether
307 the module is part of the standard Perl distribution, part of the
308 module's distribution, or must be installed separately. ]
310 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
312 =head1 INCOMPATIBILITIES
314 =for author to fill in:
315 A list of any modules that this module cannot be used in conjunction
316 with. This may be due to name conflicts in the interface, or
317 competition for system or program resources, or due to internal
318 limitations of Perl (for example, many modules that use source code
319 filters are mutually incompatible).
324 =head1 BUGS AND LIMITATIONS
326 =for author to fill in:
327 A list of known problems with the module, together with some
328 indication Whether they are likely to be fixed in an upcoming
329 release. Also a list of restrictions on the features the module
330 does provide: data types that cannot be handled, performance issues
331 and the circumstances in which they may arise, practical
332 limitations on the size of data sets, special cases that are not
335 No bugs have been reported.
337 Please report any bugs or feature requests to
338 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
339 L<http://rt.cpan.org>.
343 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
347 Chris Prather C<< <perigrin@cpan.org> >>
350 =head1 LICENCE AND COPYRIGHT
352 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
354 This module is free software; you can redistribute it and/or
355 modify it under the same terms as Perl itself. See L<perlartistic>.
358 =head1 DISCLAIMER OF WARRANTY
360 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
361 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
362 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
363 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
364 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
365 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
366 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
367 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
368 NECESSARY SERVICING, REPAIR, OR CORRECTION.
370 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
371 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
372 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
373 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
374 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
375 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
376 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
377 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
378 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF