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