| 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
| |||||||||||
| 15 | 0 | $self->_handle($fh); | |||||||||
| 16 | |||||||||||
| 17 | 0 | while (1) { | |||||||||
| 18 | 0 | ▶ | flock $fh, LOCK_EX|LOCK_NB and last; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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; | |||||||||