line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package IPC::SRLock::Base; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
440
|
use namespace::autoclean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
491
|
use Date::Format qw( time2str ); |
|
1
|
|
|
|
|
5640
|
|
|
1
|
|
|
|
|
80
|
|
6
|
1
|
|
|
1
|
|
9
|
use English qw( -no_match_vars ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
11
|
|
7
|
1
|
|
|
|
|
13
|
use File::DataClass::Types qw( Bool LoadableClass NonEmptySimpleStr |
8
|
1
|
|
|
1
|
|
343
|
Num Object PositiveInt ); |
|
1
|
|
|
|
|
2
|
|
9
|
1
|
|
|
1
|
|
852
|
use IPC::SRLock::Utils qw( Unspecified hash_from merge_attributes throw ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
10
|
1
|
|
|
1
|
|
862
|
use Time::Elapsed qw( elapsed ); |
|
1
|
|
|
|
|
2850
|
|
|
1
|
|
|
|
|
5
|
|
11
|
1
|
|
|
1
|
|
652
|
use Time::HiRes qw( usleep ); |
|
1
|
|
|
|
|
1075
|
|
|
1
|
|
|
|
|
3
|
|
12
|
1
|
|
|
1
|
|
134
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
10
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Public attributes |
15
|
|
|
|
|
|
|
has 'debug' => is => 'ro', isa => Bool, default => 0; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has 'log' => is => 'lazy', isa => Object, |
18
|
5
|
|
|
5
|
|
636
|
builder => sub { $_[ 0 ]->_null_class->new }; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'name' => is => 'ro', isa => NonEmptySimpleStr, required => 1; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has 'nap_time' => is => 'ro', isa => Num, default => 0.1; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has 'patience' => is => 'ro', isa => PositiveInt, default => 0; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has 'time_out' => is => 'ro', isa => PositiveInt, default => 300; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Private attributes |
29
|
|
|
|
|
|
|
has '_null_class' => is => 'lazy', isa => LoadableClass, |
30
|
|
|
|
|
|
|
default => 'Class::Null', init_arg => undef; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Construction |
33
|
|
|
|
|
|
|
around 'BUILDARGS' => sub { |
34
|
|
|
|
|
|
|
my ($orig, $self, @args) = @_; my $attr = $orig->( $self, @args ); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $builder = $attr->{builder} or return $attr; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
merge_attributes $attr, $builder, [ 'debug', 'log' ]; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return $attr; |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Private methods |
44
|
|
|
|
|
|
|
sub _get_args { |
45
|
15
|
|
|
15
|
|
23
|
my $self = shift; my $args = hash_from @_; |
|
15
|
|
|
|
|
48
|
|
46
|
|
|
|
|
|
|
|
47
|
15
|
100
|
|
|
|
47
|
$args->{k} or throw Unspecified, [ 'key' ]; $args->{k} .= q(); |
|
14
|
|
|
|
|
35
|
|
48
|
14
|
|
66
|
|
|
80
|
$args->{p} //= $PID; # uncoverable condition false |
49
|
14
|
|
66
|
|
|
97
|
$args->{t} //= $self->time_out; # uncoverable condition false |
50
|
|
|
|
|
|
|
|
51
|
14
|
|
|
|
|
41
|
return $args; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _sleep_or_timeout { |
55
|
3
|
|
|
3
|
|
4591
|
my ($self, $start, $now, $key) = @_; |
56
|
|
|
|
|
|
|
|
57
|
3
|
100
|
100
|
|
|
27
|
$self->patience and $now > $start + $self->patience |
58
|
|
|
|
|
|
|
and throw 'Lock [_1] timed out', [ $key ]; |
59
|
2
|
|
|
|
|
200336
|
usleep( 1_000_000 * $self->nap_time ); |
60
|
2
|
|
|
|
|
62
|
return 1; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _timeout_error { |
64
|
1
|
|
|
1
|
|
16
|
my ($self, $key, $pid, $when, $after) = @_; |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
5
|
return "Timed out ${key} set by ${pid} on " |
67
|
|
|
|
|
|
|
. time2str( '%Y-%m-%d at %H:%M', $when )." after ${after} seconds\n"; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Public methods |
71
|
|
|
|
|
|
|
sub get_table { |
72
|
4
|
|
|
4
|
1
|
569
|
my $self = shift; |
73
|
4
|
|
|
|
|
8
|
my $count = 0; |
74
|
4
|
|
|
|
|
49
|
my $data = { align => { id => 'left', |
75
|
|
|
|
|
|
|
pid => 'right', |
76
|
|
|
|
|
|
|
stime => 'right', |
77
|
|
|
|
|
|
|
tleft => 'right'}, |
78
|
|
|
|
|
|
|
count => $count, |
79
|
|
|
|
|
|
|
fields => [ qw( id pid stime tleft ) ], |
80
|
|
|
|
|
|
|
hclass => { id => 'most' }, |
81
|
|
|
|
|
|
|
labels => { id => 'Key', |
82
|
|
|
|
|
|
|
pid => 'PID', |
83
|
|
|
|
|
|
|
stime => 'Lock Time', |
84
|
|
|
|
|
|
|
tleft => 'Time Left' }, |
85
|
|
|
|
|
|
|
values => [] }; |
86
|
|
|
|
|
|
|
|
87
|
4
|
|
|
|
|
6
|
for my $lock (@{ $self->list }) { |
|
4
|
|
|
|
|
15
|
|
88
|
2
|
|
|
|
|
3
|
my $fields = {}; |
89
|
|
|
|
|
|
|
|
90
|
2
|
|
|
|
|
5
|
$fields->{id } = $lock->{key}; |
91
|
2
|
|
|
|
|
5
|
$fields->{pid } = $lock->{pid}; |
92
|
2
|
|
|
|
|
9
|
$fields->{stime} = time2str( '%Y-%m-%d %H:%M:%S', $lock->{stime} ); |
93
|
|
|
|
|
|
|
|
94
|
2
|
|
|
|
|
304
|
my $tleft = $lock->{stime} + $lock->{timeout} - time; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# uncoverable branch false |
97
|
2
|
50
|
|
|
|
17
|
$fields->{tleft} = $tleft > 0 ? elapsed( $tleft ) : 'Expired'; |
98
|
2
|
|
|
|
|
1169
|
push @{ $data->{values} }, $fields; $count++; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
4
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
4
|
|
|
|
|
10
|
$data->{count} = $count; |
102
|
4
|
|
|
|
|
24
|
return $data; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
1; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |