1MooseX::Daemonize(3) User Contributed Perl Documentation MooseX::Daemonize(3)
2
3
4
6 MooseX::Daemonize - Role for daemonizing your Moose based application
7
9 This document describes MooseX::Daemonize version 0.05
10
12 package My::Daemon;
13 use Moose;
14
15 with qw(MooseX::Daemonize);
16
17 # ... define your class ....
18
19 after start => sub {
20 my $self = shift;
21 return unless $self->is_daemon;
22 # your daemon code here ...
23 };
24
25 # then in your script ...
26
27 my $daemon = My::Daemon->new_with_options();
28
29 my ($command) = @{$daemon->extra_argv}
30 defined $command || die "No command specified";
31
32 $daemon->start if $command eq 'start';
33 $daemon->status if $command eq 'status';
34 $daemon->restart if $command eq 'restart';
35 $daemon->stop if $command eq 'stop';
36
37 warn($daemon->status_message);
38 exit($daemon->exit_code);
39
41 Often you want to write a persistant daemon that has a pid file, and
42 responds appropriately to Signals. This module provides a set of basic
43 roles as an infrastructure to do that.
44
46 This list includes attributes brought in from other roles as well we
47 include them here for ease of documentation. All of these attributes
48 are settable though MooseX::Getopt's command line handling, with the
49 exception of "is_daemon".
50
51 progname Path::Class::Dir | Str
52 The name of our daemon, defaults to "$package_name =~ s/::/_/";
53
54 pidbase Path::Class::Dir | Str
55 The base for our bid, defaults to "/var/run/$progname"
56
57 pidfile MooseX::Daemonize::Pid::File | Str
58 The file we store our PID in, defaults to "/var/run/$progname"
59
60 foreground Bool
61 If true, the process won't background. Useful for debugging. This
62 option can be set via Getopt's -f.
63
64 is_daemon Bool
65 If true, the process is the backgrounded daemon process, if false
66 it is the parent process. This is useful for example in an "after
67 'start' =" sub { }> block.
68
69 NOTE: This option is explicitly not available through
70 MooseX::Getopt.
71
72 stop_timeout
73 Number of seconds to wait for the process to stop, before trying
74 harder to kill it. Defaults to 2 seconds.
75
76 These are the internal attributes, which are not available through
77 MooseX::Getopt.
78
79 exit_code Int
80 status_message Str
81
83 Daemon Control Methods
84 These methods can be used to control the daemon behavior. Every effort
85 has been made to have these methods DWIM (Do What I Mean), so that you
86 can focus on just writing the code for your daemon.
87
88 Extending these methods is best done with the Moose method modifiers,
89 such as "before", "after" and "around".
90
91 start
92 Setup a pidfile, fork, then setup the signal handlers.
93
94 stop
95 Stop the process matching the pidfile, and unlinks the pidfile.
96
97 restart
98 Literally this is:
99
100 $self->stop();
101 $self->start();
102
103 status
104 shutdown
105
106 Pidfile Handling Methods
107 init_pidfile
108 This method will create a MooseX::Daemonize::Pid::File object and
109 tell it to store the PID in the file "$pidbase/$progname.pid".
110
111 check
112 This checks to see if the daemon process is currently running by
113 checking the pidfile.
114
115 get_pid
116 Returns the PID of the daemon process.
117
118 save_pid
119 Write the pidfile.
120
121 remove_pid
122 Removes the pidfile.
123
124 Signal Handling Methods
125 setup_signals
126 Setup the signal handlers, by default it only sets up handlers for
127 SIGINT and SIGHUP. If you wish to add more signals just use the
128 "after" method modifier and add them.
129
130 handle_sigint
131 Handle a INT signal, by default calls "$self-"stop()>
132
133 handle_sighup
134 Handle a HUP signal. By default calls "$self-"restart()>
135
136 Exit Code Methods
137 These are overriable constant methods used for setting the exit code.
138
139 OK Returns 0.
140
141 ERROR
142 Returns 1.
143
144 Introspection
145 meta()
146 The "meta()" method from Class::MOP::Class
147
149 Moose, MooseX::Getopt, MooseX::Types::Path::Class and POSIX
150
152 None reported. Although obviously this will not work on Windows.
153
155 No bugs have been reported.
156
157 Please report any bugs or feature requests to
158 "bug-acme-dahut-call@rt.cpan.org", or through the web interface at
159 <http://rt.cpan.org>.
160
162 Proc::Daemon, Daemon::Generic
163
165 Chris Prather "<chris@prather.org"
166
167 Stevan Little "<stevan.little@iinteractive.com>"
168
170 Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and
171 the #moose denzians
172
173 Some bug fixes sponsored by Takkle Inc.
174
176 Copyright (c) 2007-2009, Chris Prather "<chris@prather.org>". Some
177 rights reserved.
178
179 This module is free software; you can redistribute it and/or modify it
180 under the same terms as Perl itself. See perlartistic.
181
183 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
184 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
185 WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
186 PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
187 EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
188 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
189 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
190 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
191 NECESSARY SERVICING, REPAIR, OR CORRECTION.
192
193 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
194 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
195 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
196 TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
197 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
198 SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
199 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
200 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
201 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
202 DAMAGES.
203
204
205
206perl v5.12.0 2009-10-05 MooseX::Daemonize(3)