Commit | Line | Data |
a4952679 |
1 | package MooseX::Daemonize; |
a392fa53 |
2 | use strict; # because Kwalitee is pedantic |
a4952679 |
3 | use Moose::Role; |
4 | |
5 | our $VERSION = 0.01; |
6 | use Carp; |
7 | use Proc::Daemon; |
a392fa53 |
8 | |
a4952679 |
9 | use File::Flock; |
10 | use File::Slurp; |
11 | |
12 | with qw(MooseX::Getopt); |
13 | |
14 | has progname => ( |
a392fa53 |
15 | isa => 'Str', |
16 | is => 'ro', |
17 | lazy => 1, |
18 | required => 1, |
19 | default => sub { |
20 | ( my $name = lc $_[0]->meta->name ) =~ s/::/_/g; |
21 | return $name; |
22 | }, |
a4952679 |
23 | ); |
24 | |
24a6a660 |
25 | has basedir => ( |
26 | isa => 'Str', |
27 | is => 'ro', |
28 | lazy => 1, |
29 | default => sub { return '/' }, |
30 | ); |
31 | |
a4952679 |
32 | has pidbase => ( |
2361a590 |
33 | isa => 'Str', |
34 | is => 'ro', |
35 | |
36 | # required => 1, |
37 | lazy => 1, |
38 | default => sub { return '/var/run' }, |
a4952679 |
39 | ); |
40 | |
41 | has pidfile => ( |
42 | isa => 'Str', |
43 | is => 'ro', |
44 | lazy => 1, |
45 | required => 1, |
3543c999 |
46 | default => sub { |
47 | die 'Cannot write to ' . $_[0]->pidbase unless -w $_[0]->pidbase; |
48 | $_[0]->pidbase . '/' . $_[0]->progname . '.pid'; |
49 | }, |
a4952679 |
50 | ); |
51 | |
52 | has foreground => ( |
53 | metaclass => 'Getopt', |
54 | cmd_aliases => ['f'], |
55 | isa => 'Bool', |
56 | is => 'ro', |
57 | default => sub { 0 }, |
58 | ); |
59 | |
60 | sub check { |
61 | my ($self) = @_; |
3543c999 |
62 | if ( my $pid = $self->get_pid ) { |
a4952679 |
63 | my $prog = $self->progname; |
3543c999 |
64 | if ( CORE::kill 0 => $pid ) { |
a392fa53 |
65 | croak "$prog already running ($pid)."; |
a4952679 |
66 | } |
ee08a7fe |
67 | carp "$prog not running but found pid ($pid)." |
68 | . "Perhaps the pid file (@{ [$self->pidfile] }) is stale?"; |
a4952679 |
69 | return 1; |
70 | } |
71 | return 0; |
72 | } |
73 | |
a392fa53 |
74 | sub daemonize { |
75 | my ($self) = @_; |
76 | Proc::Daemon::Init; |
77 | } |
78 | |
a4952679 |
79 | sub start { |
80 | my ($self) = @_; |
81 | return if $self->check; |
82 | |
83 | $self->daemonize unless $self->foreground; |
84 | |
3543c999 |
85 | # Avoid 'stdin reopened for output' warning with newer perls |
0585edbb |
86 | ## no critic |
3543c999 |
87 | open( NULL, '/dev/null' ); |
88 | <NULL> if (0); |
0585edbb |
89 | ## use critic |
90 | |
24a6a660 |
91 | # Change to basedir |
92 | chdir $self->basedir; |
93 | |
3543c999 |
94 | $self->save_pid; |
95 | $self->setup_signals; |
96 | return $$; |
97 | } |
98 | |
99 | sub save_pid { |
100 | my ($self) = @_; |
a4952679 |
101 | my $pidfile = $self->pidfile; |
102 | lock( $pidfile, undef, 'nonblocking' ) |
103 | or croak "Could not lock PID file $pidfile: $!"; |
104 | write_file( $pidfile, "$$\n" ); |
a392fa53 |
105 | unlock($pidfile); |
a4952679 |
106 | return; |
107 | } |
108 | |
3543c999 |
109 | sub remove_pid { |
a4952679 |
110 | my ($self) = @_; |
111 | my $pidfile = $self->pidfile; |
a4952679 |
112 | lock( $pidfile, undef, 'nonblocking' ) |
113 | or croak "Could not lock PID file $pidfile: $!"; |
a4952679 |
114 | unlink($pidfile); |
a392fa53 |
115 | unlock($pidfile); |
a4952679 |
116 | return; |
117 | } |
118 | |
3543c999 |
119 | sub get_pid { |
120 | my ($self) = @_; |
121 | my $pidfile = $self->pidfile; |
122 | return unless -e $pidfile; |
123 | chomp( my $pid = read_file($pidfile) ); |
124 | return $pid; |
125 | } |
126 | |
127 | sub stop { |
128 | my ( $self, %args ) = @_; |
129 | my $pid = $self->get_pid; |
cecbee2d |
130 | $self->_kill($pid) unless $self->foreground(); |
3543c999 |
131 | $self->remove_pid; |
132 | return 1 if $args{no_exit}; |
133 | exit; |
134 | } |
135 | |
a4952679 |
136 | sub restart { |
137 | my ($self) = @_; |
3543c999 |
138 | $self->stop( noexit => 1 ); |
a4952679 |
139 | $self->start(); |
140 | } |
141 | |
a4952679 |
142 | sub setup_signals { |
3543c999 |
143 | my ($self) = @_; |
144 | $SIG{INT} = sub { $self->handle_sigint; }; |
145 | $SIG{HUP} = sub { $self->handle_sighup }; |
a4952679 |
146 | } |
147 | |
2361a590 |
148 | sub handle_sigint { $_[0]->stop; } |
149 | sub handle_sighup { $_[0]->restart; } |
a4952679 |
150 | |
cecbee2d |
151 | sub _kill { |
152 | confess "_kill isn't public" unless caller eq __PACKAGE__; |
a4952679 |
153 | my ( $self, $pid ) = @_; |
2361a590 |
154 | return unless $pid; |
3543c999 |
155 | unless ( CORE::kill 0 => $pid ) { |
156 | |
157 | # warn "$pid already appears dead."; |
158 | return; |
159 | } |
160 | |
161 | if ( $pid eq $$ ) { |
162 | |
163 | # warn "$pid is us! Can't commit suicied."; |
a4952679 |
164 | return; |
165 | } |
166 | |
3543c999 |
167 | CORE::kill( 2, $pid ); # Try SIGINT |
2361a590 |
168 | sleep(2) if CORE::kill( 0, $pid ); |
a4952679 |
169 | |
3543c999 |
170 | unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running |
171 | CORE::kill( 15, $pid ); # try SIGTERM |
2361a590 |
172 | sleep(2) if CORE::kill( 0, $pid ); |
a4952679 |
173 | } |
174 | |
3543c999 |
175 | unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running |
176 | CORE::kill( 9, $pid ); # finally try SIGKILL |
2361a590 |
177 | sleep(2) if CORE::kill( 0, $pid ); |
a4952679 |
178 | } |
179 | |
3543c999 |
180 | unless ( CORE::kill 0 => $pid or $!{EPERM} ) { # IF it is still running |
181 | carp "$pid doesn't seem to want to die."; # AHH EVIL DEAD! |
a4952679 |
182 | } |
183 | |
184 | return; |
185 | } |
186 | |
187 | 1; |
188 | __END__ |
189 | |
a4952679 |
190 | =head1 NAME |
191 | |
192 | MooseX::Daemonize - provides a Role that daemonizes your Moose based application. |
193 | |
194 | |
195 | =head1 VERSION |
196 | |
197 | This document describes MooseX::Daemonize version 0.0.1 |
198 | |
199 | |
200 | =head1 SYNOPSIS |
201 | |
202 | package FileMaker; |
203 | use Moose; |
204 | with qw(MooseX::Daemonize); |
205 | |
206 | sub create_file { |
207 | my ( $self, $file ) = @_; |
208 | open( FILE, ">$file" ) || die; |
209 | close(FILE); |
210 | } |
211 | |
212 | no Moose; |
213 | |
214 | # then in the main package ... |
215 | |
216 | my $daemon = FileMaker->new(); |
217 | $daemon->start(); |
218 | $daemon->create_file($file); |
219 | $daemon->stop(); |
220 | |
221 | =head1 DESCRIPTION |
222 | |
223 | Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals. |
224 | This module helps provide the basic infrastructure to do that. |
225 | |
226 | =head1 ATTRIBUTES |
227 | |
228 | =over |
229 | |
230 | =item progname Str |
231 | |
232 | The name of our daemon, defaults to $0 |
233 | |
234 | =item pidbase Str |
235 | |
236 | The base for our bid, defaults to /var/run/$progname |
237 | |
238 | =item pidfile Str |
239 | |
240 | The file we store our PID in, defaults to /var/run/$progname/ |
241 | |
242 | =item foreground Bool |
243 | |
244 | If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f. |
245 | |
246 | =back |
247 | |
248 | =head1 METHODS |
249 | |
250 | =over |
251 | |
252 | =item check() |
253 | |
254 | Check to see if an instance is already running. |
255 | |
256 | =item start() |
257 | |
258 | Setup a pidfile, fork, then setup the signal handlers. |
259 | |
260 | =item stop() |
261 | |
262 | Stop the process matching the pidfile, and unlinks the pidfile. |
263 | |
264 | =item restart() |
265 | |
266 | Litterally |
267 | |
268 | $self->stop(); |
269 | $self->start(); |
270 | |
271 | =item daemonize() |
272 | |
273 | Calls C<Proc::Daemon::Init> to daemonize this process. |
274 | |
275 | =item kill($pid) |
276 | |
277 | Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up. |
278 | |
279 | =item setup_signals() |
280 | |
281 | Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP |
282 | |
283 | =item handle_sigint() |
284 | |
cecbee2d |
285 | Handle a INT signal, by default calls C<$self->stop()> |
a4952679 |
286 | |
287 | =item handle_sighup() |
288 | |
cecbee2d |
289 | Handle a HUP signal. By default calls C<$self->restart()> |
a4952679 |
290 | |
3543c999 |
291 | =item get_pid |
292 | |
cecbee2d |
293 | Lookup the pid from our pidfile. |
294 | |
3543c999 |
295 | =item save_pid |
296 | |
cecbee2d |
297 | Save the current pid in our pidfile |
298 | |
3543c999 |
299 | =item remove_pid |
300 | |
cecbee2d |
301 | Delete our pidfile |
302 | |
a4952679 |
303 | =item meta() |
304 | |
cecbee2d |
305 | The C<meta()> method from L<Class::MOP::Class> |
a4952679 |
306 | |
307 | =back |
308 | |
309 | =head1 DEPENDENCIES |
310 | |
311 | =for author to fill in: |
312 | A list of all the other modules that this module relies upon, |
313 | including any restrictions on versions, and an indication whether |
314 | the module is part of the standard Perl distribution, part of the |
315 | module's distribution, or must be installed separately. ] |
316 | |
317 | Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp> |
318 | |
319 | =head1 INCOMPATIBILITIES |
320 | |
321 | =for author to fill in: |
322 | A list of any modules that this module cannot be used in conjunction |
323 | with. This may be due to name conflicts in the interface, or |
324 | competition for system or program resources, or due to internal |
325 | limitations of Perl (for example, many modules that use source code |
326 | filters are mutually incompatible). |
327 | |
328 | None reported. |
329 | |
330 | |
331 | =head1 BUGS AND LIMITATIONS |
332 | |
333 | =for author to fill in: |
334 | A list of known problems with the module, together with some |
335 | indication Whether they are likely to be fixed in an upcoming |
336 | release. Also a list of restrictions on the features the module |
337 | does provide: data types that cannot be handled, performance issues |
338 | and the circumstances in which they may arise, practical |
339 | limitations on the size of data sets, special cases that are not |
340 | (yet) handled, etc. |
341 | |
342 | No bugs have been reported. |
343 | |
344 | Please report any bugs or feature requests to |
345 | C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at |
346 | L<http://rt.cpan.org>. |
347 | |
348 | =head1 SEE ALSO |
349 | |
350 | L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt> |
351 | |
352 | =head1 AUTHOR |
353 | |
354 | Chris Prather C<< <perigrin@cpan.org> >> |
355 | |
356 | |
357 | =head1 LICENCE AND COPYRIGHT |
358 | |
359 | Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved. |
360 | |
361 | This module is free software; you can redistribute it and/or |
362 | modify it under the same terms as Perl itself. See L<perlartistic>. |
363 | |
364 | |
365 | =head1 DISCLAIMER OF WARRANTY |
366 | |
367 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
368 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
369 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
370 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
371 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
372 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
373 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
374 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
375 | NECESSARY SERVICING, REPAIR, OR CORRECTION. |
376 | |
377 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
378 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
379 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE |
380 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
381 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
382 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
383 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
384 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
385 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
386 | SUCH DAMAGES. |