3 package Test::MooseX::Daemonize;
10 our $AUTHORITY = 'cpan:PERIGRIN';
18 Sub::Exporter::setup_exporter(
21 groups => { default => \@exports }
26 our $Test = Test::Builder->new;
29 my ( $daemon, $msg ) = @_;
30 unless ( my $pid = Proc::Daemon::Fork ) {
35 sleep(1); # Punt on sleep time, 1 seconds should be enough
36 $Test->ok( -e $daemon->pidfile->file, $msg )
38 'Pidfile (' . $daemon->pidfile->file . ') not found.' );
42 sub check_test_output {
44 open( my $stdout_in, '<', $app->test_output )
45 or die "can't open test output: $!";
46 while ( my $line = <$stdout_in> ) {
49 if ( $line =~ /\A((not\s+)?ok)(?:\s+-)(?:\s+(.*))\z/ ) {
50 my ( $status, $not, $text ) = ( $1, $2, $3 );
53 # We don't just call ok(!$not), because that generates diagnostics of
54 # its own for failures. We only want the diagnostics from the child.
55 my $num = $Test->current_test;
56 $Test->current_test( ++$num );
57 $Test->_print("$status $num - $text\n");
59 elsif ( $line =~ s/\A#\s?// ) {
63 $Test->_print_diag("$label: $line (unrecognised)\n");
68 package Test::MooseX::Daemonize::Testable;
77 after daemonize => sub {
78 $Test->use_numbers(0);
80 open my $out, '>', $_[0]->test_output or die "Cannot open test output: $!";
81 my $fileno = fileno $out;
82 open STDERR, ">&=", $fileno
83 or die "Can't redirect STDERR";
85 open STDOUT, ">&=", $fileno
86 or die "Can't redirect STDOUT";
89 $Test->failure_output($out);
90 $Test->todo_output($out);
99 Test::MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
104 This document describes MooseX::Daemonize version 0.0.1
112 ## Try to make sure we are in the test directory
113 chdir 't' if ( Cwd::cwd() !~ m|/t$| );
114 my $cwd = Cwd::cwd();
116 my $file = join( '/', $cwd, 'im_alive' );
117 my $daemon = FileMaker->new( pidbase => '.', filename => $file );
119 daemonize_ok( $daemon, 'child forked okay' );
120 ok( -e $file, "$file exists" );
125 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
126 This module helps provide the basic infrastructure to do that.
134 The name of our daemon, defaults to $0
138 The base for our bid, defaults to /var/run/$progname
142 The file we store our PID in, defaults to /var/run/$progname/
144 =item foreground Bool
146 If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
156 Check to see if an instance is already running.
160 Setup a pidfile, fork, then setup the signal handlers.
164 Stop the process matching the pidfile, and unlinks the pidfile.
175 Calls C<Proc::Daemon::Init> to daemonize this process.
179 Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
181 =item setup_signals()
183 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
185 =item handle_sigint()
187 Handle a INT signal, by default calls C<$self->stop()>;
189 =item handle_sighup()
191 Handle a HUP signal. Nothing is done by default.
195 the C<meta()> method from L<Class::MOP::Class>
199 =item check_test_output()
205 =for author to fill in:
206 A list of all the other modules that this module relies upon,
207 including any restrictions on versions, and an indication whether
208 the module is part of the standard Perl distribution, part of the
209 module's distribution, or must be installed separately. ]
211 Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
213 =head1 INCOMPATIBILITIES
215 =for author to fill in:
216 A list of any modules that this module cannot be used in conjunction
217 with. This may be due to name conflicts in the interface, or
218 competition for system or program resources, or due to internal
219 limitations of Perl (for example, many modules that use source code
220 filters are mutually incompatible).
225 =head1 BUGS AND LIMITATIONS
227 =for author to fill in:
228 A list of known problems with the module, together with some
229 indication Whether they are likely to be fixed in an upcoming
230 release. Also a list of restrictions on the features the module
231 does provide: data types that cannot be handled, performance issues
232 and the circumstances in which they may arise, practical
233 limitations on the size of data sets, special cases that are not
236 No bugs have been reported.
238 Please report any bugs or feature requests to
239 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
240 L<http://rt.cpan.org>.
244 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
248 Chris Prather C<< <perigrin@cpan.org> >>
251 =head1 LICENCE AND COPYRIGHT
253 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
255 This module is free software; you can redistribute it and/or
256 modify it under the same terms as Perl itself. See L<perlartistic>.
259 =head1 DISCLAIMER OF WARRANTY
261 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
262 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
263 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
264 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
265 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
266 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
267 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
268 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
269 NECESSARY SERVICING, REPAIR, OR CORRECTION.
271 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
272 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
273 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
274 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
275 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
276 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
277 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
278 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
279 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF