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