File Coverage

blib/lib/PidFile.pm
Criterion Covered Total %
statement 40 40 100.0
branch n/a
condition n/a
subroutine 18 18 100.0
pod n/a
total 58 58 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             #*
3             #* Name: PidFile
4             #* Info: simple read / write pidfile
5             #* Author: Lukasz Romanowski (roman) <lroman@cpan.org>
6             #*
7 2     2   26259 use MooseX::Declare; # MooseX classes
  2         5641453  
  2         14  
8              
9 2     2   4054 use strict;
  2         5  
  2         58  
10 2     2   12 use warnings;
  2         12  
  2         59  
11              
12 2     2   69805 class PidFile {
  2     2   5  
  2     2   88  
  2     2   37864  
  2         6  
  2         22  
  2         12652  
  2         5  
  2         23  
  2         213  
13              
14             # --- version ---
15             our $VERSION = '1.04';
16              
17             #=------------------------------------------------------------------------( use, constants )
18              
19             # --- CPAN ---
20 2     2   137 use Carp; # confess
  2         3  
  2         158  
21 2     2   979 use FindBin; # script name
  2         6837  
  2         91  
22 2     2   1052 use File::Slurp; # read_file() / write_file()
  2         16281  
  2         155  
23 2     2   19 use File::Basename; # basename()
  2         3  
  2         137  
24 2     2   1871 use MooseX::ClassAttribute; # class attributes
  2         179731  
  2         13  
25              
26             #=------------------------------------------------------------------------( class attributes )
27              
28             class_has 'Dir' => ( is => 'rw', isa => 'Str', default => q{/var/run} ); # pid file dir
29             class_has 'Suffix' => ( is => 'rw', isa => 'Str', default => q{} ); # pid file suffix
30              
31             #=------------------------------------------------------------------------( class methods )
32             # start every class function with Capital letter
33              
34             # default pidfile is /var/run/$PROGRAM_NAME.pid
35             # default pid is $PID ($$)
36              
37             #=-------
38             # Path
39             #=-------
40             #* get path to pid file
41             #* return path to pid file
42 2     2   1355078 method Path( $class: Str :name( $p_name ) = $FindBin::Script ) {
43             return $class->Dir . '/' . basename( $p_name ) . ( $class->Suffix && '_'.$class->Suffix ) . '.pid';
44             }
45              
46             #=-------
47             # Read
48             #=-------
49             #* read pid from pid file
50             #* return pid from pidfile or undef if pidfile not exists
51 2     2   233733 method Read( $class: Str :name( $p_name ) = $FindBin::Script ) {
52             my $file = $class->Path( 'name' => $p_name );
53              
54             if ( not -f $file ) {
55             carp "missing pid file: $file";
56             return;
57             }
58              
59             my $pid = read_file( $file );
60             chomp $pid;
61              
62             return $pid;
63             }
64              
65             #=--------
66             # Write
67             #=--------
68             #* write pid to pid file
69             #* return 1 upon successfully writing the file or undef if it encountered an error
70 2     2   374190 method Write( $class: Int :pid( $p_pid ) = $$, :name( $p_name ) = $FindBin::Script ) {
71             my $file = $class->Path( 'name' => $p_name );
72              
73             if ( -f $file ) {
74             carp "find old pid file: $file";
75              
76             my $old_pid = $class->Read( 'name' => $p_name );
77             return 1 if $old_pid == $p_pid;
78              
79             confess "old process (pid: $old_pid) arleady running!" if $class->Check( 'pid' => $old_pid );
80             # or
81             $class->Delete( 'name' => $p_name );
82             }
83              
84             return write_file( $file, $p_pid );
85             }
86              
87             #=---------
88             # Delete
89             #=---------
90             #* delete pid file
91             #* return 1 if file successfully deleted, else 0
92 2     2   246583 method Delete( $class: Str :name( $p_name ) = $FindBin::Script ) {
93             return unlink $class->Path( 'name' => $p_name );
94             }
95              
96             #=--------
97             # Check
98             #=--------
99             #* check if process running
100             #* return pid if proces exists, undef if error, else 0
101 2     2   378794 method Check( $class: Int :pid( $p_pid ) = $$, Str :name( $p_name ) = q{} ) {
102             my $pid = $p_name ? $class->Read( 'name' => $p_name ) : $p_pid;
103             return undef if not $pid;
104             return +( kill 0, $pid ) ? $pid : 0;
105             }
106              
107 2     2   5307 }
108              
109             __END__
110              
111             =pod
112              
113             =head1 NAME
114              
115             PidFile - simple read / write pidfile
116              
117             =head1 SYNOPSIS
118              
119             use PidFile;
120              
121             # read pidfile
122             my $pid = PidFile->Read;
123              
124             if ( $pid ) {
125             # pid file for this script arealdy exists
126              
127             # check if script running
128             if ( PidFile->Check( "pid" => $pid ) {
129              
130             # script running, so i die
131             confess;
132             }
133              
134             # script not running, delete old pidfile
135             PidFile->Delete;
136             }
137              
138             # save new pid file
139             PidFile->Write;
140              
141              
142             ## or you can run just only
143              
144             PidFile->Write;
145              
146             ## and this function check if old pidfile exists and if script running
147              
148             =head1 DESCRIPTION
149              
150             PidFile provide very simple class methods to manages a pidfile for the current or any process.
151              
152             =head1 CLASS METHODS
153              
154             =over 2
155              
156             =item B<Path>
157              
158             get path to pid file
159              
160             input (hash):
161              
162             C<name> => (str) script name [ default: C<$FindBin::Script> ]
163              
164             return: path to pid file
165              
166             =item B<Read>
167              
168             read pid from pid file
169              
170             input (hash):
171              
172             C<name> => (str) script name [ default: C<$FindBin::Script> ]
173              
174             return: pid from pidfile or undef if pidfile not exists
175              
176             =item B<Write>
177              
178             write pid to pid file
179              
180             input (hash):
181              
182             C<pid> => (int) process id [ default: C<$$> ]
183              
184             C<name> => (str) script name [ default: C<$FindBin::Script> ]
185              
186             return: 1 upon successfully writing the file or undef if it encountered an error
187              
188             =item B<Delete>
189              
190             delete pid file
191              
192             input (hash):
193              
194             C<name> => (str) script name [ default: C<$FindBin::Script> ]
195              
196             return: 1 if file successfully deleted, else 0
197              
198             =item B<Check>
199              
200             check if process running
201              
202             input (hash):
203              
204             C<pid> => (int) process id [ default: C<$$> ]
205              
206             C<name> => (str) script name [ optional ]
207              
208             return: pid if proces exists, undef if error, else 0
209              
210             =back
211              
212             =head1 CLASS ATTRIBUTES
213              
214             =over 2
215              
216             =item B<Dir>
217              
218             set / get pid file dir
219              
220             default: /var/run
221              
222             =item B<Suffix>
223              
224             set / get pidfile suffix
225              
226             default: empty sting
227              
228             =back
229              
230             =head1 AUTHOR
231              
232             Lukasz Romanowski (roman) <lroman@cpan.org>
233              
234             =head1 LICENSE
235              
236             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
237              
238             =cut
239