1 package MooseX::Daemonize;
2 use strict; # because Kwalitee is pedantic
4 use MooseX::Types::Path::Class;
5 use Moose::Util::TypeConstraints;
11 use MooseX::Daemonize::PidFile;
14 MooseX::Daemonize::Core
15 MooseX::Daemonize::SignalHandling
25 ( my $name = lc $_[0]->meta->name ) =~ s/::/_/g;
31 isa => 'Path::Class::Dir',
36 default => sub { Path::Class::Dir->new('/') },
40 isa => 'Path::Class::Dir',
45 default => sub { Path::Class::Dir->new('var', 'run') },
48 coerce 'MooseX::Daemonize::PidFile'
50 => via { MooseX::Daemonize::PidFile->new( file => $_ ) };
53 isa => 'MooseX::Daemonize::PidFile',
58 predicate => 'has_pidfile',
60 my $file = $_[0]->pidbase . '/' . $_[0]->progname . '.pid';
61 confess "Cannot write to $file" unless (-e $file ? -w $file : -w $_[0]->pidbase);
62 MooseX::Daemonize::PidFile->new( file => $file );
67 remove_pid => 'remove',
73 metaclass => 'Getopt',
90 confess "instance already running" if $self->check;
92 $self->daemonize unless $self->foreground;
94 return unless $self->is_daemon;
96 $self->pidfile->pid($$);
98 # Avoid 'stdin reopened for output' warning with newer perls
100 open( NULL, '/dev/null' );
105 chdir $self->basedir;
108 $self->setup_signals;
112 # Make _kill *really* private
116 my ( $self, %args ) = @_;
117 my $pid = $self->get_pid;
118 $self->$_kill($pid) unless $self->foreground();
120 return 1 if $args{no_exit};
126 $self->stop( no_exit => 1 );
131 my ($self, $signal) = @_;
132 return $self->handle_sigint if $signal eq 'INT';
133 return $self->handle_sighup if $signal eq 'HUP';
136 sub handle_sigint { $_[0]->stop; }
137 sub handle_sighup { $_[0]->restart; }
140 my ( $self, $pid ) = @_;
142 unless ( CORE::kill 0 => $pid ) {
144 # warn "$pid already appears dead.";
150 # warn "$pid is us! Can't commit suicide.";
154 my $timeout = $self->stop_timeout;
156 # kill 0 => $pid returns 0 if the process is dead
157 # $!{EPERM} could also be true if we cant kill it (permission error)
159 # Try SIGINT ... 2s ... SIGTERM ... 2s ... SIGKILL ... 3s ... UNDEAD!
160 for ( [ 2, $timeout ], [15, $timeout], [9, $timeout * 1.5] ) {
161 my ($signal, $timeout) = @$_;
162 $timeout = int $timeout;
164 CORE::kill($signal, $pid);
166 last unless CORE::kill 0 => $pid or $!{EPERM};
170 last unless CORE::kill 0 => $pid or $!{EPERM};
175 return unless ( CORE::kill 0 => $pid or $!{EPERM} );
177 # IF it is still running
178 carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD!
186 MooseX::Daemonize - provides a Role that daemonizes your Moose based
191 This document describes MooseX::Daemonize version 0.04
197 with qw(MooseX::Daemonize);
200 my ( $self, $file ) = @_;
201 open( FILE, ">$file" ) || die;
207 # then in the main package ...
209 my $daemon = FileMaker->new();
211 $daemon->create_file($file);
216 Often you want to write a persistant daemon that has a pid file, and responds
217 appropriately to Signals. This module helps provide the basic infrastructure
224 =item progname Path::Class::Dir | Str
226 The name of our daemon, defaults to $self->meta->name =~ s/::/_/;
228 =item pidbase Path::Class::Dir | Str
230 The base for our bid, defaults to /var/run/$progname
232 =item pidfile MooseX::Daemonize::PidFile | Str
234 The file we store our PID in, defaults to /var/run/$progname
236 =item foreground Bool
238 If true, the process won't background. Useful for debugging. This option can
239 be set via Getopt's -f.
243 If true, the process is the backgrounded process. This is useful for example
244 in an after 'start' => sub { } block
248 Number of seconds to wait for the process to stop, before trying harder to kill
249 it. Defaults to 2 seconds
259 Check to see if an instance is already running.
263 Setup a pidfile, fork, then setup the signal handlers.
267 Stop the process matching the pidfile, and unlinks the pidfile.
278 Calls C<Proc::Daemon::Init> to daemonize this process.
280 =item setup_signals()
282 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
284 =item handle_sigint()
286 Handle a INT signal, by default calls C<$self->stop()>
288 =item handle_sighup()
290 Handle a HUP signal. By default calls C<$self->restart()>
294 Lookup the pid from our pidfile.
298 Save the current pid in our pidfile
306 The C<meta()> method from L<Class::MOP::Class>
312 =for author to fill in:
313 A list of all the other modules that this module relies upon,
314 including any restrictions on versions, and an indication whether
315 the module is part of the standard Perl distribution, part of the
316 module's distribution, or must be installed separately. ]
318 Obviously L<Moose>, and L<Proc::Daemon>
320 =head1 INCOMPATIBILITIES
322 =for author to fill in:
323 A list of any modules that this module cannot be used in conjunction
324 with. This may be due to name conflicts in the interface, or
325 competition for system or program resources, or due to internal
326 limitations of Perl (for example, many modules that use source code
327 filters are mutually incompatible).
332 =head1 BUGS AND LIMITATIONS
334 =for author to fill in:
335 A list of known problems with the module, together with some
336 indication Whether they are likely to be fixed in an upcoming
337 release. Also a list of restrictions on the features the module
338 does provide: data types that cannot be handled, performance issues
339 and the circumstances in which they may arise, practical
340 limitations on the size of data sets, special cases that are not
343 No bugs have been reported.
345 Please report any bugs or feature requests to
346 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
347 L<http://rt.cpan.org>.
351 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
355 Chris Prather C<< <perigrin@cpan.org> >>
359 Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and the
362 Some bug fixes sponsored by Takkle Inc.
364 =head1 LICENCE AND COPYRIGHT
366 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights
369 This module is free software; you can redistribute it and/or
370 modify it under the same terms as Perl itself. See L<perlartistic>.
373 =head1 DISCLAIMER OF WARRANTY
375 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
376 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
377 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
378 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
379 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
380 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
381 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
382 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
383 NECESSARY SERVICING, REPAIR, OR CORRECTION.
385 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
386 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
387 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
388 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
389 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
390 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
391 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
392 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
393 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF