ok,.. more tests and stuff
[gitmo/MooseX-Daemonize.git] / lib / MooseX / Daemonize / Pid / File.pm
1 package MooseX::Daemonize::Pid::File;
2 use strict;    # because Kwalitee is pedantic
3 use Moose;
4 use Moose::Util::TypeConstraints;
5 use MooseX::Types::Path::Class;
6
7 coerce 'MooseX::Daemonize::Pid::File' 
8     => from 'Str' 
9         => via { MooseX::Daemonize::Pid::File->new( file => $_ ) }
10     => from 'Path::Class::File' 
11         => via { MooseX::Daemonize::Pid::File->new( file => $_ ) };
12
13 our $VERSION = '0.01';
14
15 extends 'MooseX::Daemonize::Pid';
16
17 has '+pid' => (
18     default => sub { 
19         my $self = shift;
20         $self->does_file_exist
21             ? $self->file->slurp(chomp => 1)
22             : $$
23     }
24 );
25
26 has 'file' => (
27     is       => 'ro',
28     isa      => 'Path::Class::File',
29     coerce   => 1,
30     required => 1,
31     handles  => [ 'remove' ]
32 );
33
34 sub does_file_exist { -s (shift)->file }
35
36 sub write {
37     my $self = shift;
38     my $fh = $self->file->openw;
39     $fh->print($self->pid);
40     $fh->close;
41 }
42
43 override 'is_running' => sub {
44     return 0 unless (shift)->does_file_exist;
45     super();
46 };
47
48 1;
49
50 __END__
51
52 =pod
53
54 =head1 NAME
55
56 MooseX::Daemonize::Pid::File - PID file management for MooseX::Daemonize
57
58 =head1 SYNOPSIS
59      
60 =head1 DESCRIPTION
61
62 =head1 ATTRIBUTES
63
64 =over
65
66 =item file Path::Class::File | Str
67
68 =back
69
70 =head1 METHODS 
71
72 =over
73
74 =item remove
75
76 =item write
77
78 =item does_file_exist
79
80 =item is_running
81
82 =item meta()
83
84 The C<meta()> method from L<Class::MOP::Class>
85
86 =back
87
88 =head1 DEPENDENCIES
89
90 Obviously L<Moose>
91
92 =head1 INCOMPATIBILITIES
93
94 None reported.
95
96 =head1 BUGS AND LIMITATIONS
97
98 No bugs have been reported.
99
100 Please report any bugs or feature requests to
101 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
102 L<http://rt.cpan.org>.
103
104 =head1 AUTHOR
105
106 Stevan Little  C<< <stevan@cpan.org> >>
107
108 =head1 LICENCE AND COPYRIGHT
109
110 Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. All rights 
111 reserved.
112
113 This module is free software; you can redistribute it and/or
114 modify it under the same terms as Perl itself. See L<perlartistic>.
115
116
117 =head1 DISCLAIMER OF WARRANTY
118
119 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
120 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
121 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
122 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
123 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
124 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
125 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
126 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
127 NECESSARY SERVICING, REPAIR, OR CORRECTION.
128
129 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
130 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
131 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
132 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
133 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
134 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
135 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
136 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
137 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
138 SUCH DAMAGES.
139
140 =cut