Acrux/FilePid.pm

filter: Statementstmt 94.3% 84 / 89 Branchbran 70.8% 34 / 48 Conditioncond 57.1% 12 / 21 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 81.0% 158 / 195 CC 33 SCAR 18.9
CC
33
Cov
82%
new
14.4
load
7.2
DESTROY
6.1
CRAP 6.6
« Acrux/Pointer.pm ↑ Summary Acrux/Digest/M11R.pm »
1 package Acrux::FilePid;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 Acrux::FilePid - The Pid File simple interface
10  
11 =head1 SYNOPSIS
12  
13 use Acrux::FilePid;
14  
15 my $fp = Acrux::FilePid->new (
16 file => '/some/file.pid',
17 );
18  
19 if ( my $num = $fp->running ) {
20 die "Already running: $num";
21 } else {
22 $fp->save;
23 # . . .
24 $fp->remove;
25 }
26  
27 ... or with autoremove:
28  
29 my $fp = Acrux::FilePid->new (
30 file => '/some/file.pid',
31 autoremove => 1,
32 );
33 die "Already running" if $fp->running;
34 $fp->save;
35 # . . .
36  
37 ... or with autosave and autoremove
38  
39 my $fp = Acrux::FilePid->new (
40 file => '/some/file.pid',
41 auto => 1,
42 );
43 die "Already running" if $fp->running;
44 # . . .
45  
46 =head1 DESCRIPTION
47  
48 This software manages a pid file for you. It will create a pid file,
49 query the process within to discover if it's still running, and remove
50 the pid file.
51  
52 =head2 new
53  
54 my $fp = Acrux::FilePid->new;
55  
56 my $fp = Acrux::FilePid->new(
57 file => '/var/run/daemon.pid',
58 );
59  
60 my $fp = Acrux::FilePid->new(
61 file => '/var/run/daemon.pid',
62 pid => '145',
63 autoremove => 1,
64 );
65  
66 This constructor takes three optional paramters.
67  
68 C<file> - The name of the pid file to work on. If not specified, a pid
69 file located in current directory. So, for example, if C<$0> is F<~/bin/sig.pl>,
70 the pid file will be F<./sig.pl.pid>.
71  
72 C<pid> - The pid to write to a new pidfile. If not specified, C<$$> is
73 used when the pid file doesn't exist. When the pid file does exist, the
74 pid inside it is used.
75  
76 C<autoremove> - Auto-remove flag. If this flag specified as true, then
77 will be removed the pid file automatically on DESTROY phase. Default: false
78  
79 C<autosave> - Auto-save flag. If this flag specified as true, then
80 will be saved the pid file automatically while instance create. Default: false
81  
82 C<auto> - this flag forced sets C<autoremove> and C<autosave> flags. Default: false
83  
84 =head2 file
85  
86 $fp->file("/var/run/file.pid");
87 my $pidfile = $fp->file;
88  
89 Accessor/mutator for the filename used as the pid file.
90  
91 =head2 load
92  
93 $fp->load;
94  
95 Load owner pid from file.
96 On success, the object is returned. On failure, C<undef> is
97 returned.
98  
99 =head2 owner
100  
101 $fp->owner(123);
102 my $owner = $fp->owner;
103  
104 Accessor/mutator for the pid being saved to the pid file.
105  
106 =head2 pid
107  
108 $fp->pid(123);
109 my $pid = $fp->pid;
110  
111 Accessor/mutator for the pid being saved to the pid file.
112  
113 =head2 remove
114  
115 $fp->remove;
116  
117 Removes the pid file from disk. Returns true on success, false on
118 failure.
119  
120 =head2 running
121  
122 my $pid = $fp->running;
123 die "Service already running: $pid" if $pid;
124  
125 Checks to see if the pricess identified in the pid file is still
126 running. If the process is still running, the pid is returned. Otherwise
127 C<undef> is returned.
128  
129 =head2 save
130  
131 $fp->save;
132  
133 Writes the pid file to disk, inserting the pid inside the file.
134 On success, the object is returned. On failure, C<undef> is
135 returned.
136  
137 =head1 HISTORY
138  
139 See C<Changes> file
140  
141 =head1 TO DO
142  
143 See C<TODO> file
144  
145 =head1 SEE ALSO
146  
147 L<CTK::FilePid>, L<Acme::Ghost::FilePid>
148  
149 =head1 AUTHOR
150  
151 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
152  
153 =head1 COPYRIGHT
154  
155 Copyright (C) 1998-2026 D&D Corporation
156  
157 =head1 LICENSE
158  
159 This program is distributed under the terms of the Artistic License Version 2.0
160  
161 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
162  
163 =cut
164  
165 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 2 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 2 ▶use IO::File qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 2 ▶use Cwd qw/getcwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 8 ▶sub new {
Subroutine new: called CC=12 SCAR=26.7
Pod documented
172 8 my $class = shift;
173 0 ▶ my $args = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {};
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 8 0 8
@_ > 1 ? : 8 0 8
174 8 my $self = bless {%$args}, $class;
175 8 ▶ $self->{autoremove} ||= $args->{auto} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'auto'} ? : 2 2 4
Condition100%3/3
$self->{"autoremove"} ||= $args->{"auto"} ? 1 : 0
l!l&&r!l&&!r
422
MC/DC100%2/2
$self->{"autoremove"} ||= $args->{"auto"} ? 1 : 0
$self->{"autoremove"} $args->{"auto"} ? 1 : 0
Truth table100%3/3
A ||= B
A= $self->{"autoremove"}B= $args->{"auto"} ? 1 : 0
ABresult
1X1
011
000
176 8 ▶ $self->{autosave} ||= $args->{auto} ? 1 : 0;;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'auto'} ? : 2 6 8
Condition66%2/3
$self->{"autosave"} ||= $args->{"auto"} ? 1 : 0
l!l&&r!l&&!r
026
MC/DC50%1/2
$self->{"autosave"} ||= $args->{"auto"} ? 1 : 0
$self->{"autosave"} $args->{"auto"} ? 1 : 0
Truth table66%2/3
A ||= B
A= $self->{"autosave"}B= $args->{"auto"} ? 1 : 0
ABresult
1X1
011
000
177 8 ▶ $self->{file} //= File::Spec->catfile(getcwd, sprintf("%s.pid", basename($0)));
Condition33%1/3
$self->{"file"} //= "File::Spec"->catfile(getcwd(), sprintf("%s.pid", basename($0)))
l!l&&r!l&&!r
800
MC/DC0%0/2
$self->{"file"} //= "File::Spec"->catfile(getcwd(), sprintf("%s.pid", basename($0)))
$self->{"file"} "File::Spec"->catfile(getcwd(), sprintf("%s.pid", basename($0)))
Truth table33%1/3
A //= B
A= $self->{"file"}B= "File::Spec"->catfile(getcwd(), sprintf("%s.pid", basename($0)))
ABresult
1X1
011
000
178 8 ▶ $self->{pid} ||= $$; # Current PID
Condition33%1/3
$self->{"pid"} ||= $$
l!l&&r!l&&!r
080
MC/DC0%0/2
$self->{"pid"} ||= $$
$self->{"pid"} $$
Truth table33%1/3
A ||= B
A= $self->{"pid"}B= $$
ABresult
1X1
011
000
179 8 ▶ $self->{owner} ||= 0; # Owner PID
Condition50%1/2
$self->{"owner"} ||= 0
l!l
08
MC/DC0%0/1
$self->{"owner"} ||= 0
$self->{"owner"}
Truth table50%1/2
A
A= $self->{"owner"}
Aresult
11
00
180 8 $self->{is_running} = -1; # Unknown (is as running)
181 8 ▶ if ($self->{autosave}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autosave"}) 2 6 8
182 2 ▶ return $self->running ? $self : $self->save;
Branch: 1 branch
BranchTrueFalseTotal
$self->running ? : 0 2 2
183 }
184 6 return $self->load;
185 }
186 31 ▶sub file {
Subroutine file: called CC=2 SCAR=8.4
Pod documented
187 31 my $self = shift;
188 31 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) 0 31 31
189 0 $self->{file} = shift;
190 0 return $self;
191 }
192 31 return $self->{file};
193 }
194 12 ▶sub pid {
Subroutine pid: called CC=2 SCAR=8.4
Pod documented
195 12 my $self = shift;
196 12 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) 0 12 12
197 0 $self->{pid} = shift;
198 0 return $self;
199 }
200 12 return $self->{pid};
201 }
202 37 ▶sub owner {
Subroutine owner: called CC=2 SCAR=6.9
Pod documented
203 37 my $self = shift;
204 37 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) 23 14 37
205 23 $self->{owner} = shift;
206 23 return $self;
207 }
208 14 return $self->{owner};
209 }
210 11 ▶sub running {
Subroutine running: called CC=4 SCAR=13.9
Pod documented
211 11 my $self = shift;
212 11 ▶ my $owner = $self->load->owner || 0; # Get PID from file
Condition100%2/2
$self->load->owner || 0
l!l
83
MC/DC100%1/1
$self->load->owner || 0
$self->load->owner
Truth table100%2/2
A
A= $self->load->owner
Aresult
11
00
213 11 ▶ my $r = kill(0, $owner) ? $owner : undef;
Branch: 1 branch
BranchTrueFalseTotal
kill(0, $owner) ? : 11 0 11
214 11 ▶ $self->{is_running} = $r ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 8 3 11
215 11 return $r; # Is running?
216 }
217 7 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
218 7 my $self = shift;
219 7 my $file = $self->file;
220 7 ▶ return $self unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 7 7
221 7 unlink $file;
222 7 $self->owner(0); # Reset owner PID to 0
223 7 return $self;
224 }
225 7 ▶sub save {
Subroutine save: called CC=4 SCAR=14.5
Pod documented
226 7 my $self = shift;
227 7 my $file = $self->file;
228 7 ▶ my $pid = $self->pid || $$;
Condition33%1/3
$self->pid || $$
l!l&&r!l&&!r
700
MC/DC0%0/2
$self->pid || $$
$self->pid $$
Truth table33%1/3
A || B
A= $self->pidB= $$
ABresult
1X1
011
000
229 7 $self->owner($pid); # Set owner PID as current PID
230  
231 # Save PID to file
232 7 my $fh = IO::File->new($file, "w");
233 7 ▶ croak qq/Can't open file "$file": $!/ unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 7 7
234 7 ▶ $fh->write("$pid\n") or croak qq/Can't write to file "$file": $!/;
Branch: 1 branch
BranchTrueFalseTotal
$fh->write("$pid\n") or croak(qq[Can't write to file "$file": $!]) 7 0 7
235 7 undef $fh; # automatically closes the file
236  
237 # Returns self
238 7 return $self;
239 }
240 17 ▶sub load {
Subroutine load: called CC=7 SCAR=19.7
Pod documented
241 17 my $self = shift;
242 17 my $file = $self->file;
243 17 ▶ return $self unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 8 9 17
244  
245 # Read file
246 9 my $ret = my $content = '';
247 9 my $fh = IO::File->new($file, "r");
248 9 ▶ croak qq/Can't open file "$file": $!/ unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 9 9
249 9 ▶ while ($ret = $fh->read(my $buf, 255)) { $content .= $buf }
Branch: 1 branch
BranchTrueFalseTotal
if ($ret = $fh->read(my $buf, 255)) 9 9 18
250 9 ▶ croak qq/Can't read from file "$file": $!/ unless defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 9 9
251 9 undef $fh; # automatically closes the file
252  
253 # Set loaded PID as owner
254 9 chomp $content;
255 9 ▶ $self->owner(($content || 0) * 1) if $content =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /^\d+$/ 9 0 9
Condition50%1/2
$content || 0
l!l
90
MC/DC0%0/1
$content || 0
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
256  
257 # Returns object
258 9 return $self;
259 }
260 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=6 SCAR=18.0
261 8 my $self = shift;
262 8 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 8 8
263 8 ▶ return unless $self->{autoremove};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"autoremove"} 2 6 8
264 6 ▶ return $self->remove unless $self->{is_running};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_running"} 1 5 6
265 5 ▶ return unless $self->{owner};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"owner"} 0 5 5
266 5 ▶ $self->remove if $self->{owner} == $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"owner"} == $self->{"pid"} 4 1 5
267 }
268  
269 2 1;
270  
271 __END__
« Acrux/Pointer.pm ↑ Summary Acrux/Digest/M11R.pm »