line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Proc::tored::LockFile; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
$Proc::tored::LockFile::VERSION = '0.20'; |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
266305
|
use warnings; |
|
4
|
|
|
|
|
22
|
|
|
4
|
|
|
|
|
178
|
|
6
|
4
|
|
|
4
|
|
27
|
use strict; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
114
|
|
7
|
4
|
|
|
4
|
|
328
|
use Moo; |
|
4
|
|
|
|
|
7585
|
|
|
4
|
|
|
|
|
36
|
|
8
|
4
|
|
|
4
|
|
2404
|
use Carp; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
324
|
|
9
|
4
|
|
|
4
|
|
30
|
use Guard qw(guard); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
198
|
|
10
|
4
|
|
|
4
|
|
27
|
use Path::Tiny qw(path); |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
181
|
|
11
|
4
|
|
|
4
|
|
321
|
use Try::Tiny; |
|
4
|
|
|
|
|
1001
|
|
|
4
|
|
|
|
|
252
|
|
12
|
4
|
|
|
4
|
|
637
|
use Types::Standard -types; |
|
4
|
|
|
|
|
61496
|
|
|
4
|
|
|
|
|
40
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has file_path => ( |
16
|
|
|
|
|
|
|
is => 'ro', |
17
|
|
|
|
|
|
|
isa => Str, |
18
|
|
|
|
|
|
|
required => 1, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has file => ( |
22
|
|
|
|
|
|
|
is => 'lazy', |
23
|
|
|
|
|
|
|
isa => InstanceOf['Path::Tiny'], |
24
|
|
|
|
|
|
|
handles => [qw(exists)], |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
7
|
|
|
7
|
|
28019
|
sub _build_file { path(shift->file_path) } |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub lock { |
31
|
27
|
|
|
27
|
1
|
1773
|
my $self = shift; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
27
|
100
|
|
|
|
713
|
return if $self->exists; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $locked = try { |
37
|
25
|
|
|
25
|
|
2534
|
$self->file->filehandle({exclusive => 1}, '>'); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
catch { |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
die $_ |
44
|
0
|
0
|
0
|
0
|
|
0
|
unless $_->{op} eq 'sysopen' && $_->{err} =~ /File exists/i |
|
|
|
0
|
|
|
|
|
45
|
|
|
|
|
|
|
|| $self->exists; |
46
|
25
|
|
|
|
|
2188
|
}; |
47
|
|
|
|
|
|
|
|
48
|
25
|
50
|
|
|
|
6835
|
return unless $locked; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
return guard { |
51
|
25
|
50
|
|
|
|
2168
|
try { $self->exists && $self->file->remove } |
52
|
0
|
|
|
|
|
|
catch { carp "unable to remove lock file: $_" } |
53
|
25
|
|
|
25
|
|
577
|
}; |
|
25
|
|
|
|
|
1139
|
|
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
1; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
__END__ |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=pod |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=encoding UTF-8 |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 NAME |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Proc::tored::LockFile - Guard actions with atomic writes |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 VERSION |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
version 0.20 |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use Proc::tored::LockFile; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $lockfile = Proc::tored::LockFile->new(file_path => '/path/to/something.lock'); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
if (my $lock = $lockfile->lock) { |
79
|
|
|
|
|
|
|
... |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 file_path |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Path where lock file should be created. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 METHODS |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 lock |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Attempts to lock the guarded resource by created a new file at L</file_path>. |
93
|
|
|
|
|
|
|
If the file could not be created because it already exists (using |
94
|
|
|
|
|
|
|
C<O_CREAT|O_EXCL>), the lock attempt fails and undef is returned. If the lock |
95
|
|
|
|
|
|
|
is successfully acquired, a L<Guard> object is returned that will unlink the |
96
|
|
|
|
|
|
|
lock file as it falls out of scope. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 AUTHOR |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Jeff Ober <sysread@fastmail.fm> |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Jeff Ober. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
107
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|