Skilletor/State.pm

filter: Statementstmt 83.0% 225 / 271 Branchbran 60.1% 89 / 148 Conditioncond 43.8% 50 / 114 MC/DCmcdc 20.9% 13 / 62 Subroutinesub 89.1% 41 / 46 Podpod 94.1% 16 / 17 totaltotal 65.9% 434 / 658 CC 119 SCAR 26.4
CC
119
Cov
68%
_break_lock
53.6
sources_read
26.3
put_hermes_home
25.8
CRAP 14.0
« Skilletor/JSON.pm ↑ Summary Skilletor/URL.pm »
1 package Skilletor::State;
2 # ABSTRACT: Instance state under ~/.claude/skilletor – trust, the sync lock and the records a sync keeps
3  
4 35 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use Sys::Hostname ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Skilletor::Error::LockTimeout;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Skilletor::FSUtil qw( read_text write_text path_join );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Skilletor::JSON qw( js_parse js_stringify js_ordered js_sort_keys js_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 35 our $VERSION = '0.001';
17  
18 35 has root => (
19 is => 'ro',
20 required => 1,
21 );
22  
23 483 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
24 483 my ( $self ) = @_;
25 483 path( $self->root )->mkpath;
26 }
27  
28 1 ▶sub for_home {
Subroutine for_home: called CC=1 SCAR=0.0
Pod documented
29 1 my ( $class, $home ) = @_;
30 1 return $class->new( root => $class->default_root($home) );
31 }
32  
33 265 ▶sub default_root {
Subroutine default_root: called CC=3 SCAR=11.0
Pod documented
34 265 my ( $class, $home, $claude_config_dir ) = @_;
35 265 ▶ return path_join( ( defined $claude_config_dir && length $claude_config_dir ) ? $claude_config_dir : path_join( $home, '.claude' ), 'skilletor' );
Branch: 1 branch
BranchTrueFalseTotal
defined $claude_config_dir && length $claude_config_dir ? : 2 263 265
Condition100%3/3
defined $claude_config_dir and length $claude_config_dir
!ll&&!rl&&r
26212
MC/DC100%2/2
defined $claude_config_dir and length $claude_config_dir
defined $claude_config_dir length $claude_config_dir
Truth table100%3/3
A and B
A= defined $claude_config_dirB= length $claude_config_dir
ABresult
0X0
100
111
36 }
37  
38 1005 ▶sub _file { path_join( $_[0]->root, $_[1] ) }
Subroutine _file: called CC=1 SCAR=0.0
39  
40 # A state file as an object; anything unreadable or no object reads as an empty one.
41 270 ▶sub _read_json {
Subroutine _read_json: called CC=3 SCAR=11.0
42 270 my ( $self, $name ) = @_;
43 270 my $value = eval { js_parse( read_text( $self->_file($name) ) ) };
44 270 ▶ return ( defined $value && js_type($value) eq 'object' ) ? $value : js_ordered();
Branch: 1 branch
BranchTrueFalseTotal
defined $value && js_type($value) eq 'object' ? : 45 225 270
Condition100%3/3
defined $value and js_type($value) eq "object"
!ll&&!rl&&r
223245
MC/DC100%2/2
defined $value and js_type($value) eq "object"
defined $value js_type($value) eq "object"
Truth table100%3/3
A and B
A= defined $valueB= js_type($value) eq "object"
ABresult
0X0
100
111
45 }
46  
47 35 ▶sub _write_json {
Subroutine _write_json: called CC=1 SCAR=0.0
48 35 my ( $self, $name, $value ) = @_;
49 35 write_text( $self->_file($name), js_stringify( $value, 2 )."\n" );
50 }
51  
52 #### Trust #############################################################
53  
54 5 ▶sub trust {
Subroutine trust: called CC=1 SCAR=0.0
Pod documented
55 5 my ( $self, $name, $backend ) = @_;
56 5 my $trust = $self->_read_json('trust.json');
57 5 $trust->{$name} = js_ordered( kind => $backend->{kind}, address => $backend->{address} );
58 5 $self->_write_json( 'trust.json', $trust );
59 }
60  
61 # A user-origin backend is trusted as is; a project-origin one needs an entry for exactly its
62 # kind and address. An entry written before k66 is the URL alone: it counts for a git or url
63 # backend at exactly that URL, never for a local one.
64 381 ▶sub is_trusted {
Subroutine is_trusted: called CC=12 SCAR=25.4
Pod documented
65 381 my ( $self, $name, $backend ) = @_;
66 381 ▶ return 1 if ( $backend->{origin} // '' ) eq 'user';
Branch: 1 branch
BranchTrueFalseTotal
if ($backend->{"origin"} // "") eq "user" 374 7 381
Condition50%1/2
$backend->{"origin"} // ""
l!l
3810
MC/DC0%0/1
$backend->{"origin"} // ""
$backend->{"origin"}
Truth table50%1/2
A
A= $backend->{"origin"}
Aresult
11
00
67 7 my $trust = $self->_read_json('trust.json');
68 7 ▶ return 0 unless exists $trust->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $trust->{$name} 2 5 7
69 5 my $entry = $trust->{$name};
70 5 my $type = js_type($entry);
71 5 ▶ if ( $type eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "string") 3 2 5
72 3 ▶ return ( $backend->{kind} ne 'local' && $entry eq $backend->{address} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$backend->{'kind'} ne 'local' && $entry eq $backend->{'address'} ? : 2 1 3
Condition66%2/3
$backend->{"kind"} ne "local" and $entry eq $backend->{"address"}
!ll&&!rl&&r
102
MC/DC50%1/2
$backend->{"kind"} ne "local" and $entry eq $backend->{"address"}
$backend->{"kind"} ne "local" $entry eq $backend->{"address"}
Truth table66%2/3
A and B
A= $backend->{"kind"} ne "local"B= $entry eq $backend->{"address"}
ABresult
0X0
100
111
73 }
74 2 ▶ return 0 unless $type eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "object" 0 2 2
75 return ( js_type( $entry->{kind} ) eq 'string' && $entry->{kind} eq $backend->{kind}
76 2 ▶ && js_type( $entry->{address} ) eq 'string' && $entry->{address} eq $backend->{address} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
js_type($entry->{'kind'}) eq 'string' && $entry->{'kind'} eq $backend->{'kind'} && js_type($entry->{'address'}) eq 'string' && $entry->{'address'} eq $backend->{'address'} ? : 1 1 2
Condition66%6/9
js_type($entry->{"kind"}) eq "string" and $entry->{"kind"} eq $backend->{"kind"} and js_type($entry->{"address"}) eq "string" and $entry->{"address"} eq $backend->{"address"}
!ll&&!rl&&r
101
js_type($entry->{"kind"}) eq "string" and $entry->{"kind"} eq $backend->{"kind"} and js_type($entry->{"address"}) eq "string"
!ll&&!rl&&r
101
js_type($entry->{"kind"}) eq "string" and $entry->{"kind"} eq $backend->{"kind"}
!ll&&!rl&&r
011
MC/DC25%1/4
js_type($entry->{"kind"}) eq "string" and $entry->{"kind"} eq $backend->{"kind"} and js_type($entry->{"address"}) eq "string" and $entry->{"address"} eq $backend->{"address"}
js_type($entry->{"kind"}) eq "string" $entry->{"kind"} eq $backend->{"kind"} js_type($entry->{"address"}) eq "string" $entry->{"address"} eq $backend->{"address"}
Truth table40%2/5
A and B and C and D
A= js_type($entry->{"kind"}) eq "string"B= $entry->{"kind"} eq $backend->{"kind"}C= js_type($entry->{"address"}) eq "string"D= $entry->{"address"} eq $backend->{"address"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
77 }
78  
79 #### Last check (the in-session check's clock) #########################
80  
81 0 ▶sub _now_ms_number { 0 + int( Time::HiRes::time() * 1000 ) }
Subroutine _now_ms_number: not called CC=1 SCAR=6.9
82  
83 0 ▶sub is_due {
Subroutine is_due: not called CC=3 SCAR=24.8
Pod documented
84 0 my ( $self, $scope_key, $interval_seconds ) = @_;
85 0 my $last = $self->_read_json('last-check.json')->{$scope_key};
86 0 ▶ return 1 unless js_type($last) eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($last) eq "number" 0 0 0
87 0 ▶ return _now_ms_number() - $last >= $interval_seconds * 1000 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_now_ms_number() - $last >= $interval_seconds * 1000 ? : 0 0 0
88 }
89  
90 0 ▶sub mark_checked {
Subroutine mark_checked: not called CC=1 SCAR=6.9
Pod documented
91 0 my ( $self, $scope_key ) = @_;
92 0 my $checks = $self->_read_json('last-check.json');
93 0 $checks->{$scope_key} = _now_ms_number();
94 0 $self->_write_json( 'last-check.json', $checks );
95 }
96  
97 0 ▶sub clear_checked {
Subroutine clear_checked: not called CC=2 SCAR=17.9
Pod documented
98 0 my ( $self, $scope_key ) = @_;
99 0 my $checks = $self->_read_json('last-check.json');
100 0 ▶ return unless exists $checks->{$scope_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $checks->{$scope_key} 0 0 0
101 0 delete $checks->{$scope_key};
102 0 $self->_write_json( 'last-check.json', $checks );
103 }
104  
105 #### Unreached drift (spec §14.3, k70) #################################
106  
107 # The drift ids the last sync of a scope (keyed by its lock path) left in place although every
108 # source it needed was at hand: `check` does not count them again.
109 13 ▶sub unreached {
Subroutine unreached: called CC=2 SCAR=8.4
Pod documented
110 13 my ( $self, $scope_key ) = @_;
111 13 my $ids = $self->_read_json('unreached.json')->{$scope_key};
112 13 ▶ return [] unless js_type($ids) eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($ids) eq "array" 13 0 13
113 0 return [ grep { js_type($_) eq 'string' } @$ids ];
114 }
115  
116 26 ▶sub put_unreached {
Subroutine put_unreached: called CC=4 SCAR=19.2
Pod documented
117 26 my ( $self, $scope_key, $ids ) = @_;
118 26 my $all = $self->_read_json('unreached.json');
119 26 my %seen;
120 0 my $next = [ js_sort_keys( grep { !$seen{$_}++ } @$ids ) ];
121 26 ▶ return if js_stringify( $all->{$scope_key} // [] ) eq js_stringify($next);
Branch: 1 branch
BranchTrueFalseTotal
if js_stringify($all->{$scope_key} // []) eq js_stringify($next) 26 0 26
Condition50%1/2
$all->{$scope_key} // []
l!l
026
MC/DC0%0/1
$all->{$scope_key} // []
$all->{$scope_key}
Truth table50%1/2
A
A= $all->{$scope_key}
Aresult
11
00
122 0 ▶ if (@$next) { $all->{$scope_key} = $next }
Branch: 1 branch
BranchTrueFalseTotal
if (@$next) { } 0 0 0
123 0 else { delete $all->{$scope_key} }
124 0 $self->_write_json( 'unreached.json', $all );
125 }
126  
127 #### Sources read (spec §14.3, k80) ####################################
128  
129 20 ▶sub _source_read {
Subroutine _source_read: called CC=3 SCAR=11.3
130 20 my ( $r ) = @_;
131 20 my $out = js_ordered( kind => $r->{kind}, address => $r->{address}, version => $r->{version} );
132 20 ▶ $out->{ref} = $r->{ref} if defined $r->{ref};
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"ref"} 0 20 20
133 20 ▶ $out->{path} = $r->{path} if defined $r->{path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"path"} 0 20 20
134 20 return $out;
135 }
136  
137 # What the last sync of a scope read of each source, and through which backend: `check`
138 # compares a source with it. A malformed entry counts as none.
139 36 ▶sub sources_read {
Subroutine sources_read: called CC=13 SCAR=32.7
Pod documented
140 36 my ( $self, $scope_key ) = @_;
141 36 my $record = $self->_read_json('sources-read.json')->{$scope_key};
142 36 my $out = js_ordered();
143 36 ▶ return $out unless js_type($record) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($record) eq "object" 30 6 36
144 6 for my $name ( keys %$record ) {
145 6 my $v = $record->{$name};
146 6 ▶ next unless js_type($v) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($v) eq "object" 0 6 6
147 6 ▶ next unless js_type( $v->{kind} ) eq 'string' && $v->{kind} =~ /\A(?:git|url|local)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($v->{"kind"}) eq "string" and $v->{"kind"} =~ /\A(?:git|url|local)\z/ 0 6 6
Condition33%1/3
js_type($v->{"kind"}) eq "string" and $v->{"kind"} =~ /\A(?:git|url|local)\z/
!ll&&!rl&&r
006
MC/DC0%0/2
js_type($v->{"kind"}) eq "string" and $v->{"kind"} =~ /\A(?:git|url|local)\z/
js_type($v->{"kind"}) eq "string" $v->{"kind"} =~ /\A(?:git|url|local)\z/
Truth table33%1/3
A and B
A= js_type($v->{"kind"}) eq "string"B= $v->{"kind"} =~ /\A(?:git|url|local)\z/
ABresult
0X0
100
111
148 6 ▶ next unless js_type( $v->{address} ) eq 'string' && js_type( $v->{version} ) eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($v->{"address"}) eq "string" and js_type($v->{"version"}) eq "string" 0 6 6
Condition33%1/3
js_type($v->{"address"}) eq "string" and js_type($v->{"version"}) eq "string"
!ll&&!rl&&r
006
MC/DC0%0/2
js_type($v->{"address"}) eq "string" and js_type($v->{"version"}) eq "string"
js_type($v->{"address"}) eq "string" js_type($v->{"version"}) eq "string"
Truth table33%1/3
A and B
A= js_type($v->{"address"}) eq "string"B= js_type($v->{"version"}) eq "string"
ABresult
0X0
100
111
149 6 ▶ next if exists $v->{ref} && js_type( $v->{ref} ) ne 'string';
Branch: 1 branch
BranchTrueFalseTotal
if exists $v->{"ref"} and js_type($v->{"ref"}) ne "string" 0 6 6
Condition33%1/3
exists $v->{"ref"} and js_type($v->{"ref"}) ne "string"
!ll&&!rl&&r
600
MC/DC0%0/2
exists $v->{"ref"} and js_type($v->{"ref"}) ne "string"
exists $v->{"ref"} js_type($v->{"ref"}) ne "string"
Truth table33%1/3
A and B
A= exists $v->{"ref"}B= js_type($v->{"ref"}) ne "string"
ABresult
0X0
100
111
150 0 ▶ next if exists $v->{path} && !( js_type( $v->{path} ) eq 'array' && !grep { js_type($_) ne 'string' } @{ $v->{path} } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $v->{"path"} and not js_type($v->{"path"}) eq "array" && !grep({js_type($_) ne "string";} @{$$v{"path"};}) 0 6 6
Condition16%1/6
exists $v->{"path"} and not js_type($v->{"path"}) eq "array" && !grep({js_type($_) ne "string";} @{$$v{"path"};})
!ll&&!rl&&r
600
js_type($v->{'path'}) eq 'array' && !grep({js_type($_) ne 'string';} @{$$v{'path'};})
!ll&&!rl&&r
000
MC/DC0%0/3
exists $v->{"path"} and not js_type($v->{"path"}) eq "array" && !grep({js_type($_) ne "string";} @{$$v{"path"};})
exists $v->{"path"} js_type($v->{'path'}) eq 'array' !grep({js_type($_) ne 'string';} @{$$v{'path'};})
Truth table25%1/4
A and not(B && not(C))
A= exists $v->{"path"}B= js_type($v->{'path'}) eq 'array'C= !grep({js_type($_) ne 'string';} @{$$v{'path'};})
ABCresult
0XX0
1110
10X1
1101
151 6 $out->{$name} = _source_read($v);
152 }
153 6 return $out;
154 }
155  
156 26 ▶sub put_sources_read {
Subroutine put_sources_read: called CC=5 SCAR=16.2
Pod documented
157 26 my ( $self, $scope_key, $record ) = @_;
158 26 my $all = $self->_read_json('sources-read.json');
159 26 my $next = js_ordered();
160 26 $next->{$_} = _source_read( $record->{$_} ) for js_sort_keys( keys %$record );
161 26 ▶ return if js_stringify( $all->{$scope_key} // {} ) eq js_stringify($next);
Branch: 1 branch
BranchTrueFalseTotal
if js_stringify($all->{$scope_key} // {}) eq js_stringify($next) 14 12 26
Condition100%2/2
$all->{$scope_key} // {}
l!l
224
MC/DC100%1/1
$all->{$scope_key} // {}
$all->{$scope_key}
Truth table100%2/2
A
A= $all->{$scope_key}
Aresult
11
00
162 12 ▶ if (%$next) { $all->{$scope_key} = $next }
Branch: 1 branch
BranchTrueFalseTotal
if (%$next) { } 12 0 12
163 0 else { delete $all->{$scope_key} }
164 12 $self->_write_json( 'sources-read.json', $all );
165 }
166  
167 #### Render inputs (spec §14.3, k76) ###################################
168  
169 16 ▶sub render_inputs {
Subroutine render_inputs: called CC=2 SCAR=6.9
Pod documented
170 16 my ( $self, $scope_key ) = @_;
171 16 my $hash = $self->_read_json('render-inputs.json')->{$scope_key};
172 16 ▶ return js_type($hash) eq 'string' ? $hash : undef;
Branch: 1 branch
BranchTrueFalseTotal
js_type($hash) eq 'string' ? : 4 12 16
173 }
174  
175 # Replace a scope's render-inputs hash, or drop it (undef); written only when it changes.
176 23 ▶sub put_render_inputs {
Subroutine put_render_inputs: called CC=6 SCAR=18.0
Pod documented
177 23 my ( $self, $scope_key, $hash ) = @_;
178 23 my $all = $self->_read_json('render-inputs.json');
179 23 ▶ if ( defined $hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash) { } 17 6 23
180 17 ▶ return if exists $all->{$scope_key} && js_type( $all->{$scope_key} ) eq 'string' && $all->{$scope_key} eq $hash;
Branch: 1 branch
BranchTrueFalseTotal
if exists $all->{$scope_key} and js_type($all->{$scope_key}) eq "string" and $all->{$scope_key} eq $hash 2 15 17
Condition66%4/6
exists $all->{$scope_key} and js_type($all->{$scope_key}) eq "string" and $all->{$scope_key} eq $hash
!ll&&!rl&&r
1502
exists $all->{$scope_key} and js_type($all->{$scope_key}) eq "string"
!ll&&!rl&&r
1502
MC/DC33%1/3
exists $all->{$scope_key} and js_type($all->{$scope_key}) eq "string" and $all->{$scope_key} eq $hash
exists $all->{$scope_key} js_type($all->{$scope_key}) eq "string" $all->{$scope_key} eq $hash
Truth table50%2/4
A and B and C
A= exists $all->{$scope_key}B= js_type($all->{$scope_key}) eq "string"C= $all->{$scope_key} eq $hash
ABCresult
0XX0
10X0
1100
1111
181 15 $all->{$scope_key} = $hash;
182 }
183 else {
184 6 ▶ return unless exists $all->{$scope_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $all->{$scope_key} 3 3 6
185 3 delete $all->{$scope_key};
186 }
187 18 $self->_write_json( 'render-inputs.json', $all );
188 }
189  
190 #### Hermes home (spec §16.2) ##########################################
191  
192 118 ▶sub hermes_home {
Subroutine hermes_home: called CC=3 SCAR=12.5
Pod documented
193 118 my ( $self ) = @_;
194 118 my $home = $self->_read_json('hermes-home.json')->{home};
195 118 ▶ return ( js_type($home) eq 'string' && length $home ) ? $home : undef;
Branch: 1 branch
BranchTrueFalseTotal
js_type($home) eq 'string' && length $home ? : 0 118 118
Condition33%1/3
js_type($home) eq "string" and length $home
!ll&&!rl&&r
11800
MC/DC0%0/2
js_type($home) eq "string" and length $home
js_type($home) eq "string" length $home
Truth table33%1/3
A and B
A= js_type($home) eq "string"B= length $home
ABresult
0X0
100
111
196 }
197  
198 # Record the Hermes home, or drop the record (undef); written only when it changes.
199 20 ▶sub put_hermes_home {
Subroutine put_hermes_home: called CC=7 SCAR=32.5
Pod documented
200 20 my ( $self, $home ) = @_;
201 20 my $have = $self->hermes_home;
202 20 ▶ return if ( !defined $have && !defined $home ) || ( defined $have && defined $home && $have eq $home );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $have || defined $home or defined $have and defined $home and $have eq $home 20 0 20
Condition16%2/12
not defined $have || defined $home or defined $have and defined $home and $have eq $home
l!l&&r!l&&!r
2000
defined $have || defined $home
l!l&&r!l&&!r
0020
defined $have and defined $home and $have eq $home
!ll&&!rl&&r
000
defined $have and defined $home
!ll&&!rl&&r
000
MC/DC0%0/5
not defined $have || defined $home or defined $have and defined $home and $have eq $home
defined $have defined $home defined $have defined $home $have eq $home
Truth table11%1/9
not(A || B) or C and D and E
A= defined $haveB= defined $homeC= defined $haveD= defined $homeE= $have eq $home
ABCDEresult
00XXX1
1X1111
011111
1X0XX0
1X10X0
1X1100
010XX0
0110X0
011100
203 0 ▶ if ( defined $home ) { $self->_write_json( 'hermes-home.json', js_ordered( home => $home ) ) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $home) { } 0 0 0
204 0 else { unlink Skilletor::FSUtil::encode_text( $self->_file('hermes-home.json') ) }
205 }
206  
207 #### The sync lock (spec §6.5) #########################################
208  
209 35 ▶use constant LOCK_NAME => 'sync.lock';
Subroutine BEGIN: called CC=1 SCAR=0.0
210  
211 # What a lock leaves beside it: a broken lock's tombstone, a released lock on its way out.
212 35 ▶use constant LEFTOVER => LOCK_NAME.'.';
Subroutine BEGIN: called CC=1 SCAR=0.0
213  
214 750 ▶sub _now_ms { int( Time::HiRes::time() * 1000 ) }
Subroutine _now_ms: called CC=1 SCAR=0.0
215  
216 # Run $code holding `sync.lock/`, the one mutex over the locks, the target dirs and the source
217 # cache – shared with the reference, which takes the same directory the same way: mkdir, then
218 # `owner.json` created exclusively; a holder refreshes the record's mtime; a record not
219 # refreshed for the stale age – or for two refreshes when its pid on this host is gone – is a
220 # dead run's, an owner-less dir older than the grace time one that died before writing it.
221 360 ▶sub with_lock {
Subroutine with_lock: called CC=16 SCAR=27.9
Pod documented
222 360 my ( $self, $code, %opts ) = @_;
223 360 ▶ my $timeout_ms = $opts{timeout_ms} // 5_000;
Condition100%2/2
$opts{"timeout_ms"} // 5000
l!l
3546
MC/DC100%1/1
$opts{"timeout_ms"} // 5000
$opts{"timeout_ms"}
Truth table100%2/2
A
A= $opts{"timeout_ms"}
Aresult
11
00
224 360 ▶ my $stale_ms = $opts{stale_ms} // 300_000;
Condition50%1/2
$opts{"stale_ms"} // 300000
l!l
0360
MC/DC0%0/1
$opts{"stale_ms"} // 300000
$opts{"stale_ms"}
Truth table50%1/2
A
A= $opts{"stale_ms"}
Aresult
11
00
225 360 ▶ my $refresh_ms = $opts{refresh_ms} // ( $stale_ms / 3 < 10_000 ? int( $stale_ms / 3 ) : 10_000 );
Branch: 1 branch
BranchTrueFalseTotal
$stale_ms / 3 < 10000 ? : 0 359 359
Condition100%2/2
$opts{"refresh_ms"} // ($stale_ms / 3 < 10000 ? int $stale_ms / 3 : 10000)
l!l
1359
MC/DC100%1/1
$opts{"refresh_ms"} // ($stale_ms / 3 < 10000 ? int $stale_ms / 3 : 10000)
$opts{"refresh_ms"}
Truth table100%2/2
A
A= $opts{"refresh_ms"}
Aresult
11
00
226 360 ▶ $refresh_ms = 1 if $refresh_ms < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $refresh_ms < 1 0 360 360
227 360 ▶ my $grace_ms = $opts{grace_ms} // 2_000;
Condition50%1/2
$opts{"grace_ms"} // 2000
l!l
0360
MC/DC0%0/1
$opts{"grace_ms"} // 2000
$opts{"grace_ms"}
Truth table50%1/2
A
A= $opts{"grace_ms"}
Aresult
11
00
228 360 ▶ my $poll_ms = $opts{poll_ms} // 25;
Condition50%1/2
$opts{"poll_ms"} // 25
l!l
0360
MC/DC0%0/1
$opts{"poll_ms"} // 25
$opts{"poll_ms"}
Truth table50%1/2
A
A= $opts{"poll_ms"}
Aresult
11
00
229 360 my $lock_dir = $self->_file(LOCK_NAME);
230 360 my $owner = path_join( $lock_dir, 'owner.json' );
231 360 my $token = join '', map { sprintf '%02x', int rand 256 } 1 .. 8;
232 360 my $deadline = _now_ms() + $timeout_ms;
233  
234 360 for ( ;; ) {
235 373 ▶ last if $self->_acquire( $lock_dir, $owner, $token, $refresh_ms );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_acquire($lock_dir, $owner, $token, $refresh_ms) 358 15 373
236 15 ▶ if ( my $stale = $self->_judge( $lock_dir, $owner, $stale_ms, $grace_ms ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stale = $self->_judge($lock_dir, $owner, $stale_ms, $grace_ms)) 3 12 15
237 3 ▶ next if $self->_break_lock( $lock_dir, $owner, $stale );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_break_lock($lock_dir, $owner, $stale) 3 0 3
238 }
239 12 ▶ Skilletor::Error::LockTimeout->throw( 'timed out acquiring sync lock at '.$lock_dir ) if _now_ms() >= $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if _now_ms() >= $deadline 2 10 12
240 10 Time::HiRes::sleep( $poll_ms / 1000 );
241 }
242  
243 358 my $heartbeat = $self->_heartbeat( $owner, $refresh_ms );
244 358 my $want = wantarray;
245 358 my @result;
246 358 my $ok = eval {
247 358 $self->_sweep_leftovers($stale_ms);
248 358 ▶ if ($want) { @result = $code->() }
Branch: 2 branches
BranchTrueFalseTotal
if ($want) { } 1 357 358
elsif (defined $want) { } 350 7 357
249 350 elsif ( defined $want ) { $result[0] = $code->() }
250 7 else { $code->() }
251 305 1;
252 };
253 333 my $err = $@;
254 333 $self->_stop_heartbeat($heartbeat);
255 333 $self->_release( $lock_dir, $owner, $token );
256 333 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 28 305 333
257 305 ▶ return $want ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
$want ? : 1 304 305
258 }
259  
260 373 ▶sub _acquire {
Subroutine _acquire: called CC=6 SCAR=22.2
261 373 my ( $self, $lock_dir, $owner, $token, $refresh_ms ) = @_;
262 373 ▶ unless ( mkdir $lock_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $lock_dir) 15 358 373
263 15 ▶ return 0 if $!{EEXIST};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EEXIST"} 15 0 15
264 0 die 'mkdir '.$lock_dir.': '.$!."\n";
265 }
266 358 my $record = js_ordered( pid => 0 + $$, host => Sys::Hostname::hostname(), token => $token, at => _now_ms(), refreshMs => 0 + $refresh_ms );
267 358 my $fh;
268 358 ▶ unless ( sysopen $fh, $owner, Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_EXCL() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh, $owner, 193) 0 358 358
269 0 ▶ return 0 if $!{EEXIST} || $!{ENOENT};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EEXIST"} or $!{"ENOENT"} 0 0 0
Condition0%0/3
$!{"EEXIST"} or $!{"ENOENT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EEXIST"} or $!{"ENOENT"}
$!{"EEXIST"} $!{"ENOENT"}
Truth table0%0/3
A or B
A= $!{"EEXIST"}B= $!{"ENOENT"}
ABresult
1X1
011
000
270 0 my $why = "$!";
271 0 unlink $owner;
272 0 rmdir $lock_dir;
273 0 die 'open '.$owner.': '.$why."\n";
274 }
275 358 binmode $fh;
276 358 print {$fh} Skilletor::FSUtil::encode_text( js_stringify($record) );
277 358 close $fh;
278 358 _touch($owner);
279 358 return 1;
280 }
281  
282 # Is the lock stale? What the waiter saw of it when it is, else undef.
283 15 ▶sub _judge {
Subroutine _judge: called CC=6 SCAR=18.0
284 15 my ( $self, $lock_dir, $owner, $stale_ms, $grace_ms ) = @_;
285 15 my @st = Time::HiRes::stat($owner);
286 15 ▶ unless (@st) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@st) 1 14 15
287 1 ▶ my @dir = Time::HiRes::stat($lock_dir) or return undef; # released meanwhile
Branch: 1 branch
BranchTrueFalseTotal
my(@dir) = &Time::HiRes::stat($lock_dir) or return undef 1 0 1
288 1 ▶ return undef if _now_ms() - $dir[9] * 1000 <= $grace_ms;
Branch: 1 branch
BranchTrueFalseTotal
if _now_ms() - $dir[9] * 1000 <= $grace_ms 0 1 1
289 1 return { ownerless => 1, ino => $dir[1], mtime_ms => $dir[9] * 1000 };
290 }
291 14 my $age = _now_ms() - $st[9] * 1000;
292 14 ▶ return undef if $age <= $stale_ms && !$self->_dead_owner( $owner, $age );
Branch: 1 branch
BranchTrueFalseTotal
if $age <= $stale_ms and not $self->_dead_owner($owner, $age) 12 2 14
Condition100%3/3
$age <= $stale_ms and not $self->_dead_owner($owner, $age)
!ll&&!rl&&r
1112
MC/DC100%2/2
$age <= $stale_ms and not $self->_dead_owner($owner, $age)
$age <= $stale_ms not $self->_dead_owner($owner, $age)
Truth table100%3/3
A and not(B)
A= $age <= $stale_msB= not $self->_dead_owner($owner, $age)
ABresult
0X0
100
111
293 2 return { ownerless => 0, ino => $st[1], mtime_ms => $st[9] * 1000 };
294 }
295  
296 # Did the lock's owner die? Only for a record of this host whose pid is gone and that missed two
297 # of its own refreshes.
298 13 ▶sub _dead_owner {
Subroutine _dead_owner: called CC=13 SCAR=30.6
299 13 my ( $self, $owner, $age ) = @_;
300 13 my $record = eval { js_parse( read_text($owner) ) };
301 13 ▶ return 0 unless defined $record && js_type($record) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record and js_type($record) eq "object" 0 13 13
Condition33%1/3
defined $record and js_type($record) eq "object"
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $record and js_type($record) eq "object"
defined $record js_type($record) eq "object"
Truth table33%1/3
A and B
A= defined $recordB= js_type($record) eq "object"
ABresult
0X0
100
111
302 13 my ( $pid, $host, $refresh ) = @{$record}{qw( pid host refreshMs )};
303 13 ▶ return 0 unless js_type($host) eq 'string' && $host eq Sys::Hostname::hostname();
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($host) eq "string" and $host eq Sys::Hostname::hostname() 0 13 13
Condition33%1/3
js_type($host) eq "string" and $host eq Sys::Hostname::hostname()
!ll&&!rl&&r
0013
MC/DC0%0/2
js_type($host) eq "string" and $host eq Sys::Hostname::hostname()
js_type($host) eq "string" $host eq Sys::Hostname::hostname()
Truth table33%1/3
A and B
A= js_type($host) eq "string"B= $host eq Sys::Hostname::hostname()
ABresult
0X0
100
111
304 13 ▶ return 0 unless js_type($pid) eq 'number' && $pid == int($pid) && $pid > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($pid) eq "number" and $pid == int $pid and $pid > 0 0 13 13
Condition33%2/6
js_type($pid) eq "number" and $pid == int $pid and $pid > 0
!ll&&!rl&&r
0013
js_type($pid) eq "number" and $pid == int $pid
!ll&&!rl&&r
0013
MC/DC0%0/3
js_type($pid) eq "number" and $pid == int $pid and $pid > 0
js_type($pid) eq "number" $pid == int $pid $pid > 0
Truth table25%1/4
A and B and C
A= js_type($pid) eq "number"B= $pid == int $pidC= $pid > 0
ABCresult
0XX0
10X0
1100
1111
305 13 ▶ return 0 unless js_type($refresh) eq 'number' && $refresh > 0 && $age > 2 * $refresh;
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($refresh) eq "number" and $refresh > 0 and $age > 2 * $refresh 7 6 13
Condition50%3/6
js_type($refresh) eq "number" and $refresh > 0 and $age > 2 * $refresh
!ll&&!rl&&r
076
js_type($refresh) eq "number" and $refresh > 0
!ll&&!rl&&r
0013
MC/DC33%1/3
js_type($refresh) eq "number" and $refresh > 0 and $age > 2 * $refresh
js_type($refresh) eq "number" $refresh > 0 $age > 2 * $refresh
Truth table50%2/4
A and B and C
A= js_type($refresh) eq "number"B= $refresh > 0C= $age > 2 * $refresh
ABCresult
0XX0
10X0
1100
1111
306 6 ▶ return 0 if kill 0, $pid;
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pid 5 1 6
307 1 ▶ return $!{ESRCH} ? 1 : 0; # EPERM: alive, another user's
Branch: 1 branch
BranchTrueFalseTotal
$!{'ESRCH'} ? : 1 0 1
308 }
309  
310 # Break a lock judged stale; true when it is gone, so taking it is worth a try at once.
311 3 ▶sub _break_lock {
Subroutine _break_lock: called CC=11 SCAR=39.8
312 3 my ( $self, $lock_dir, $owner, $stale ) = @_;
313 3 ▶ if ( $stale->{ownerless} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stale->{"ownerless"}) 1 2 3
314 1 ▶ return 1 if rmdir $lock_dir;
Branch: 1 branch
BranchTrueFalseTotal
if rmdir $lock_dir 1 0 1
315 0 ▶ return 1 if $!{ENOENT};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOENT"} 0 0 0
316 0 ▶ return 0 if -e $owner; # its run wrote the record meanwhile: live
Branch: 1 branch
BranchTrueFalseTotal
if -e $owner 0 0 0
317 }
318 2 my $tomb = $self->_file( LEFTOVER.'broken-'.$stale->{ino}.'-'.int( $stale->{mtime_ms} ) );
319 2 _touch($lock_dir);
320 2 ▶ return 1 if rename $lock_dir, $tomb;
Branch: 1 branch
BranchTrueFalseTotal
if rename $lock_dir, $tomb 2 0 2
321 0 ▶ return 1 if $!{ENOENT};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOENT"} 0 0 0
322 0 ▶ return 0 if $!{ENOTEMPTY} || $!{EEXIST}; # another waiter broke it
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOTEMPTY"} or $!{"EEXIST"} 0 0 0
Condition0%0/3
$!{"ENOTEMPTY"} or $!{"EEXIST"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"ENOTEMPTY"} or $!{"EEXIST"}
$!{"ENOTEMPTY"} $!{"EEXIST"}
Truth table0%0/3
A or B
A= $!{"ENOTEMPTY"}B= $!{"EEXIST"}
ABresult
1X1
011
000
323 0 ▶ return 0 if $^O eq 'MSWin32' && $!{EACCES};
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" and $!{"EACCES"} 0 0 0
Condition0%0/3
$^O eq "MSWin32" and $!{"EACCES"}
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "MSWin32" and $!{"EACCES"}
$^O eq "MSWin32" $!{"EACCES"}
Truth table0%0/3
A and B
A= $^O eq "MSWin32"B= $!{"EACCES"}
ABresult
0X0
100
111
324 0 die 'rename '.$lock_dir.': '.$!."\n";
325 }
326  
327 # Give the lock up – only this run's own; renamed away first, so no waiter sees it half removed.
328 333 ▶sub _release {
Subroutine _release: called CC=6 SCAR=20.2
329 333 my ( $self, $lock_dir, $owner, $token ) = @_;
330 333 my $record = eval { js_parse( read_text($owner) ) };
331 333 ▶ return unless defined $record && js_type($record) eq 'object' && ( $record->{token} // '' ) eq $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record and js_type($record) eq "object" and ($record->{"token"} // "") eq $token 0 333 333
Condition37%3/8
defined $record and js_type($record) eq "object" and ($record->{"token"} // "") eq $token
!ll&&!rl&&r
00333
defined $record and js_type($record) eq "object"
!ll&&!rl&&r
00333
$record->{"token"} // ""
l!l
3330
MC/DC0%0/3
defined $record and js_type($record) eq "object" and ($record->{"token"} // "") eq $token
defined $record js_type($record) eq "object" $record->{"token"}
Truth table25%1/4
A and B and C
A= defined $recordB= js_type($record) eq "object"C= $record->{"token"}
ABCresult
0XX0
10X0
1100
1111
332 333 my $gone = $self->_file( LEFTOVER.'released-'.$token );
333 333 ▶ rename $lock_dir, $gone or return;
Branch: 1 branch
BranchTrueFalseTotal
rename $lock_dir, $gone or return 333 0 333
334 333 eval { path($gone)->remove_tree( { safe => 0 } ) };
335 }
336  
337 # Remove what earlier locks left beside `sync.lock/` once older than the stale age.
338 358 ▶sub _sweep_leftovers {
Subroutine _sweep_leftovers: called CC=7 SCAR=20.5
339 358 my ( $self, $stale_ms ) = @_;
340 358 ▶ opendir( my $dh, $self->root ) or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->root or return 358 0 358
341 358 my @names = grep { index( $_, LEFTOVER ) == 0 } readdir $dh;
342 358 closedir $dh;
343 358 for my $name (@names) {
344 5 my $p = $self->_file($name);
345 5 ▶ my @st = Time::HiRes::lstat($p) or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@st) = &Time::HiRes::lstat($p) or next 5 0 5
346 5 ▶ next unless _now_ms() - $st[9] * 1000 > $stale_ms;
Branch: 1 branch
BranchTrueFalseTotal
unless _now_ms() - $st[9] * 1000 > $stale_ms 3 2 5
347 0 ▶ if ( -l $p || !-d $p ) { unlink $p }
Branch: 1 branch
BranchTrueFalseTotal
if (-l $p or not -d $p) { } 0 2 2
Condition33%1/3
-l $p or not -d $p
l!l&&r!l&&!r
002
MC/DC0%0/2
-l $p or not -d $p
-l $p not -d $p
Truth table33%1/3
A or not(B)
A= -l $pB= not -d $p
ABresult
1X1
011
000
348 2 else { eval { path($p)->remove_tree( { safe => 0 } ) } }
349 }
350 }
351  
352 360 ▶sub _touch {
Subroutine _touch: called CC=1 SCAR=0.0
353 360 my ( $path ) = @_;
354 360 my $now = Time::HiRes::time();
355 360 Time::HiRes::utime( $now, $now, $path );
356 }
357  
358 # The refreshes: Perl runs a resolve synchronously, so a child process stamps the owner record
359 # while the holder works, and stops when told or when the holder is gone. None on Windows.
360 358 ▶sub _heartbeat {
Subroutine _heartbeat: called CC=6 SCAR=26.5
361 358 my ( $self, $owner, $refresh_ms ) = @_;
362 358 ▶ return undef if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 358 358
363 358 my $parent = $$;
364 358 my $pid = fork;
365 358 ▶ return undef unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 358 358
366 358 ▶ return $pid if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 358 0 358
367 0 ▶ local $SIG{TERM} = sub { POSIX::_exit(0) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
368 0 ▶ while ( getppid() == $parent && -e $owner ) {
Branch: 1 branch
BranchTrueFalseTotal
if (getppid == $parent and -e $owner) 0 0 0
Condition0%0/3
getppid == $parent and -e $owner
!ll&&!rl&&r
000
MC/DC0%0/2
getppid == $parent and -e $owner
getppid == $parent -e $owner
Truth table0%0/3
A and B
A= getppid == $parentB= -e $owner
ABresult
0X0
100
111
369 0 Time::HiRes::sleep( $refresh_ms / 1000 );
370 0 _touch($owner);
371 }
372 0 POSIX::_exit(0);
373 }
374  
375 333 ▶sub _stop_heartbeat {
Subroutine _stop_heartbeat: called CC=2 SCAR=7.0
376 333 my ( $self, $pid ) = @_;
377 333 ▶ return unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 333 333
378 333 kill 'TERM', $pid;
379 333 waitpid $pid, 0;
380 }
381  
382 35 1;
383  
384 __END__
« Skilletor/JSON.pm ↑ Summary Skilletor/URL.pm »