keep namespaces clean
[gitmo/MooseX-Daemonize.git] / lib / MooseX / Daemonize / Pid / File.pm
CommitLineData
5231273c 1use strict;
2use warnings;
d8985b7d 3package MooseX::Daemonize::Pid::File;
5231273c 4
2eced271 5use Moose;
8ac4733f 6use Moose::Util::TypeConstraints;
4327fe98 7
8ac4733f 8use MooseX::Types::Path::Class;
4327fe98 9use MooseX::Getopt::OptionTypeMap;
d5a304d9 10use namespace::autoclean;
d9e417f4 11
4327fe98 12# NOTE:
13# set up some basic coercions
14# that will come in handy
15# - SL
1d85c76d 16coerce 'MooseX::Daemonize::Pid::File'
17 => from 'Str'
8ac4733f 18 => via { MooseX::Daemonize::Pid::File->new( file => $_ ) }
1d85c76d 19 => from 'ArrayRef'
20 => via { MooseX::Daemonize::Pid::File->new( file => $_ ) }
21 => from 'Path::Class::File'
8ac4733f 22 => via { MooseX::Daemonize::Pid::File->new( file => $_ ) };
1d85c76d 23
4327fe98 24# NOTE:
1d85c76d 25# make sure this class plays
4327fe98 26# well with MooseX::Getopt
27# - SL
28MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
29 'MooseX::Daemonize::Pid::File' => '=s',
30);
2eced271 31
d8985b7d 32extends 'MooseX::Daemonize::Pid';
33
34has '+pid' => (
1d85c76d 35 default => sub {
2eced271 36 my $self = shift;
37 $self->does_file_exist
38 ? $self->file->slurp(chomp => 1)
39 : $$
40 }
41);
42
43has 'file' => (
44 is => 'ro',
45 isa => 'Path::Class::File',
46 coerce => 1,
47 required => 1,
48 handles => [ 'remove' ]
49);
50
51sub does_file_exist { -s (shift)->file }
52
53sub write {
54 my $self = shift;
8ac4733f 55 my $fh = $self->file->openw;
2db85b18 56 $fh->print($self->pid . "\n");
8ac4733f 57 $fh->close;
2eced271 58}
59
d8985b7d 60override 'is_running' => sub {
61 return 0 unless (shift)->does_file_exist;
62 super();
63};
2eced271 64
651;
66
67__END__
68
69=pod
70
71=head1 NAME
72
d8985b7d 73MooseX::Daemonize::Pid::File - PID file management for MooseX::Daemonize
1d85c76d 74
2eced271 75=head1 DESCRIPTION
76
4327fe98 77This object extends L<MooseX::Daemonize::Pid> to add persistence in a Pidfile.
78
1d85c76d 79This class sets up some basic coercion routines for itself so that it can
4327fe98 80be created from a I<Str> (a file name), I<ArrayRef> (an array of path components
1d85c76d 81for a filename) or a I<Path::Class::File> object.
4327fe98 82
83This class registers it's type with L<MooseX::Getopt> as well, and is expected
1d85c76d 84to be passed on the command line as a string (which will then go through the
4327fe98 85coercion routines mentioned above).
86
2eced271 87=head1 ATTRIBUTES
88
89=over
90
4327fe98 91=item I<pid Int>
92
1d85c76d 93This is inherited from L<MooseX:Daemonize::Pid> and extended here to
4327fe98 94get it's default value from the Pidfile (if available).
95
96=item I<file Path::Class::File | Str>
2eced271 97
98=back
99
1d85c76d 100=head1 METHODS
2eced271 101
102=over
103
4327fe98 104=item B<clear_pid>
105
106=item B<has_pid>
107
1d85c76d 108Both of these methods are inherited from L<MooseX:Daemonize::Pid> see that
4327fe98 109module for more information.
110
111=item B<remove>
112
113This removes the Pidfile.
114
115=item B<write>
116
117This writes the Pidfile.
118
119=item B<does_file_exist>
2eced271 120
4327fe98 121This checks if the Pidfile exists.
2eced271 122
4327fe98 123=item B<is_running>
2eced271 124
4327fe98 125This checks if the Pidfile exists, if it does it checks to see if the process
126is running, if the Pidfile doesn't exist, it returns false.
2eced271 127
128=item meta()
129
130The C<meta()> method from L<Class::MOP::Class>
131
132=back
133
134=head1 DEPENDENCIES
135
136Obviously L<Moose>
137
138=head1 INCOMPATIBILITIES
139
140None reported.
141
142=head1 BUGS AND LIMITATIONS
143
144No bugs have been reported.
145
146Please report any bugs or feature requests to
147C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
148L<http://rt.cpan.org>.
149
150=head1 AUTHOR
151
92cf56b7 152Stevan Little C<< <stevan.little@iinteractive.com> >>
2eced271 153
154=head1 LICENCE AND COPYRIGHT
155
05b96f4d 156Copyright (c) 2007-2011, Chris Prather C<< <perigrin@cpan.org> >>. All rights
2eced271 157reserved.
158
159This module is free software; you can redistribute it and/or
160modify it under the same terms as Perl itself. See L<perlartistic>.
161
162
163=head1 DISCLAIMER OF WARRANTY
164
165BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
166FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
167OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
168PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
169EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
170WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
171ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
172YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
173NECESSARY SERVICING, REPAIR, OR CORRECTION.
174
175IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
176WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
177REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
178LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
179OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
180THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
181RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
182FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
183SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
184SUCH DAMAGES.
185
2db85b18 186=cut