Carmel/Lock.pm

filter: Statementstmt 30.2% 13 / 43 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 36.3% 4 / 11 Podpod 0.0% 0 / 5 totaltotal 24.6% 17 / 69 CC 6 SCAR 19.8
CC
6
Cov
25%
acquire
20.0
pid
6.0
release
6.0
CRAP 7.2
« Carmel/Setup.pm ↑ Summary Carmel/Runtime.pm »
1 package Carmel::Lock;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Class::Tiny qw(path _handle);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶sub acquire {
Subroutine acquire: not called CC=4 SCAR=30.0
Pod undocumented
8 0 my $self = shift;
9  
10 0 $self->path->mkpath;
11  
12 0 my $timeout = 3600; # 1h: reasonable?
13  
14 0 ▶ my $fh = $self->lockfile->openw or die "Can't open ", $self->lockfile, ": $!";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->lockfile->openw or die "Can't open ", $self->lockfile, ": $!" 0 0 0
15 0 $self->_handle($fh);
16  
17 0 while (1) {
18 0 ▶ flock $fh, LOCK_EX|LOCK_NB and last;
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 6 and last 0 0 0
19  
20 0 my $pid = $self->pid;
21 0 warn "Waiting for another carmel process (pid: $pid) to finish.\n";
22  
23 0 ▶ local $SIG{ALRM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 die "Couldn't get lock held by $pid for ${timeout}s, giving up.\n";
25 0 };
26 0 alarm $timeout;
27  
28 0 ▶ flock $fh, LOCK_EX
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or die "Couldn't get lock held by $pid\n" 0 0 0
29 or die "Couldn't get lock held by $pid\n";
30 }
31  
32 0 $self->pidfile->spew("$$\n");
33  
34 0 return 1;
35 }
36  
37 0 ▶sub pid {
Subroutine pid: not called CC=2 SCAR=17.9
Pod undocumented
38 0 my $self = shift;
39  
40 0 ▶ if ($self->pidfile->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pidfile->exists) 0 0 0
41 0 chomp(my $pid = $self->pidfile->slurp);
42 0 return $pid;
43 }
44  
45 0 return '';
46 }
47  
48 0 ▶sub lockfile {
Subroutine lockfile: not called CC=1 SCAR=6.9
Pod undocumented
49 0 my $self = shift;
50 0 $self->path->child('lock');
51 }
52  
53 0 ▶sub pidfile {
Subroutine pidfile: not called CC=1 SCAR=6.9
Pod undocumented
54 0 my $self = shift;
55 0 $self->path->child('pid');
56 }
57  
58 0 ▶sub release {
Subroutine release: not called CC=2 SCAR=17.9
Pod undocumented
59 0 my $self = shift;
60  
61 0 ▶ return unless $self->_handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_handle 0 0 0
62  
63 0 $self->_handle->close;
64 0 $self->pidfile->remove;
65 }
66  
67 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
68 0 my $self = shift;
69 0 $self->release;
70 }
71  
72 1 1;
« Carmel/Setup.pm ↑ Summary Carmel/Runtime.pm »