| 1 | # ABSTRACT: Manage locks to synchronize concurrent operations | ||||||||||||
| 2 | |||||||||||||
| 3 | package Pinto::Locker; | ||||||||||||
| 4 | |||||||||||||
| 5 | 58 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 58 | ▶ | use MooseX::StrictConstructor; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 58 | ▶ | use MooseX::MarkAsMethods ( autoclean => 1 ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 58 | ▶ | use Path::Class; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 58 | ▶ | use File::NFSLock; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 58 | ▶ | use Pinto::Util qw(debug throw whine); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 58 | ▶ | use Pinto::Types qw(File); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | #----------------------------------------------------------------------------- | ||||||||||||
| 16 | |||||||||||||
| 17 | 58 | our $VERSION = '0.14'; # VERSION | |||||||||||
| 18 | |||||||||||||
| 19 | #----------------------------------------------------------------------------- | ||||||||||||
| 20 | |||||||||||||
| 21 | 58 | ▶ | our $LOCKFILE_TIMEOUT = $ENV{PINTO_LOCKFILE_TIMEOUT} || 50; # Seconds | ||||||||||
|
Condition50%1/2
$ENV{'PINTO_LOCKFILE_TIMEOUT'} || 50
MC/DC0%0/1
$ENV{'PINTO_LOCKFILE_TIMEOUT'} || 50
$ENV{'PINTO_LOCKFILE_TIMEOUT'}
Truth table50%1/2
A
A= $ENV{'PINTO_LOCKFILE_TIMEOUT'}
| |||||||||||||
| 22 | 58 | ▶ | our $STALE_LOCKFILE_TIMEOUT = $ENV{PINTO_STALE_LOCKFILE_TIMEOUT} || 0; # Seconds | ||||||||||
|
Condition50%1/2
$ENV{'PINTO_STALE_LOCKFILE_TIMEOUT'} || 0
MC/DC0%0/1
$ENV{'PINTO_STALE_LOCKFILE_TIMEOUT'} || 0
$ENV{'PINTO_STALE_LOCKFILE_TIMEOUT'}
Truth table50%1/2
A
A= $ENV{'PINTO_STALE_LOCKFILE_TIMEOUT'}
| |||||||||||||
| 23 | |||||||||||||
| 24 | #----------------------------------------------------------------------------- | ||||||||||||
| 25 | |||||||||||||
| 26 | 58 | has repo => ( | |||||||||||
| 27 | is => 'ro', | ||||||||||||
| 28 | isa => 'Pinto::Repository', | ||||||||||||
| 29 | weak_ref => 1, | ||||||||||||
| 30 | required => 1, | ||||||||||||
| 31 | ); | ||||||||||||
| 32 | |||||||||||||
| 33 | 58 | has _lock => ( | |||||||||||
| 34 | is => 'rw', | ||||||||||||
| 35 | isa => 'File::NFSLock', | ||||||||||||
| 36 | predicate => '_is_locked', | ||||||||||||
| 37 | clearer => '_clear_lock', | ||||||||||||
| 38 | init_arg => undef, | ||||||||||||
| 39 | ); | ||||||||||||
| 40 | |||||||||||||
| 41 | #----------------------------------------------------------------------------- | ||||||||||||
| 42 | |||||||||||||
| 43 | |||||||||||||
| 44 | 203 | ▶ | sub lock { ## no critic qw(Homonym) | ||||||||||
|
Subroutine
lock: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 45 | 203 | my ( $self, $lock_type ) = @_; | |||||||||||
| 46 | |||||||||||||
| 47 | 203 | ▶ | return if $self->_is_locked; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | |||||||||||||
| 49 | 203 | ▶ | $lock_type ||= 'SH'; | ||||||||||
|
Condition50%1/2
$lock_type ||= "SH"
MC/DC0%0/1
$lock_type ||= "SH"
$lock_type
Truth table50%1/2
A
A= $lock_type
| |||||||||||||
| 50 | |||||||||||||
| 51 | 203 | local $File::NFSLock::LOCK_EXTENSION = ''; | |||||||||||
| 52 | 203 | local @File::NFSLock::CATCH_SIGS = (); | |||||||||||
| 53 | |||||||||||||
| 54 | 203 | my $root_dir = $self->repo->config->root_dir; | |||||||||||
| 55 | 203 | my $lock_file = $root_dir->file('.lock')->stringify; | |||||||||||
| 56 | |||||||||||||
| 57 | 203 | ▶ | if ($STALE_LOCKFILE_TIMEOUT) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 1 | whine( 'PINTO_STALE_LOCKFILE_TIMEOUT > 0, may steal lock !!'); | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 203 | ▶ | my $lock = File::NFSLock->new( $lock_file, $lock_type, $LOCKFILE_TIMEOUT, $STALE_LOCKFILE_TIMEOUT ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | or throw 'The repository is currently in use -- please try again later (' . $File::NFSLock::errstr . ')'; | ||||||||||||
| 63 | |||||||||||||
| 64 | 199 | debug("Process $$ got $lock_type lock on $root_dir"); | |||||||||||
| 65 | |||||||||||||
| 66 | 199 | $self->_lock($lock); | |||||||||||
| 67 | |||||||||||||
| 68 | 199 | return $self; | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | #----------------------------------------------------------------------------- | ||||||||||||
| 72 | |||||||||||||
| 73 | |||||||||||||
| 74 | 292 | ▶ | sub unlock { | ||||||||||
|
Subroutine
unlock: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 75 | 292 | my ($self) = @_; | |||||||||||
| 76 | |||||||||||||
| 77 | 292 | ▶ | return $self if not $self->_is_locked; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | |||||||||||||
| 79 | # I'm not sure if failure to unlock is really a problem | ||||||||||||
| 80 | 199 | ▶ | $self->_lock->unlock or warn 'Unable to unlock repository'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | |||||||||||||
| 82 | 199 | $self->_clear_lock; | |||||||||||
| 83 | |||||||||||||
| 84 | 199 | my $root_dir = $self->repo->config->root_dir; | |||||||||||
| 85 | 199 | debug("Process $$ released the lock on $root_dir"); | |||||||||||
| 86 | |||||||||||||
| 87 | 199 | return $self; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | #------------------------------------------------------------------------------ | ||||||||||||
| 91 | |||||||||||||
| 92 | 58 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 93 | |||||||||||||
| 94 | #----------------------------------------------------------------------------- | ||||||||||||
| 95 | 58 | 1; | |||||||||||
| 96 | |||||||||||||
| 97 | __END__ | ||||||||||||