bf1bcf68d339ad975f6edfcd90a4a3fca7d0a821
[gitmo/MooseX-Daemonize.git] / lib / MooseX / Daemonize / WithPidFile.pm
1 package MooseX::Daemonize::WithPidFile;
2 use strict;         # cause Perl::Critic errors are annoying
3 use MooseX::Getopt; # to load the Getopt metaclass
4 use Moose::Role;
5
6
7 use MooseX::Daemonize::Pid::File;
8
9 with 'MooseX::Daemonize::Core';
10
11 requires 'init_pidfile';
12
13 has pidfile => (
14     # NOTE:
15     # this should always be accessible
16     # from the command line IMO
17     # - SL
18     metaclass => 'Getopt',
19     isa       => 'MooseX::Daemonize::Pid::File',
20     is        => 'rw',
21     lazy      => 1,
22     coerce    => 1,
23     predicate => 'has_pidfile',
24     builder   => 'init_pidfile',
25 );
26
27 after 'daemonize' => sub {
28     my $self = shift;
29     # NOTE:
30     # make sure that we do not have
31     # any bad PID values stashed around
32     # - SL
33     $self->pidfile->clear_pid;
34     if ($self->is_daemon) {
35         $self->pidfile->write;
36     }
37 };
38
39 1;
40
41 __END__
42
43 =pod
44
45 =head1 NAME
46
47 MooseX::Daemonize::WithPidFile - A Role with the core daemonization and pidfile management
48
49 =head1 SYNOPSIS
50
51   package My::Daemon;
52   use Moose;
53
54   with 'MooseX::Daemonize::WithPidFile';
55
56   sub start {
57       my $self = shift;
58       # daemonize me ...
59       $self->daemonize; # << this will write the pidfile for you
60       # return from the parent,...
61       return unless $self->is_daemon;
62       # but continue on in the child (daemon)
63   }
64
65 =head1 DESCRIPTION
66
67 This is a slightly extended basic daemonization Role, it provides
68 Pidfile management along with the core daemonization features
69 found in L<MooseX::Daemonize::Core>.
70
71 =head1 ATTRIBUTES
72
73 =over
74
75 =item I<pidfile (is => rw, isa => MooseX::Daemonize::Pid::File)>
76
77 This attribute holds the L<MooseX::Daemonize::Pid::File> object used
78 to manage the Pidfile. It will initialize the object using the
79 C<init_pidfile> method (which is required by this role).
80
81 =back
82
83 =head1 REQUIRED METHODS
84
85 =over 4
86
87 =item I<init_pidfile>
88
89 This method is used to build the I<pidfile> attribute's object. It should
90 return a L<MooseX::Daemonize::Pid::File> object.
91
92 =item B<has_pidfile>
93
94 This is a predicate method to tell you if your I<pidfile> attribute has
95 been initialized yet.
96
97 =back
98
99 =head1 METHODS
100
101 =over 4
102
103 =item B<daemonize>
104
105 This adds an C<after> method modifier to the C<daemonize> method (from
106 L<MooseX::Daemonize::Core>) and handles writing your Pidfile for you.
107
108 =item B<meta>
109
110 The C<meta()> method from L<Class::MOP::Class>
111
112 =back
113
114 =head1 DEPENDENCIES
115
116 L<Moose::Role>, L<MooseX::Getopt> and L<MooseX::Daemonize::Pid::File>
117
118 =head1 INCOMPATIBILITIES
119
120 None reported.
121
122 =head1 BUGS AND LIMITATIONS
123
124 No bugs have been reported.
125
126 Please report any bugs or feature requests to
127 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
128 L<http://rt.cpan.org>.
129
130 =head1 AUTHOR
131
132 Stevan Little  C<< <stevan.little@iinteractive.com> >>
133
134 =head1 LICENCE AND COPYRIGHT
135
136 Copyright (c) 2007-2011, Chris Prather C<< <perigrin@cpan.org> >>. All rights
137 reserved.
138
139 Portions heavily borrowed from L<Proc::Daemon> which is copyright Earl Hood.
140
141 This module is free software; you can redistribute it and/or
142 modify it under the same terms as Perl itself. See L<perlartistic>.
143
144 =head1 DISCLAIMER OF WARRANTY
145
146 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
147 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
148 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
149 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
150 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
151 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
152 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
153 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
154 NECESSARY SERVICING, REPAIR, OR CORRECTION.
155
156 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
157 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
158 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
159 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
160 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
161 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
162 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
163 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
164 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
165 SUCH DAMAGES.
166
167 =cut