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;
30 default => sub { return '/' },
38 default => sub { return '/var/run' },
41 subtype 'Pidfile' => as 'Object' => where { $_->isa('File::Pid') };
43 coerce 'Pidfile' => from 'Str' => via { File::Pid->new( { file => $_, } ); };
51 predicate => 'has_pidfile',
53 my $file = $_[0]->pidbase . '/' . $_[0]->progname . '.pid';
54 die "Cannot write to $file" unless (-e $file ? -w $file : -w $_[0]->pidbase);
55 File::Pid->new( { file => $file } );
60 remove_pid => 'remove',
67 metaclass => 'MooseX::Getopt::Meta::Attribute',
82 return if Proc::Daemon::Fork;
89 confess "instance already running" if $self->check;
90 $self->daemonize unless $self->foreground;
92 return unless $self->is_daemon;
94 $self->pidfile->pid($$);
96 # Avoid 'stdin reopened for output' warning with newer perls
98 open( NULL, '/dev/null' );
103 chdir $self->basedir;
106 $self->setup_signals;
111 my ( $self, %args ) = @_;
112 my $pid = $self->get_pid;
113 $self->_kill($pid) unless $self->foreground();
115 return 1 if $args{no_exit};
121 $self->stop( no_exit => 1 );
127 $SIG{INT} = sub { $self->handle_sigint; };
128 $SIG{HUP} = sub { $self->handle_sighup };
131 sub handle_sigint { $_[0]->stop; }
132 sub handle_sighup { $_[0]->restart; }
135 confess "_kill isn't public" unless caller eq __PACKAGE__;
136 my ( $self, $pid ) = @_;
138 unless ( CORE::kill 0 => $pid ) {
140 # warn "$pid already appears dead.";
146 # warn "$pid is us! Can't commit suicied.";
150 CORE::kill( 2, $pid ); # Try SIGINT
151 sleep(2) if CORE::kill( 0, $pid );
153 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
154 CORE::kill( 15, $pid ); # try SIGTERM
155 sleep(2) if CORE::kill( 0, $pid );
158 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
159 CORE::kill( 9, $pid ); # finally try SIGKILL
160 sleep(3) if CORE::kill( 0, $pid );
163 unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running
164 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
175 MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
180 This document describes MooseX::Daemonize version 0.0.1
187 with qw(MooseX::Daemonize);
190 my ( $self, $file ) = @_;
191 open( FILE, ">$file" ) || die;
197 # then in the main package ...
199 my $daemon = FileMaker->new();
201 $daemon->create_file($file);
206 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
207 This module helps provide the basic infrastructure to do that.
215 The name of our daemon, defaults to $self->meta->name =~ s/::/_/;
219 The base for our bid, defaults to /var/run/$progname
223 The file we store our PID in, defaults to /var/run/$progname/
225 =item foreground Bool
227 If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
231 If true, the process is the backgrounded process. This is useful for example in an after 'start' => sub { } block
241 Check to see if an instance is already running.
245 Setup a pidfile, fork, then setup the signal handlers.
249 Stop the process matching the pidfile, and unlinks the pidfile.
260 Calls C<Proc::Daemon::Init> to daemonize this process.
264 Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
266 =item setup_signals()
268 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
270 =item handle_sigint()
272 Handle a INT signal, by default calls C<$self->stop()>
274 =item handle_sighup()
276 Handle a HUP signal. By default calls C<$self->restart()>
280 Lookup the pid from our pidfile.
284 Save the current pid in our pidfile
292 The C<meta()> method from L<Class::MOP::Class>
298 =for author to fill in:
299 A list of all the other modules that this module relies upon,
300 including any restrictions on versions, and an indication whether
301 the module is part of the standard Perl distribution, part of the
302 module's distribution, or must be installed separately. ]
304 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Pid>
306 =head1 INCOMPATIBILITIES
308 =for author to fill in:
309 A list of any modules that this module cannot be used in conjunction
310 with. This may be due to name conflicts in the interface, or
311 competition for system or program resources, or due to internal
312 limitations of Perl (for example, many modules that use source code
313 filters are mutually incompatible).
318 =head1 BUGS AND LIMITATIONS
320 =for author to fill in:
321 A list of known problems with the module, together with some
322 indication Whether they are likely to be fixed in an upcoming
323 release. Also a list of restrictions on the features the module
324 does provide: data types that cannot be handled, performance issues
325 and the circumstances in which they may arise, practical
326 limitations on the size of data sets, special cases that are not
329 No bugs have been reported.
331 Please report any bugs or feature requests to
332 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
333 L<http://rt.cpan.org>.
337 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
341 Chris Prather C<< <perigrin@cpan.org> >>
345 Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, and the #moose denzians
347 =head1 LICENCE AND COPYRIGHT
349 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
351 This module is free software; you can redistribute it and/or
352 modify it under the same terms as Perl itself. See L<perlartistic>.
355 =head1 DISCLAIMER OF WARRANTY
357 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
358 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
359 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
360 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
361 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
362 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
363 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
364 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
365 NECESSARY SERVICING, REPAIR, OR CORRECTION.
367 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
368 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
369 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
370 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
371 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
372 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
373 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
374 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
375 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF