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',
88 return if Proc::Daemon::Fork;
95 confess "instance already running" if $self->check;
96 $self->daemonize unless $self->foreground;
98 return unless $self->is_daemon;
100 $self->pidfile->pid($$);
102 # Avoid 'stdin reopened for output' warning with newer perls
104 open( NULL, '/dev/null' );
109 chdir $self->basedir;
112 $self->setup_signals;
116 # Make _kill *really* private
120 my ( $self, %args ) = @_;
121 my $pid = $self->get_pid;
122 $self->$_kill($pid) unless $self->foreground();
124 return 1 if $args{no_exit};
130 $self->stop( no_exit => 1 );
136 $SIG{INT} = sub { $self->handle_sigint; };
137 $SIG{HUP} = sub { $self->handle_sighup };
140 sub handle_sigint { $_[0]->stop; }
141 sub handle_sighup { $_[0]->restart; }
144 my ( $self, $pid ) = @_;
146 unless ( CORE::kill 0 => $pid ) {
148 # warn "$pid already appears dead.";
154 # warn "$pid is us! Can't commit suicide.";
158 my $timeout = $self->stop_timeout;
160 # kill 0 => $pid returns 0 if the process is dead
161 # $!{EPERM} could also be true if we cant kill it (permission error)
163 # Try SIGINT ... 2s ... SIGTERM ... 2s ... SIGKILL ... 3s ... UNDEAD!
164 for ( [ 2, $timeout ], [15, $timeout], [9, $timeout * 1.5] ) {
165 my ($signal, $timeout) = @$_;
166 $timeout = int $timeout;
168 CORE::kill($signal, $pid);
170 last unless CORE::kill 0 => $pid or $!{EPERM};
174 last unless CORE::kill 0 => $pid or $!{EPERM};
179 return unless ( CORE::kill 0 => $pid or $!{EPERM} );
181 # IF it is still running
182 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
190 MooseX::Daemonize - provides a Role that daemonizes your Moose based
195 This document describes MooseX::Daemonize version 0.04
201 with qw(MooseX::Daemonize);
204 my ( $self, $file ) = @_;
205 open( FILE, ">$file" ) || die;
211 # then in the main package ...
213 my $daemon = FileMaker->new();
215 $daemon->create_file($file);
220 Often you want to write a persistant daemon that has a pid file, and responds
221 appropriately to Signals. This module helps provide the basic infrastructure
230 The name of our daemon, defaults to $self->meta->name =~ s/::/_/;
234 The base for our bid, defaults to /var/run/$progname
238 The file we store our PID in, defaults to /var/run/$progname/
240 =item foreground Bool
242 If true, the process won't background. Useful for debugging. This option can
243 be set via Getopt's -f.
247 If true, the process is the backgrounded process. This is useful for example
248 in an after 'start' => sub { } block
252 Number of seconds to wait for the process to stop, before trying harder to kill
253 it. Defaults to 2 seconds
263 Check to see if an instance is already running.
267 Setup a pidfile, fork, then setup the signal handlers.
271 Stop the process matching the pidfile, and unlinks the pidfile.
282 Calls C<Proc::Daemon::Init> to daemonize this process.
284 =item setup_signals()
286 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
288 =item handle_sigint()
290 Handle a INT signal, by default calls C<$self->stop()>
292 =item handle_sighup()
294 Handle a HUP signal. By default calls C<$self->restart()>
298 Lookup the pid from our pidfile.
302 Save the current pid in our pidfile
310 The C<meta()> method from L<Class::MOP::Class>
316 =for author to fill in:
317 A list of all the other modules that this module relies upon,
318 including any restrictions on versions, and an indication whether
319 the module is part of the standard Perl distribution, part of the
320 module's distribution, or must be installed separately. ]
322 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Pid>
324 =head1 INCOMPATIBILITIES
326 =for author to fill in:
327 A list of any modules that this module cannot be used in conjunction
328 with. This may be due to name conflicts in the interface, or
329 competition for system or program resources, or due to internal
330 limitations of Perl (for example, many modules that use source code
331 filters are mutually incompatible).
336 =head1 BUGS AND LIMITATIONS
338 =for author to fill in:
339 A list of known problems with the module, together with some
340 indication Whether they are likely to be fixed in an upcoming
341 release. Also a list of restrictions on the features the module
342 does provide: data types that cannot be handled, performance issues
343 and the circumstances in which they may arise, practical
344 limitations on the size of data sets, special cases that are not
347 No bugs have been reported.
349 Please report any bugs or feature requests to
350 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
351 L<http://rt.cpan.org>.
355 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
359 Chris Prather C<< <perigrin@cpan.org> >>
363 Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and the
366 Some bug fixes sponsored by Takkle Inc.
368 =head1 LICENCE AND COPYRIGHT
370 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights
373 This module is free software; you can redistribute it and/or
374 modify it under the same terms as Perl itself. See L<perlartistic>.
377 =head1 DISCLAIMER OF WARRANTY
379 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
380 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
381 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
382 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
383 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
384 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
385 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
386 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
387 NECESSARY SERVICING, REPAIR, OR CORRECTION.
389 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
390 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
391 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
392 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
393 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
394 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
395 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
396 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
397 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF