Commit | Line | Data |
7d7115e5 |
1 | use strict; |
3c3db18c |
2 | |
3 | package Test::MooseX::Daemonize; |
7d7115e5 |
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 |
3c3db18c |
31 | $Test->ok( kill(0 => $pid or $!{EPERM}), $msg ); |
7d7115e5 |
32 | return $pid; |
33 | } |
34 | } |
35 | |
3c3db18c |
36 | package Test::MooseX::Daemonize::Testable; |
37 | use Moose::Role; |
38 | |
39 | has test_output => ( |
40 | isa => 'Str', |
41 | is => 'ro', |
42 | required => 1, |
43 | ); |
44 | |
45 | after 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 |
61 | 1; |
62 | __END__ |
63 | |
64 | |
65 | =head1 NAME |
66 | |
67 | Test::MooseX::Daemonize - provides a Role that daemonizes your Moose based application. |
68 | |
69 | |
70 | =head1 VERSION |
71 | |
72 | This 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 | |
93 | Often you want to write a persistant daemon that has a pid file, and responds appropriately to Signals. |
94 | This module helps provide the basic infrastructure to do that. |
95 | |
96 | =head1 ATTRIBUTES |
97 | |
98 | =over |
99 | |
100 | =item progname Str |
101 | |
102 | The name of our daemon, defaults to $0 |
103 | |
104 | =item pidbase Str |
105 | |
106 | The base for our bid, defaults to /var/run/$progname |
107 | |
108 | =item pidfile Str |
109 | |
110 | The file we store our PID in, defaults to /var/run/$progname/ |
111 | |
112 | =item foreground Bool |
113 | |
114 | If 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 | |
124 | Check to see if an instance is already running. |
125 | |
126 | =item start() |
127 | |
128 | Setup a pidfile, fork, then setup the signal handlers. |
129 | |
130 | =item stop() |
131 | |
132 | Stop the process matching the pidfile, and unlinks the pidfile. |
133 | |
134 | =item restart() |
135 | |
136 | Litterally |
137 | |
138 | $self->stop(); |
139 | $self->start(); |
140 | |
141 | =item daemonize() |
142 | |
143 | Calls C<Proc::Daemon::Init> to daemonize this process. |
144 | |
145 | =item kill($pid) |
146 | |
147 | Kills 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 | |
151 | Setup the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP |
152 | |
153 | =item handle_sigint() |
154 | |
155 | Handle a INT signal, by default calls C<$self->stop()>; |
156 | |
157 | =item handle_sighup() |
158 | |
159 | Handle a HUP signal. Nothing is done by default. |
160 | |
161 | =item meta() |
162 | |
163 | the 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 | |
175 | Obviously 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 | |
186 | None 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 | |
200 | No bugs have been reported. |
201 | |
202 | Please report any bugs or feature requests to |
203 | C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at |
204 | L<http://rt.cpan.org>. |
205 | |
206 | =head1 SEE ALSO |
207 | |
208 | L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt> |
209 | |
210 | =head1 AUTHOR |
211 | |
212 | Chris Prather C<< <perigrin@cpan.org> >> |
213 | |
214 | |
215 | =head1 LICENCE AND COPYRIGHT |
216 | |
217 | Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved. |
218 | |
219 | This module is free software; you can redistribute it and/or |
220 | modify it under the same terms as Perl itself. See L<perlartistic>. |
221 | |
222 | |
223 | =head1 DISCLAIMER OF WARRANTY |
224 | |
225 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
226 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
227 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
228 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
229 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
230 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
231 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
232 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
233 | NECESSARY SERVICING, REPAIR, OR CORRECTION. |
234 | |
235 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
236 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
237 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE |
238 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
239 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
240 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
241 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
242 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
243 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
244 | SUCH DAMAGES. |