add Test::MooseX::Daemonize
[gitmo/MooseX-Daemonize.git] / lib / Test / MooseX / Daemonize.pm
CommitLineData
7d7115e5 1package Test::MooseX::Daemonize;
2use strict;
3use Test::More;
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
31 $Test->ok( kill 0 => $pid or $!{EPERM}, $msg );
32 return $pid;
33 }
34}
35
361;
37__END__
38
39
40=head1 NAME
41
42Test::MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
43
44
45=head1 VERSION
46
47This document describes MooseX::Daemonize version 0.0.1
48
49
50=head1 SYNOPSIS
51
52 package main;
53 use Cwd;
54
55 ## Try to make sure we are in the test directory
56 chdir 't' if ( Cwd::cwd() !~ m|/t$| );
57 my $cwd = Cwd::cwd();
58
59 my $file = join( '/', $cwd, 'im_alive' );
60 my $daemon = FileMaker->new( pidbase => '.', filename => $file );
61
62 daemonize_ok( $daemon, 'child forked okay' );
63 ok( -e $file, "$file exists" );
64 unlink($file);
65
66=head1 DESCRIPTION
67
68Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals.
69This module helps provide the basic infrastructure to do that.
70
71=head1 ATTRIBUTES
72
73=over
74
75=item progname Str
76
77The name of our daemon, defaults to $0
78
79=item pidbase Str
80
81The base for our bid, defaults to /var/run/$progname
82
83=item pidfile Str
84
85The file we store our PID in, defaults to /var/run/$progname/
86
87=item foreground Bool
88
89If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.
90
91=back
92
93=head1 METHODS
94
95=over
96
97=item check()
98
99Check to see if an instance is already running.
100
101=item start()
102
103Setup a pidfile, fork, then setup the signal handlers.
104
105=item stop()
106
107Stop the process matching the pidfile, and unlinks the pidfile.
108
109=item restart()
110
111Litterally
112
113 $self->stop();
114 $self->start();
115
116=item daemonize()
117
118Calls C<Proc::Daemon::Init> to daemonize this process.
119
120=item kill($pid)
121
122Kills the process for $pid. This will try SIGINT, and SIGTERM before falling back to SIGKILL and finally giving up.
123
124=item setup_signals()
125
126Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
127
128=item handle_sigint()
129
130Handle a INT signal, by default calls C<$self->stop()>;
131
132=item handle_sighup()
133
134Handle a HUP signal. Nothing is done by default.
135
136=item meta()
137
138the C<meta()> method from L<Class::MOP::Class>
139
140=back
141
142=head1 DEPENDENCIES
143
144=for author to fill in:
145 A list of all the other modules that this module relies upon,
146 including any restrictions on versions, and an indication whether
147 the module is part of the standard Perl distribution, part of the
148 module's distribution, or must be installed separately. ]
149
150Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
151
152=head1 INCOMPATIBILITIES
153
154=for author to fill in:
155 A list of any modules that this module cannot be used in conjunction
156 with. This may be due to name conflicts in the interface, or
157 competition for system or program resources, or due to internal
158 limitations of Perl (for example, many modules that use source code
159 filters are mutually incompatible).
160
161None reported.
162
163
164=head1 BUGS AND LIMITATIONS
165
166=for author to fill in:
167 A list of known problems with the module, together with some
168 indication Whether they are likely to be fixed in an upcoming
169 release. Also a list of restrictions on the features the module
170 does provide: data types that cannot be handled, performance issues
171 and the circumstances in which they may arise, practical
172 limitations on the size of data sets, special cases that are not
173 (yet) handled, etc.
174
175No bugs have been reported.
176
177Please report any bugs or feature requests to
178C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
179L<http://rt.cpan.org>.
180
181=head1 SEE ALSO
182
183L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
184
185=head1 AUTHOR
186
187Chris Prather C<< <perigrin@cpan.org> >>
188
189
190=head1 LICENCE AND COPYRIGHT
191
192Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
193
194This module is free software; you can redistribute it and/or
195modify it under the same terms as Perl itself. See L<perlartistic>.
196
197
198=head1 DISCLAIMER OF WARRANTY
199
200BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
201FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
202OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
203PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
204EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
205WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
206ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
207YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
208NECESSARY SERVICING, REPAIR, OR CORRECTION.
209
210IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
211WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
212REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
213LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
214OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
215THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
216RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
217FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
218SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
219SUCH DAMAGES.