blib/lib/JIP/LockFile.pm

filter: Statementstmt 91.4% 85 / 93 Branchbran 78.5% 22 / 28 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 23 / 23 Podpod 37.5% 3 / 8 totaltotal 86.4% 134 / 155 SCAR 28.0
CC
16
Cov
88%
_lock
8.0
get_lock_data
4.0
unlock
3.2
CRAP 16.5
↑ Summary
1 package JIP::LockFile;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Fcntl qw(LOCK_EX LOCK_NB);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.063';
13  
14 sub new {
15 16 ▶ my ( $class, %param ) = @ARG;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
16  
17 # Mandatory options
18 16 ▶ if ( !exists $param{lock_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param{"lock_file"}) 1 15 16
19 1 croak q{Mandatory argument "lock_file" is missing};
20 }
21  
22 # Check "lock_file"
23 15 my $lock_file = $param{lock_file};
24 15 ▶ if ( !length $lock_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $lock_file) 2 13 15
25 2 croak q{Bad argument "lock_file"};
26 }
27  
28 # Class to object
29 13 return bless(
30 {
31 is_locked => 0,
32 fh => undef,
33 error => undef,
34 lock_file => $lock_file,
35 },
36 $class,
37 );
38 } ## end sub new
39  
40 sub is_locked {
41 40 ▶ my ($self) = @ARG;
Subroutine is_locked: called CC=1 SCAR=0.0
Pod documented
42  
43 40 return $self->{is_locked};
44 }
45  
46 sub lock_file {
47 26 ▶ my ($self) = @ARG;
Subroutine lock_file: called CC=1 SCAR=0.0
Pod documented
48  
49 26 return $self->{lock_file};
50 }
51  
52 sub error {
53 5 ▶ my ($self) = @ARG;
Subroutine error: called CC=1 SCAR=0.0
Pod documented
54  
55 5 return $self->{error};
56 }
57  
58 # Lock or raise an exception
59 sub lock {
60 6 ▶ my ($self) = @ARG;
Subroutine lock: called CC=1 SCAR=0.0
Pod undocumented
61  
62 6 return $self->_lock();
63 }
64  
65 # Or just return undef
66 sub try_lock {
67 4 ▶ my ($self) = @ARG;
Subroutine try_lock: called CC=1 SCAR=0.0
Pod undocumented
68  
69 4 return $self->_lock( try => 1 );
70 }
71  
72 # You can manually unlock
73 sub unlock {
74 15 ▶ my ($self) = @ARG;
Subroutine unlock: called CC=3 SCAR=11.6
Pod undocumented
75  
76 # Re-unlocking changes nothing
77 15 ▶ return $self if !$self->is_locked();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_locked 9 6 15
78  
79 # Close filehandle before file removing
80 6 $self->_set_fh(undef);
81  
82 6 ▶ if ( !unlink $self->lock_file() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $self->lock_file) 0 6 6
83 0 $self->_set_error($OS_ERROR);
84  
85 0 croak sprintf( q{Can't unlink "%s": %s}, $self->lock_file(), $self->error() );
86 }
87  
88 6 return $self->_set_is_locked(0);
89 }
90  
91 sub get_lock_data {
92 9 ▶ my ($self) = @_;
Subroutine get_lock_data: called CC=4 SCAR=13.9
Pod undocumented
93  
94 9 my $line;
95 {
96 9 ▶ my $fh
Branch: 1 branch
BranchTrueFalseTotal
$self->is_locked ? : 6 3 9
97 = $self->is_locked()
98 ? $self->_fh()
99 : $self->_init_file_handle();
100  
101 9 ▶ return if !$fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 9 9
102  
103 9 $fh->seek( 0, 0 );
104  
105 9 $line = $fh->getline();
106 }
107  
108 9 ▶ return if !$line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 1 8 9
109  
110 8 chomp $line;
111  
112 8 my ( $pid, $executable_name ) = $line =~ m{
113 ^
114 {
115 "pid":"(\d+)"
116 ,
117 "executable_name":"( [^""]+ )"
118 }
119 $
120 }x;
121  
122 return {
123 8 pid => $pid,
124 executable_name => $executable_name,
125 };
126 } ## end sub get_lock_data
127  
128 # unlocking on scope exit
129 sub DESTROY {
130 13 ▶ my ($self) = @ARG;
Subroutine DESTROY: called CC=1 SCAR=0.0
131  
132 13 return $self->unlock();
133 }
134  
135 sub _init_file_handle {
136 11 ▶ my ($self) = @ARG;
Subroutine _init_file_handle: called CC=2 SCAR=7.4
137  
138 11 my $fh = IO::File->new( $self->lock_file(), O_RDWR | O_CREAT );
139  
140 11 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 11 11
141 0 $self->_set_error($OS_ERROR);
142 }
143  
144 11 return $fh;
145 }
146  
147 sub _lock {
148 10 ▶ my ( $self, %param ) = @_;
Subroutine _lock: called CC=7 SCAR=20.8
149  
150 # Re-locking changes nothing
151 10 ▶ return $self if $self->is_locked();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_locked 2 8 10
152  
153 8 my $fh = $self->_init_file_handle();
154  
155 8 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 8 8
156 0 croak sprintf( q{Can't open "%s": %s}, $self->lock_file(), $self->error() );
157 }
158  
159 8 ▶ if ( !flock $fh, LOCK_EX | LOCK_NB ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 6) 2 6 8
160 2 $self->_set_error($OS_ERROR);
161  
162 2 ▶ return if $param{try};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"try"} 1 1 2
163  
164 1 croak sprintf( q{Can't lock "%s": %s}, $self->lock_file(), $self->error() );
165 }
166  
167 6 ▶ if ( !truncate $fh, 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (truncate $fh, 0) 0 6 6
168 0 $self->_set_error($OS_ERROR);
169  
170 0 croak sprintf( q{Can't truncate "%s": %s}, $self->lock_file(), $self->error() );
171 }
172  
173 6 autoflush $fh 1;
174  
175 6 ▶ if ( !$fh->print( $self->_lock_message() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->print($self->_lock_message)) 0 6 6
176 0 $self->_set_error($OS_ERROR);
177  
178 0 croak sprintf( q{Can't write message to file: %s}, $self->error() );
179 }
180  
181 6 return $self->_set_fh($fh)->_set_is_locked(1);
182 } ## end sub _lock
183  
184 sub _lock_message {
185 6 ▶ return sprintf(
Subroutine _lock_message: called CC=1 SCAR=0.0
186 q[{"pid":"%s","executable_name":"%s"}],
187 $PROCESS_ID,
188 $EXECUTABLE_NAME,
189 );
190 }
191  
192 sub _set_is_locked {
193 12 ▶ my ( $self, $is_locked ) = @ARG;
Subroutine _set_is_locked: called CC=1 SCAR=0.0
194  
195 12 $self->{is_locked} = $is_locked;
196  
197 12 return $self;
198 }
199  
200 sub _fh {
201 6 ▶ my ($self) = @ARG;
Subroutine _fh: called CC=1 SCAR=0.0
202  
203 6 return $self->{fh};
204 }
205  
206 sub _set_fh {
207 12 ▶ my ( $self, $fh ) = @ARG;
Subroutine _set_fh: called CC=1 SCAR=0.0
208  
209 12 $self->{fh} = $fh;
210  
211 12 return $self;
212 }
213  
214 sub _set_error {
215 2 ▶ my ( $self, $error ) = @ARG;
Subroutine _set_error: called CC=2 SCAR=7.1
216  
217 2 ▶ $self->{error} = $error || '<unknown_error>';
Condition50%1/2
$error || '<unknown_error>'
l!l
20
MC/DC0%0/1
$error || '<unknown_error>'
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
218  
219 2 return $self;
220 }
221  
222 1 1;
223  
224 __END__
↑ Summary