Adding Cogwheel, a Moose wrapped Sprocket
[gitmo/MooseX-Daemonize.git] / lib / Test / MooseX / Daemonize.pm
CommitLineData
7d7115e5 1use strict;
3c3db18c 2
3package Test::MooseX::Daemonize;
7d7115e5 4use Proc::Daemon;
5
6# BEGIN CARGO CULTING
7use Sub::Exporter;
8use Test::Builder;
9our $VERSION = '0.01';
10our $AUTHORITY = 'cpan:PERIGRIN';
11
12my @exports = qw[
13 daemonize_ok
14];
15
16Sub::Exporter::setup_exporter({
17 exports => \@exports,
18 groups => { default => \@exports }
19});
20
21our $Test = Test::Builder->new;
22
23sub daemonize_ok {
24 my ( $daemon, $msg ) = @_;
25 unless ( my $pid = Proc::Daemon::Fork ) {
26 $daemon->start();
27 exit;
28 }
29 else {
30 sleep(5); # Punt on sleep time, 5 seconds should be enough
3c3db18c 31 $Test->ok( kill(0 => $pid or $!{EPERM}), $msg );
7d7115e5 32 return $pid;
33 }
34}
35
3c3db18c 36package Test::MooseX::Daemonize::Testable;
37use Moose::Role;
38
39has test_output => (
40 isa => 'Str',
41 is => 'ro',
42 required => 1,
43);
44
45after daemonize => sub {
46 $Test->use_numbers(0);
47 $Test->no_ending(1);
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";
52
53 open STDOUT, ">&=$fileno"
54 or die "Can't redirect STDOUT";
55
56 $Test->output($stdout_out);
57 $Test->failure_output($stdout_out);
58 $Test->todo_output($stdout_out);
59};
60
7d7115e5 611;
62__END__
63
64
65=head1 NAME
66
67Test::MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
68
69
70=head1 VERSION
71
72This document describes MooseX::Daemonize version 0.0.1
73
74
75=head1 SYNOPSIS
76
77 package main;
78 use Cwd;
79
80 ## Try to make sure we are in the test directory
81 chdir 't' if ( Cwd::cwd() !~ m|/t$| );
82 my $cwd = Cwd::cwd();
83
84 my $file = join( '/', $cwd, 'im_alive' );
85 my $daemon = FileMaker->new( pidbase => '.', filename => $file );
86
87 daemonize_ok( $daemon, 'child forked okay' );
88 ok( -e $file, "$file exists" );
89 unlink($file);
90
91=head1 DESCRIPTION
92
93Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
94This module helps provide the basic infrastructure to do that.
95
96=head1 ATTRIBUTES
97
98=over
99
100=item progname Str
101
102The name of our daemon, defaults to $0
103
104=item pidbase Str
105
106The base for our bid, defaults to /var/run/$progname
107
108=item pidfile Str
109
110The file we store our PID in, defaults to /var/run/$progname/
111
112=item foreground Bool
113
114If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
115
116=back
117
118=head1 METHODS
119
120=over
121
122=item check()
123
124Check to see if an instance is already running.
125
126=item start()
127
128Setup a pidfile, fork, then setup the signal handlers.
129
130=item stop()
131
132Stop the process matching the pidfile, and unlinks the pidfile.
133
134=item restart()
135
136Litterally
137
138 $self->stop();
139 $self->start();
140
141=item daemonize()
142
143Calls C<Proc::Daemon::Init> to daemonize this process.
144
145=item kill($pid)
146
147Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
148
149=item setup_signals()
150
151Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
152
153=item handle_sigint()
154
155Handle a INT signal, by default calls C<$self->stop()>;
156
157=item handle_sighup()
158
159Handle a HUP signal. Nothing is done by default.
160
161=item meta()
162
163the C<meta()> method from L<Class::MOP::Class>
164
165=back
166
167=head1 DEPENDENCIES
168
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. ]
174
175Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
176
177=head1 INCOMPATIBILITIES
178
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).
185
186None reported.
187
188
189=head1 BUGS AND LIMITATIONS
190
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
198 (yet) handled, etc.
199
200No bugs have been reported.
201
202Please report any bugs or feature requests to
203C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
204L<http://rt.cpan.org>.
205
206=head1 SEE ALSO
207
208L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
209
210=head1 AUTHOR
211
212Chris Prather C<< <perigrin@cpan.org> >>
213
214
215=head1 LICENCE AND COPYRIGHT
216
217Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
218
219This module is free software; you can redistribute it and/or
220modify it under the same terms as Perl itself. See L<perlartistic>.
221
222
223=head1 DISCLAIMER OF WARRANTY
224
225BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
226FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
227OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
228PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
229EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
230WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
231ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
232YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
233NECESSARY SERVICING, REPAIR, OR CORRECTION.
234
235IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
236WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
237REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
238LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
239OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
240THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
241RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
242FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
243SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
244SUCH DAMAGES.