3 package Test::MooseX::Daemonize;
10 our $AUTHORITY = 'cpan:PERIGRIN';
16 Sub::Exporter::setup_exporter({
18 groups => { default => \@exports }
21 our $Test = Test::Builder->new;
24 my ( $daemon, $msg ) = @_;
25 unless ( my $pid = Proc::Daemon::Fork ) {
30 sleep(5); # Punt on sleep time, 5 seconds should be enough
31 $Test->ok( kill(0 => $pid or $!{EPERM}), $msg );
36 package Test::MooseX::Daemonize::Testable;
45 after daemonize => sub {
46 $Test->use_numbers(0);
48 open my $stdout_out, '>', $_[0]->test_output;
49 my $fileno = fileno $stdout_out;
50 open STDERR, ">&=$fileno"
51 or die "Can't redirect STDERR";
53 open STDOUT, ">&=$fileno"
54 or die "Can't redirect STDOUT";
56 $Test->output($stdout_out);
57 $Test->failure_output($stdout_out);
58 $Test->todo_output($stdout_out);
67 Test::MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
72 This document describes MooseX::Daemonize version 0.0.1
80 ## Try to make sure we are in the test directory
81 chdir 't' if ( Cwd::cwd() !~ m|/t$| );
84 my $file = join( '/', $cwd, 'im_alive' );
85 my $daemon = FileMaker->new( pidbase => '.', filename => $file );
87 daemonize_ok( $daemon, 'child forked okay' );
88 ok( -e $file, "$file exists" );
93 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
94 This module helps provide the basic infrastructure to do that.
102 The name of our daemon, defaults to $0
106 The base for our bid, defaults to /var/run/$progname
110 The file we store our PID in, defaults to /var/run/$progname/
112 =item foreground Bool
114 If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
124 Check to see if an instance is already running.
128 Setup a pidfile, fork, then setup the signal handlers.
132 Stop the process matching the pidfile, and unlinks the pidfile.
143 Calls C<Proc::Daemon::Init> to daemonize this process.
147 Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
149 =item setup_signals()
151 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
153 =item handle_sigint()
155 Handle a INT signal, by default calls C<$self->stop()>;
157 =item handle_sighup()
159 Handle a HUP signal. Nothing is done by default.
163 the C<meta()> method from L<Class::MOP::Class>
169 =for author to fill in:
170 A list of all the other modules that this module relies upon,
171 including any restrictions on versions, and an indication whether
172 the module is part of the standard Perl distribution, part of the
173 module's distribution, or must be installed separately. ]
175 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
177 =head1 INCOMPATIBILITIES
179 =for author to fill in:
180 A list of any modules that this module cannot be used in conjunction
181 with. This may be due to name conflicts in the interface, or
182 competition for system or program resources, or due to internal
183 limitations of Perl (for example, many modules that use source code
184 filters are mutually incompatible).
189 =head1 BUGS AND LIMITATIONS
191 =for author to fill in:
192 A list of known problems with the module, together with some
193 indication Whether they are likely to be fixed in an upcoming
194 release. Also a list of restrictions on the features the module
195 does provide: data types that cannot be handled, performance issues
196 and the circumstances in which they may arise, practical
197 limitations on the size of data sets, special cases that are not
200 No bugs have been reported.
202 Please report any bugs or feature requests to
203 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
204 L<http://rt.cpan.org>.
208 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
212 Chris Prather C<< <perigrin@cpan.org> >>
215 =head1 LICENCE AND COPYRIGHT
217 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
219 This module is free software; you can redistribute it and/or
220 modify it under the same terms as Perl itself. See L<perlartistic>.
223 =head1 DISCLAIMER OF WARRANTY
225 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
226 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
227 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
228 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
229 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
230 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
231 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
232 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
233 NECESSARY SERVICING, REPAIR, OR CORRECTION.
235 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
236 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
237 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
238 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
239 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
240 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
241 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
242 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
243 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF