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