Commit | Line | Data |
5231273c |
1 | use strict; |
2 | use warnings; |
d8985b7d |
3 | package MooseX::Daemonize::Pid::File; |
5231273c |
4 | |
2eced271 |
5 | use Moose; |
8ac4733f |
6 | use Moose::Util::TypeConstraints; |
4327fe98 |
7 | |
62b880da |
8 | our $VERSION = '0.15'; |
4327fe98 |
9 | |
8ac4733f |
10 | use MooseX::Types::Path::Class; |
4327fe98 |
11 | use MooseX::Getopt::OptionTypeMap; |
d9e417f4 |
12 | |
4327fe98 |
13 | # NOTE: |
14 | # set up some basic coercions |
15 | # that will come in handy |
16 | # - SL |
1d85c76d |
17 | coerce '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 |
29 | MooseX::Getopt::OptionTypeMap->add_option_type_to_map( |
30 | 'MooseX::Daemonize::Pid::File' => '=s', |
31 | ); |
2eced271 |
32 | |
d8985b7d |
33 | extends 'MooseX::Daemonize::Pid'; |
34 | |
35 | has '+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 | |
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; |
8ac4733f |
56 | my $fh = $self->file->openw; |
2db85b18 |
57 | $fh->print($self->pid . "\n"); |
8ac4733f |
58 | $fh->close; |
2eced271 |
59 | } |
60 | |
d8985b7d |
61 | override 'is_running' => sub { |
62 | return 0 unless (shift)->does_file_exist; |
63 | super(); |
64 | }; |
2eced271 |
65 | |
66 | 1; |
67 | |
68 | __END__ |
69 | |
70 | =pod |
71 | |
72 | =head1 NAME |
73 | |
d8985b7d |
74 | MooseX::Daemonize::Pid::File - PID file management for MooseX::Daemonize |
1d85c76d |
75 | |
2eced271 |
76 | =head1 DESCRIPTION |
77 | |
4327fe98 |
78 | This object extends L<MooseX::Daemonize::Pid> to add persistence in a Pidfile. |
79 | |
1d85c76d |
80 | This class sets up some basic coercion routines for itself so that it can |
4327fe98 |
81 | be created from a I<Str> (a file name), I<ArrayRef> (an array of path components |
1d85c76d |
82 | for a filename) or a I<Path::Class::File> object. |
4327fe98 |
83 | |
84 | This class registers it's type with L<MooseX::Getopt> as well, and is expected |
1d85c76d |
85 | to be passed on the command line as a string (which will then go through the |
4327fe98 |
86 | coercion routines mentioned above). |
87 | |
2eced271 |
88 | =head1 ATTRIBUTES |
89 | |
90 | =over |
91 | |
4327fe98 |
92 | =item I<pid Int> |
93 | |
1d85c76d |
94 | This is inherited from L<MooseX:Daemonize::Pid> and extended here to |
4327fe98 |
95 | get 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 |
109 | Both of these methods are inherited from L<MooseX:Daemonize::Pid> see that |
4327fe98 |
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> |
2eced271 |
121 | |
4327fe98 |
122 | This checks if the Pidfile exists. |
2eced271 |
123 | |
4327fe98 |
124 | =item B<is_running> |
2eced271 |
125 | |
4327fe98 |
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. |
2eced271 |
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 | |
92cf56b7 |
153 | Stevan Little C<< <stevan.little@iinteractive.com> >> |
2eced271 |
154 | |
155 | =head1 LICENCE AND COPYRIGHT |
156 | |
05b96f4d |
157 | Copyright (c) 2007-2011, Chris Prather C<< <perigrin@cpan.org> >>. All rights |
2eced271 |
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 | |
2db85b18 |
187 | =cut |