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