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;
30 default => sub { return '/var/run' },
38 default => sub { $_[0]->pidbase . '/' . $_[0]->progname . '.pid' },
42 metaclass => 'Getopt',
51 my $pidfile = $self->pidfile;
53 my $prog = $self->progname;
54 chomp( my $pid = read_file($pidfile) );
55 if ( kill 0 => $pid ) {
56 croak "$prog already running ($pid).";
58 carp "$prog not running but $pidfile exists. Perhaps it is stale?";
71 return if $self->check;
73 $self->daemonize unless $self->foreground;
75 my $pidfile = $self->pidfile;
76 lock( $pidfile, undef, 'nonblocking' )
77 or croak "Could not lock PID file $pidfile: $!";
78 write_file( $pidfile, "$$\n" );
86 my $pidfile = $self->pidfile;
87 unless ( -e $pidfile ) {
88 carp $self->progname . ' is not currently running.';
91 chomp( my $pid = read_file($pidfile) );
92 $self->kill($pid) unless $self->foreground();
93 lock( $pidfile, undef, 'nonblocking' )
94 or croak "Could not lock PID file $pidfile: $!";
108 $SIG{INT} = sub { $_[0]->handle_sigint; };
109 $SIG{HUP} = sub { $_[0]->handle_sighup };
112 sub handle_sigint { $_[0]->stop; }
113 sub handle_sighup { return; }
116 my ( $self, $pid ) = @_;
117 unless ( kill 0 => $pid ) {
118 carp "$pid already appears dead.";
122 kill( 2, $pid ); # Try SIGINT
123 sleep(1) if kill( 0, $pid );
125 unless ( kill 0 => $pid or $!{EPERM} ) { # IF it is still running
126 kill( 15, $pid ); # try SIGTERM
127 sleep(1) if kill( 0, $pid );
130 unless ( kill 0 => $pid or $!{EPERM} ) { # IF it is still running
131 kill( 9, $pid ); # finally try SIGKILL
132 sleep(1) if kill( 0, $pid );
135 unless ( kill 0 => $pid or $!{EPERM} ) { # IF it is still running
136 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
147 MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
152 This document describes MooseX::Daemonize version 0.0.1
159 with qw(MooseX::Daemonize);
162 my ( $self, $file ) = @_;
163 open( FILE, ">$file" ) || die;
169 # then in the main package ...
171 my $daemon = FileMaker->new();
173 $daemon->create_file($file);
178 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
179 This module helps provide the basic infrastructure to do that.
187 The name of our daemon, defaults to $0
191 The base for our bid, defaults to /var/run/$progname
195 The file we store our PID in, defaults to /var/run/$progname/
197 =item foreground Bool
199 If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
209 Check to see if an instance is already running.
213 Setup a pidfile, fork, then setup the signal handlers.
217 Stop the process matching the pidfile, and unlinks the pidfile.
228 Calls C<Proc::Daemon::Init> to daemonize this process.
232 Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
234 =item setup_signals()
236 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
238 =item handle_sigint()
240 Handle a INT signal, by default calls C<$self->stop()>;
242 =item handle_sighup()
244 Handle a HUP signal. Nothing is done by default.
248 the C<meta()> method from L<Class::MOP::Class>
254 =for author to fill in:
255 A list of all the other modules that this module relies upon,
256 including any restrictions on versions, and an indication whether
257 the module is part of the standard Perl distribution, part of the
258 module's distribution, or must be installed separately. ]
260 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
262 =head1 INCOMPATIBILITIES
264 =for author to fill in:
265 A list of any modules that this module cannot be used in conjunction
266 with. This may be due to name conflicts in the interface, or
267 competition for system or program resources, or due to internal
268 limitations of Perl (for example, many modules that use source code
269 filters are mutually incompatible).
274 =head1 BUGS AND LIMITATIONS
276 =for author to fill in:
277 A list of known problems with the module, together with some
278 indication Whether they are likely to be fixed in an upcoming
279 release. Also a list of restrictions on the features the module
280 does provide: data types that cannot be handled, performance issues
281 and the circumstances in which they may arise, practical
282 limitations on the size of data sets, special cases that are not
285 No bugs have been reported.
287 Please report any bugs or feature requests to
288 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
289 L<http://rt.cpan.org>.
293 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
297 Chris Prather C<< <perigrin@cpan.org> >>
300 =head1 LICENCE AND COPYRIGHT
302 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
304 This module is free software; you can redistribute it and/or
305 modify it under the same terms as Perl itself. See L<perlartistic>.
308 =head1 DISCLAIMER OF WARRANTY
310 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
311 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
312 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
313 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
314 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
315 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
316 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
317 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
318 NECESSARY SERVICING, REPAIR, OR CORRECTION.
320 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
321 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
322 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
323 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
324 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
325 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
326 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
327 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
328 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF