1 package MooseX::Daemonize;
2 use strict; # because Kwalitee is pedantic
10 use Moose::Util::TypeConstraints;
12 with qw(MooseX::Getopt);
20 ( my $name = lc $_[0]->meta->name ) =~ s/::/_/g;
29 default => sub { return '/' },
38 default => sub { return '/var/run' },
41 subtype 'Pidfile' => as 'Object' => where { $_->isa('File::Pid') };
42 coerce 'Pidfile' => from 'Str' => via {
43 File::Pid->new( { file => $_, } );
53 die 'Cannot write to ' . $_[0]->pidbase unless -w $_[0]->pidbase;
54 my $file = $_[0]->pidbase . '/' . $_[0]->progname . '.pid';
55 File::Pid->new( { file => $file } );
60 remove_pid => 'remove',
66 metaclass => 'Getopt',
80 return if $self->check;
82 $self->daemonize unless $self->foreground;
84 # Avoid 'stdin reopened for output' warning with newer perls
86 open( NULL, '/dev/null' );
99 my ( $self, %args ) = @_;
100 my $pid = $self->get_pid;
101 $self->_kill($pid) unless $self->foreground();
103 return 1 if $args{no_exit};
109 $self->stop( no_exit => 1 );
115 $SIG{INT} = sub { $self->handle_sigint; };
116 $SIG{HUP} = sub { $self->handle_sighup };
119 sub handle_sigint { $_[0]->stop; }
120 sub handle_sighup { $_[0]->restart; }
123 confess "_kill isn't public" unless caller eq __PACKAGE__;
124 my ( $self, $pid ) = @_;
126 unless ( CORE::kill 0 => $pid ) {
128 # warn "$pid already appears dead.";
134 # warn "$pid is us! Can't commit suicied.";
138 CORE::kill( 2, $pid ); # Try SIGINT
139 sleep(2) if CORE::kill( 0, $pid );
141 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
142 CORE::kill( 15, $pid ); # try SIGTERM
143 sleep(2) if CORE::kill( 0, $pid );
146 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
147 CORE::kill( 9, $pid ); # finally try SIGKILL
148 sleep(3) if CORE::kill( 0, $pid );
151 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
152 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
163 MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
168 This document describes MooseX::Daemonize version 0.0.1
175 with qw(MooseX::Daemonize);
178 my ( $self, $file ) = @_;
179 open( FILE, ">$file" ) || die;
185 # then in the main package ...
187 my $daemon = FileMaker->new();
189 $daemon->create_file($file);
194 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
195 This module helps provide the basic infrastructure to do that.
203 The name of our daemon, defaults to $0
207 The base for our bid, defaults to /var/run/$progname
211 The file we store our PID in, defaults to /var/run/$progname/
213 =item foreground Bool
215 If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
225 Check to see if an instance is already running.
229 Setup a pidfile, fork, then setup the signal handlers.
233 Stop the process matching the pidfile, and unlinks the pidfile.
244 Calls C<Proc::Daemon::Init> to daemonize this process.
248 Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
250 =item setup_signals()
252 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
254 =item handle_sigint()
256 Handle a INT signal, by default calls C<$self->stop()>
258 =item handle_sighup()
260 Handle a HUP signal. By default calls C<$self->restart()>
264 Lookup the pid from our pidfile.
268 Save the current pid in our pidfile
276 The C<meta()> method from L<Class::MOP::Class>
282 =for author to fill in:
283 A list of all the other modules that this module relies upon,
284 including any restrictions on versions, and an indication whether
285 the module is part of the standard Perl distribution, part of the
286 module's distribution, or must be installed separately. ]
288 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Pid>
290 =head1 INCOMPATIBILITIES
292 =for author to fill in:
293 A list of any modules that this module cannot be used in conjunction
294 with. This may be due to name conflicts in the interface, or
295 competition for system or program resources, or due to internal
296 limitations of Perl (for example, many modules that use source code
297 filters are mutually incompatible).
302 =head1 BUGS AND LIMITATIONS
304 =for author to fill in:
305 A list of known problems with the module, together with some
306 indication Whether they are likely to be fixed in an upcoming
307 release. Also a list of restrictions on the features the module
308 does provide: data types that cannot be handled, performance issues
309 and the circumstances in which they may arise, practical
310 limitations on the size of data sets, special cases that are not
313 No bugs have been reported.
315 Please report any bugs or feature requests to
316 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
317 L<http://rt.cpan.org>.
321 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
325 Chris Prather C<< <perigrin@cpan.org> >>
329 Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, and the #moose denzians
331 =head1 LICENCE AND COPYRIGHT
333 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
335 This module is free software; you can redistribute it and/or
336 modify it under the same terms as Perl itself. See L<perlartistic>.
339 =head1 DISCLAIMER OF WARRANTY
341 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
342 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
343 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
344 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
345 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
346 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
347 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
348 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
349 NECESSARY SERVICING, REPAIR, OR CORRECTION.
351 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
352 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
353 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
354 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
355 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
356 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
357 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
358 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
359 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF