add Test::MooseX::Daemonize
[gitmo/MooseX-Daemonize.git] / lib / Test / MooseX / Daemonize.pm
1 package Test::MooseX::Daemonize;
2 use strict;
3 use Test::More;
4 use Proc::Daemon;
5
6 # BEGIN CARGO CULTING
7 use Sub::Exporter;
8 use Test::Builder;
9 our $VERSION   = '0.01';
10 our $AUTHORITY = 'cpan:PERIGRIN';
11
12 my @exports = qw[
13     daemonize_ok
14 ];
15
16 Sub::Exporter::setup_exporter({
17     exports => \@exports,
18     groups  => { default => \@exports }
19 });
20
21 our $Test = Test::Builder->new;
22
23 sub 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
36 1;
37 __END__
38
39
40 =head1 NAME
41
42 Test::MooseX::Daemonize - provides a Role that daemonizes your Moose based application.
43
44
45 =head1 VERSION
46
47 This 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
68 Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals. 
69 This module helps provide the basic infrastructure to do that.
70
71 =head1 ATTRIBUTES
72
73 =over
74
75 =item progname Str
76
77 The name of our daemon, defaults to $0
78
79 =item pidbase Str
80
81 The base for our bid, defaults to /var/run/$progname
82
83 =item pidfile Str
84
85 The file we store our PID in, defaults to /var/run/$progname/ 
86
87 =item foreground Bool
88
89 If 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
99 Check to see if an instance is already running.
100
101 =item start()
102
103 Setup a pidfile, fork, then setup the signal handlers.
104
105 =item stop()
106
107 Stop the process matching the pidfile, and unlinks the pidfile.
108
109 =item restart()
110
111 Litterally 
112
113     $self->stop();
114     $self->start();
115
116 =item daemonize()
117
118 Calls C<Proc::Daemon::Init> to daemonize this process. 
119
120 =item kill($pid)
121
122 Kills 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
126 Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP
127
128 =item handle_sigint()
129
130 Handle a INT signal, by default calls C<$self->stop()>;
131
132 =item handle_sighup()
133
134 Handle a HUP signal. Nothing is done by default.
135
136 =item meta()
137
138 the 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
150 Obviously 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
161 None 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
175 No bugs have been reported.
176
177 Please report any bugs or feature requests to
178 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
179 L<http://rt.cpan.org>.
180
181 =head1 SEE ALSO
182
183 L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
184
185 =head1 AUTHOR
186
187 Chris Prather  C<< <perigrin@cpan.org> >>
188
189
190 =head1 LICENCE AND COPYRIGHT
191
192 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
193
194 This module is free software; you can redistribute it and/or
195 modify it under the same terms as Perl itself. See L<perlartistic>.
196
197
198 =head1 DISCLAIMER OF WARRANTY
199
200 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
201 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
202 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
203 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
204 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
205 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
206 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
207 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
208 NECESSARY SERVICING, REPAIR, OR CORRECTION.
209
210 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
211 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
212 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
213 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
214 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
215 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
216 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
217 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
218 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
219 SUCH DAMAGES.