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