ConcurrencyLimit/WithStandby.pm

filter: Statementstmt 82.2% 65 / 79 Branchbran 64.2% 18 / 28 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 6 totaltotal 68.6% 94 / 137 CC 21 SCAR 22.6
CC
21
Cov
74%
get_lock
14.5
new
12.8
is_locked
2.0
CRAP 9.6
« ConcurrencyLimit/WithLatestStandby.pm ↑ Summary ConcurrencyLimit/Lock/Flock.pm »
1 package IPC::ConcurrencyLimit::WithStandby;
2 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.17';
7 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IPC::ConcurrencyLimit;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶sub new {
Subroutine new: called CC=10 SCAR=25.5
Pod undocumented
12 8 my $class = shift;
13 8 my %params = @_;
14 8 my $type = delete $params{type};
15 8 ▶ $type = 'Flock' if not defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 8 0 8
16  
17 8 ▶ my $standby_type = delete($params{standby_type}) || $type;
Condition33%1/3
delete $params{'standby_type'} || $type
l!l&&r!l&&!r
080
MC/DC0%0/2
delete $params{'standby_type'} || $type
delete $params{'standby_type'} $type
Truth table33%1/3
A || B
A= delete $params{'standby_type'}B= $type
ABresult
1X1
011
000
18  
19 8 foreach my $t ($type, $standby_type) {
20 16 my $lock_class = "IPC::ConcurrencyLimit::Lock::$t";
21 16 ▶ if (not eval "require $lock_class; 1;") {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require $lock_class; 1;") 0 16 16
22 0 ▶ my $err = $@ || 'Zombie error';
Condition0%0/2
$@ || 'Zombie error'
l!l
00
MC/DC0%0/1
$@ || 'Zombie error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
23 0 croak("Invalid lock type '$t'. Could not load lock class '$lock_class': $err");
24 }
25 }
26  
27 8 my %standby;
28 8 foreach my $key (grep /^standby_/, keys %params) {
29 10 my $munged = $key;
30 10 $munged =~ s/^standby_//;
31 10 $standby{$munged} = delete $params{$key};
32 }
33 8 $standby{$_} = $params{$_} for grep !exists($standby{$_}), keys %params;
34  
35 8 my $main_lock = IPC::ConcurrencyLimit->new(%params, type => $type);
36 8 my $standby_lock = IPC::ConcurrencyLimit->new(%standby, type => $standby_type);
37  
38 my $self = bless({
39 main_lock => $main_lock,
40 standby_lock => $standby_lock,
41 retries => defined($params{retries}) ? $params{retries} : 10,
42 interval => defined($params{interval}) ? $params{interval} : 1,
43 process_name_change => $params{process_name_change},
44 8 ▶ } => $class);
Branch: 2 branches
BranchTrueFalseTotal
defined $params{'retries'} ? : 8 0 8
defined $params{'interval'} ? : 8 0 8
45  
46 8 return $self;
47 }
48  
49 10 ▶sub get_lock {
Subroutine get_lock: called CC=12 SCAR=26.7
Pod undocumented
50 10 my $self = shift;
51 10 my $main_lock = $self->{main_lock};
52  
53 # Convert retries to a sub if it's not one already
54 10 ▶ if ( ref $self->{retries} ne "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"retries"} ne "CODE") 6 4 10
55 6 my $max_retries = $self->{retries};
56 6 ▶ $self->{retries} = sub { return $_[0] <= $max_retries };
Subroutine __ANON__: called CC=1 SCAR=0.0
57 }
58  
59 10 my $id = $main_lock->get_lock;
60 10 ▶ return $id if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 4 6 10
61  
62 6 my $st_lock = $self->{standby_lock};
63 6 my $st_id = $st_lock->get_lock;
64 6 ▶ return undef if not defined $st_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $st_id 0 6 6
65  
66 # got standby lock, go into wait-retry loop
67 6 my $old_proc_name;
68 6 ▶ if ($self->{process_name_change}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"process_name_change"}) 0 6 6
69 0 $old_proc_name = $0;
70 0 $0 = "$0 - standby";
71 }
72 6 my $interval = $self->{interval};
73 eval {
74 6 my $tries = 0;
75 6 while (1) {
76 300 $id = $main_lock->get_lock;
77 300 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 3 297 300
78 3 $st_lock->release_lock;
79 3 last;
80 }
81  
82 297 ▶ last unless $self->{retries}->(++$tries);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"retries"}->(++$tries) 3 294 297
83 294 ▶ sleep($interval) if $interval;
Branch: 1 branch
BranchTrueFalseTotal
if $interval 264 30 294
84 }
85 6 1;
86 }
87 6 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $tries = 0; while (1) { $id = $main_lock->get_lock; if (defined $id) { $st_lock->release_lock; last; } ; last unless $self->{"retries"}->(++$tries); &IPC::ConcurrencyLimit::WithStandby::sleep($interval) if $interval; } ; 1 } } or do { my $err = $@ || "Zombie error"; $0 = $old_proc_name if defined $old_proc_name; $st_lock->release_lock; die $err } 6 0 6
88 0 ▶ my $err = $@ || 'Zombie error';
Condition0%0/2
$@ || 'Zombie error'
l!l
00
MC/DC0%0/1
$@ || 'Zombie error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
89 0 ▶ $0 = $old_proc_name if defined $old_proc_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_proc_name 0 0 0
90 0 $st_lock->release_lock;
91 0 die $err;
92 };
93  
94 6 ▶ $0 = $old_proc_name if defined $old_proc_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_proc_name 0 6 6
95 6 return $id;
96 }
97  
98 0 ▶sub is_locked {
Subroutine is_locked: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my $self = shift;
100 0 return $self->{main_lock}->is_locked(@_);
101 }
102  
103 4 ▶sub release_lock {
Subroutine release_lock: called CC=1 SCAR=0.0
Pod undocumented
104 4 my $self = shift;
105 4 return $self->{main_lock}->release_lock(@_);
106 }
107  
108 0 ▶sub lock_id {
Subroutine lock_id: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $self = shift;
110 0 return $self->{main_lock}->lock_id(@_);
111 }
112  
113 0 ▶sub heartbeat {
Subroutine heartbeat: not called CC=1 SCAR=6.9
Pod undocumented
114 0 my $self = shift;
115 0 return $self->{main_lock}->heartbeat;
116 }
117  
118 3 1;
119  
120 __END__
« ConcurrencyLimit/WithLatestStandby.pm ↑ Summary ConcurrencyLimit/Lock/Flock.pm »