1 package MooseX::Daemonize;
2 use strict; # because Kwalitee is pedantic
11 with qw(MooseX::Getopt);
16 default => sub { lc $_[0]->meta->name },
24 default => sub { return '/var/run' },
32 default => sub { $_[0]->pidbase .'/'. $_[0]->progname . '.pid' },
36 metaclass => 'Getopt',
45 my $pidfile = $self->pidfile;
47 my $prog = $self->progname;
48 chomp( my $pid = read_file($pidfile) );
49 unless ( kill 0 => $pid or $!{EPERM} ) {
50 carp "$prog already running ($pid).";
53 carp "$prog not running but $pidfile exists. Perhaps it is stale?";
62 return if $self->check;
64 $self->daemonize unless $self->foreground;
66 my $pidfile = $self->pidfile;
67 lock( $pidfile, undef, 'nonblocking' )
68 or croak "Could not lock PID file $pidfile: $!";
69 write_file( $pidfile, "$$\n" );
77 my $pidfile = $self->pidfile;
78 unless ( -e $pidfile ) {
79 croak $self->progname . 'is not currently running.';
81 lock( $pidfile, undef, 'nonblocking' )
82 or croak "Could not lock PID file $pidfile: $!";
83 chomp( my $pid = read_file($pidfile) );
102 $SIG{INT} = sub { $_[0]->handle_sigint; };
103 $SIG{HUP} = sub { $_[0]->handle_sighup };
106 sub handle_sigint { $_[0]->stop; }
107 sub handle_sighup { return; }
110 my ( $self, $pid ) = @_;
111 unless ( kill 0 => $pid or $!{EPERM} ) {
112 carp "$pid appears dead.";
116 kill( 2, $pid ); # Try SIGINT
117 sleep(1) if kill( 0, $pid );
119 unless ( kill 0 => $pid or $!{EPERM} ) { # IF it is still running
120 kill( 15, $pid ); # try SIGTERM
121 sleep(1) if kill( 0, $pid );
124 unless ( kill 0 => $pid or $!{EPERM} ) { # IF it is still running
125 kill( 9, $pid ); # finally try SIGKILL
126 sleep(1) if kill( 0, $pid );
129 unless ( kill 0 => $pid or $!{EPERM} ) { # IF it is still running
130 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
141 MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
146 This document describes MooseX::Daemonize version 0.0.1
153 with qw(MooseX::Daemonize);
156 my ( $self, $file ) = @_;
157 open( FILE, ">$file" ) || die;
163 # then in the main package ...
165 my $daemon = FileMaker->new();
167 $daemon->create_file($file);
172 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
173 This module helps provide the basic infrastructure to do that.
181 The name of our daemon, defaults to $0
185 The base for our bid, defaults to /var/run/$progname
189 The file we store our PID in, defaults to /var/run/$progname/
191 =item foreground Bool
193 If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
203 Check to see if an instance is already running.
207 Setup a pidfile, fork, then setup the signal handlers.
211 Stop the process matching the pidfile, and unlinks the pidfile.
222 Calls C<Proc::Daemon::Init> to daemonize this process.
226 Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
228 =item setup_signals()
230 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
232 =item handle_sigint()
234 Handle a INT signal, by default calls C<$self->stop()>;
236 =item handle_sighup()
238 Handle a HUP signal. Nothing is done by default.
242 the C<meta()> method from L<Class::MOP::Class>
248 =for author to fill in:
249 A list of all the other modules that this module relies upon,
250 including any restrictions on versions, and an indication whether
251 the module is part of the standard Perl distribution, part of the
252 module's distribution, or must be installed separately. ]
254 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
256 =head1 INCOMPATIBILITIES
258 =for author to fill in:
259 A list of any modules that this module cannot be used in conjunction
260 with. This may be due to name conflicts in the interface, or
261 competition for system or program resources, or due to internal
262 limitations of Perl (for example, many modules that use source code
263 filters are mutually incompatible).
268 =head1 BUGS AND LIMITATIONS
270 =for author to fill in:
271 A list of known problems with the module, together with some
272 indication Whether they are likely to be fixed in an upcoming
273 release. Also a list of restrictions on the features the module
274 does provide: data types that cannot be handled, performance issues
275 and the circumstances in which they may arise, practical
276 limitations on the size of data sets, special cases that are not
279 No bugs have been reported.
281 Please report any bugs or feature requests to
282 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
283 L<http://rt.cpan.org>.
287 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
291 Chris Prather C<< <perigrin@cpan.org> >>
294 =head1 LICENCE AND COPYRIGHT
296 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
298 This module is free software; you can redistribute it and/or
299 modify it under the same terms as Perl itself. See L<perlartistic>.
302 =head1 DISCLAIMER OF WARRANTY
304 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
305 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
306 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
307 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
308 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
309 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
310 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
311 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
312 NECESSARY SERVICING, REPAIR, OR CORRECTION.
314 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
315 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
316 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
317 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
318 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
319 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
320 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
321 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
322 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF