blib/lib/IPC/Lock/WithTTL.pm

filter: Statementstmt 98.6% 71 / 72 Branchbran 80.7% 21 / 26 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 11 / 11 Podpod 75.0% 3 / 4 totaltotal 90.9% 110 / 121 CC 16 SCAR 21.3
CC
16
Cov
92%
acquire
14.3
_fh
3.0
BEGIN
1.0
CRAP 8.4
↑ Summary
1 package IPC::Lock::WithTTL;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '0.02';
7  
8 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Smart::Args;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 rw => [qw(ttl)],
12 ro => [qw(file kill_old_proc)],
13 6 );
14 6 ▶use Fcntl qw(:DEFAULT :flock :seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17 5 args(my $class,
18 my $file => { isa => 'Str' },
19 my $ttl => { isa => 'Int', default => 0 },
20 my $kill_old_proc => { isa => 'Bool', default => 0 },
21 );
22  
23 5 my $self = bless {
24 file => $file,
25 ttl => $ttl,
26 kill_old_proc => $kill_old_proc,
27 #
28 _fh => undef,
29 }, $class;
30  
31 5 return $self;
32 }
33  
34 17 ▶sub _fh {
Subroutine _fh: called CC=3 SCAR=11.0
35 17 args(my $self);
36  
37 17 ▶ unless ($self->{_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_fh"}) 5 12 17
38 5 ▶ open $self->{_fh}, '+>>', $self->file or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"_fh"}, "+>>", $self->file or croak($!) 5 0 5
39 }
40  
41 17 return $self->{_fh};
42 }
43  
44 9 ▶sub acquire {
Subroutine acquire: called CC=14 SCAR=26.6
Pod documented
45 9 args(my $self,
46 my $ttl => { isa => 'Int', optional => 1 },
47 );
48 9 ▶ $self->ttl($ttl) if $ttl;
Branch: 1 branch
BranchTrueFalseTotal
if $ttl 0 9 9
49  
50 9 my $fh = $self->_fh;
51 9 ▶ flock $fh, LOCK_EX or return;
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or return 9 0 9
52  
53 9 seek $fh, 0, SEEK_SET;
54 9 my($heartbeat) = <$fh>;
55 9 ▶ $heartbeat ||= "0 0";
Condition100%2/2
$heartbeat ||= "0 0"
l!l
63
MC/DC100%1/1
$heartbeat ||= "0 0"
$heartbeat
Truth table100%2/2
A
A= $heartbeat
Aresult
11
00
56 9 my($pid, $expiration) = split /\s+/, $heartbeat;
57 9 $pid += 0; $expiration += 0;
58  
59 9 my $now = time();
60 9 my $new_expiration;
61 9 my $acquired = 0;
62 9 ▶ if ($pid == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid == 0) { } 5 4 9
elsif ($pid != $$) { } 2 2 4
63 # Previous task finished successfully
64 5 ▶ if ($now >= $expiration) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now >= $expiration) { } 4 1 5
65 # expired
66 4 $new_expiration = $self->update_heartbeat;
67 4 $acquired = 1;
68 } else {
69 # not expired
70 1 $acquired = 0;
71 }
72 } elsif ($pid != $$) {
73 # Other task is in process?
74 2 ▶ if ($now >= $expiration) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now >= $expiration) { } 1 1 2
75 # expired (Last task may have terminated abnormally)
76 1 $new_expiration = $self->update_heartbeat;
77  
78 1 ▶ if ($self->kill_old_proc && $pid > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kill_old_proc and $pid > 0) 0 1 1
Condition33%1/3
$self->kill_old_proc and $pid > 0
!ll&&!rl&&r
100
MC/DC0%0/2
$self->kill_old_proc and $pid > 0
$self->kill_old_proc $pid > 0
Truth table33%1/3
A and B
A= $self->kill_old_procB= $pid > 0
ABresult
0X0
100
111
79 0 kill 'KILL', $pid;
80 }
81 1 $acquired = 1;
82 } else {
83 # not expired (Still running)
84 1 $acquired = 0;
85 }
86 } else {
87 # Previous task done by this process
88 2 ▶ if ($now >= $expiration) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now >= $expiration) { } 1 1 2
89 # expired (Last task may have terminated abnormally)
90 1 $new_expiration = $self->update_heartbeat;
91 1 $acquired = 1;
92 } else {
93 # not expired (Last task may have terminated abnormally)
94 1 $new_expiration = $self->update_heartbeat;
95 1 $acquired = 1;
96 }
97 }
98  
99 9 flock $fh, LOCK_UN;
100 9 ▶ if ($acquired) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acquired) { } 7 2 9
101 7 ▶ return wantarray ? (1, { pid => $$, expiration => $new_expiration })
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 1 7
102 : 1;
103 } else {
104 2 ▶ return wantarray ? (0, { pid => $pid, expiration => $expiration })
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
105 : 0;
106 }
107 }
108  
109 1 ▶sub release {
Subroutine release: called CC=1 SCAR=0.0
Pod documented
110 1 args(my $self);
111  
112 1 $self->update_heartbeat(pid => 0);
113 1 undef $self->{_fh};
114  
115 1 return 1;
116 }
117  
118 8 ▶sub update_heartbeat {
Subroutine update_heartbeat: called CC=1 SCAR=0.0
Pod undocumented
119 8 args(my $self,
120 my $pid => { isa => 'Int', default => $$ },
121 );
122  
123 8 my $fh = $self->_fh;
124  
125 8 my $expiration = time() + $self->ttl;
126  
127 8 seek $fh, 0, SEEK_SET;
128 8 truncate $fh, 0;
129 8 print {$fh} join(' ', $pid, $expiration)."\n";
130  
131 8 return $expiration;
132 }
133  
134 6 1;
135  
136 __END__
↑ Summary