blib/lib/App/karr/Foundation/ChainStore.pm

filter: Statementstmt 15.6% 51 / 325 Branchbran 0.0% 0 / 184 Conditioncond 0.0% 0 / 102 MC/DCmcdc 0.0% 0 / 63 Subroutinesub 21.4% 12 / 56 Podpod 100% 17 / 17 totaltotal 10.7% 80 / 747 CC 152 SCAR 58.3
CC
152
Cov
8%
_validate_step
1560.0
parse_chain_document
210.0
validate_chain
132.0
CRAP 340.6
« blib/lib/App/karr/Foundation/Questions.pm ↑ Summary blib/lib/App/karr/Cmd/Metrics.pm »
1 # ABSTRACT: karr-foundation chain and run-log storage under refs/karr-foundation/*
2  
3 package App::karr::Foundation::ChainStore;
4 1 our $VERSION = '0.602';
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use App::karr::Error qw( user_error clean_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use App::karr::Encoding qw( yaml_dump yaml_load json_encode json_decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13  
14 1 has git => (
15 is => 'ro',
16 required => 1,
17 );
18  
19 1 ▶use constant CHAIN_META => 'refs/karr-foundation/chain/meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant STEP_ROOT => 'refs/karr-foundation/chain/step/';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant LOG_ROOT => 'refs/karr-foundation/log/';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # The same cap App::karr::ActivityLog uses, for the same reason (#171): one
24 # append may never cost more than one blob of this size, so the bytes a run
25 # log writes grow linearly with its entries instead of quadratically.
26 1 ▶use constant SEGMENT_MAX_BYTES => 8192;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # Retention. Days is what an operator thinks in; the count is what actually
29 # bounds the namespace, because a fleet that runs ten thousand steps a day
30 # would otherwise keep ten thousand refs inside its two weeks.
31 1 ▶use constant KEEP_DAYS => 14;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use constant KEEP_RUNS => 500;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 my %STEP_KIND = map { $_ => 1 } qw( ticket shell question plan );
35  
36 # pending -> running -> done | failed, plus stale for a step whose precheck no
37 # longer holds. "blocked" is deliberately not a state: whether a step waits on
38 # another is read off the needs edges, and a second, stored copy of that would
39 # be one more thing that can disagree with the graph.
40 1 my %STEP_STATE = map { $_ => 1 } qw( pending running done failed stale );
41  
42 # Keys a step may carry. Anything matching on_* is a policy and passes as well;
43 # what a policy value means is the runner's (#186), not this store's, so the
44 # vocabulary is not enumerated here.
45 1 my %STEP_KEY = map { $_ => 1 } qw(
46 id kind repo ticket needs timeout precheck command note
47 chain state stale_reason stale_at started finished attempts result
48 );
49  
50 0 ▶sub _now { return strftime( '%Y-%m-%dT%H:%M:%SZ', gmtime() ) }
Subroutine _now: not called CC=1 SCAR=6.9
51  
52 # ---------------------------------------------------------------------------
53 # Steps
54 # ---------------------------------------------------------------------------
55  
56 0 ▶sub _step_ref { return STEP_ROOT . $_[1] }
Subroutine _step_ref: not called CC=1 SCAR=6.9
57  
58 # Ids become ref components, so they are checked against something narrower
59 # than git's own grammar: no dots (git forbids '..', a leading dot, a trailing
60 # '.lock'), no slashes (a step id is one component), and a leading alphanumeric.
61 0 ▶sub _valid_id { return defined $_[0] && "$_[0]" =~ /\A[A-Za-z0-9][A-Za-z0-9_-]*\z/ }
Subroutine _valid_id: not called CC=2 SCAR=17.9
Condition0%0/3
defined $_[0] && "$_[0]" =~ /\A[A-Za-z0-9][A-Za-z0-9_-]*\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] && "$_[0]" =~ /\A[A-Za-z0-9][A-Za-z0-9_-]*\z/
defined $_[0] "$_[0]" =~ /\A[A-Za-z0-9][A-Za-z0-9_-]*\z/
Truth table0%0/3
A && B
A= defined $_[0]B= "$_[0]" =~ /\A[A-Za-z0-9][A-Za-z0-9_-]*\z/
ABresult
0X0
100
111
62  
63 0 ▶sub _validate_step {
Subroutine _validate_step: not called CC=39 SCAR=73.5
64 0 my ( $self, $step ) = @_;
65 0 ▶ user_error('A chain step must be a mapping') unless ref $step eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $step eq "HASH" 0 0 0
66 0 my %s = %$step;
67  
68 0 my $id = $s{id};
69 0 ▶ user_error('A chain step needs an id') unless defined $id && length "$id";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length "$id" 0 0 0
Condition0%0/3
defined $id and length "$id"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $id and length "$id"
defined $id length "$id"
Truth table0%0/3
A and B
A= defined $idB= length "$id"
ABresult
0X0
100
111
70 0 ▶ user_error("Chain step id '$id' must be alphanumeric (letters, digits, '_', '-')")
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_id($id) 0 0 0
71 unless _valid_id($id);
72 0 $id = "$id";
73 0 $s{id} = $id;
74  
75 # The dividing line of this whole design, enforced where it is cheapest to
76 # enforce. Ignoring the key would leave a chain that reads as if it routed
77 # work, and a planner that never hears about it keeps writing more.
78 user_error("Chain step '$id' names an agent: the chain is shared state and an "
79 . 'agent is a property of a machine, so routing belongs in the local config')
80 0 ▶ if exists $s{agent} || exists $s{agents};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s{"agent"} or exists $s{"agents"} 0 0 0
Condition0%0/3
exists $s{"agent"} or exists $s{"agents"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $s{"agent"} or exists $s{"agents"}
exists $s{"agent"} exists $s{"agents"}
Truth table0%0/3
A or B
A= exists $s{"agent"}B= exists $s{"agents"}
ABresult
1X1
011
000
81  
82 0 my $kind = $s{kind};
83 0 ▶ user_error("Chain step '$id' has no kind (expected: "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind and length $kind 0 0 0
Condition0%0/3
defined $kind and length $kind
!ll&&!rl&&r
000
MC/DC0%0/2
defined $kind and length $kind
defined $kind length $kind
Truth table0%0/3
A and B
A= defined $kindB= length $kind
ABresult
0X0
100
111
84 . join( ', ', sort keys %STEP_KIND ) . ')')
85 unless defined $kind && length $kind;
86 user_error("Chain step '$id' has unknown kind '$kind' (expected: "
87 . join( ', ', sort keys %STEP_KIND ) . ')')
88 0 ▶ unless $STEP_KIND{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $STEP_KIND{$kind} 0 0 0
89  
90 # A ticket step is an invocation in a repository, so both halves of "which
91 # ticket, where" have to be there; a question or a plan is fleet-wide and
92 # needs neither.
93 0 ▶ if ( $kind eq 'ticket' || $kind eq 'shell' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "ticket" or $kind eq "shell") 0 0 0
Condition0%0/3
$kind eq "ticket" or $kind eq "shell"
l!l&&r!l&&!r
000
MC/DC0%0/2
$kind eq "ticket" or $kind eq "shell"
$kind eq "ticket" $kind eq "shell"
Truth table0%0/3
A or B
A= $kind eq "ticket"B= $kind eq "shell"
ABresult
1X1
011
000
94 user_error("Chain step '$id' ($kind) needs a repo")
95 0 ▶ unless defined $s{repo} && length $s{repo};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s{"repo"} and length $s{"repo"} 0 0 0
Condition0%0/3
defined $s{"repo"} and length $s{"repo"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s{"repo"} and length $s{"repo"}
defined $s{"repo"} length $s{"repo"}
Truth table0%0/3
A and B
A= defined $s{"repo"}B= length $s{"repo"}
ABresult
0X0
100
111
96 }
97 0 ▶ if ( $kind eq 'ticket' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "ticket") 0 0 0
98 user_error("Chain step '$id' (ticket) needs a ticket id")
99 0 ▶ unless defined $s{ticket} && length "$s{ticket}";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s{"ticket"} and length qq[$s{"ticket"}] 0 0 0
Condition0%0/3
defined $s{"ticket"} and length qq[$s{"ticket"}]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s{"ticket"} and length qq[$s{"ticket"}]
defined $s{"ticket"} length qq[$s{"ticket"}]
Truth table0%0/3
A and B
A= defined $s{"ticket"}B= length qq[$s{"ticket"}]
ABresult
0X0
100
111
100 }
101 # And a shell step needs something to run, for the same reason a precheck is
102 # parsed here rather than at execution time: a planning mistake the planner
103 # still hears about beats a step the executor later cannot do anything with.
104 0 ▶ if ( $kind eq 'shell' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "shell") 0 0 0
105 user_error("Chain step '$id' (shell) needs a command")
106 0 ▶ unless defined $s{command} && length "$s{command}";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s{"command"} and length qq[$s{"command"}] 0 0 0
Condition0%0/3
defined $s{"command"} and length qq[$s{"command"}]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s{"command"} and length qq[$s{"command"}]
defined $s{"command"} length qq[$s{"command"}]
Truth table0%0/3
A and B
A= defined $s{"command"}B= length qq[$s{"command"}]
ABresult
0X0
100
111
107 }
108  
109 # YAML writes a one-element list as a scalar often enough that refusing one
110 # would be pedantry; the board's frontmatter accepts both spellings too.
111 0 my $needs = $s{needs};
112 0 ▶ if ( defined $needs && !ref $needs ) { $needs = [ $needs ] }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $needs and not ref $needs) 0 0 0
Condition0%0/3
defined $needs and not ref $needs
!ll&&!rl&&r
000
MC/DC0%0/2
defined $needs and not ref $needs
defined $needs not ref $needs
Truth table0%0/3
A and not(B)
A= defined $needsB= not ref $needs
ABresult
0X0
100
111
113 0 ▶ $needs //= [];
Condition0%0/2
$needs //= []
l!l
00
MC/DC0%0/1
$needs //= []
$needs
Truth table0%0/2
A
A= $needs
Aresult
11
00
114 0 ▶ user_error("Chain step '$id': needs must be a list of step ids")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $needs eq "ARRAY" 0 0 0
115 unless ref $needs eq 'ARRAY';
116 0 my @needs;
117 0 for my $n ( @$needs ) {
118 0 ▶ user_error("Chain step '$id': needs must be a list of step ids")
Branch: 1 branch
BranchTrueFalseTotal
if ref $n or not defined $n 0 0 0
Condition0%0/3
ref $n or not defined $n
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $n or not defined $n
ref $n not defined $n
Truth table0%0/3
A or not(B)
A= ref $nB= not defined $n
ABresult
1X1
011
000
119 if ref $n || !defined $n;
120 0 ▶ user_error("Chain step '$id' needs '$n', which is not a valid step id")
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_id($n) 0 0 0
121 unless _valid_id($n);
122 0 ▶ user_error("Chain step '$id' needs itself") if "$n" eq $id;
Branch: 1 branch
BranchTrueFalseTotal
if "$n" eq $id 0 0 0
123 0 push @needs, "$n";
124 }
125 0 $s{needs} = \@needs;
126  
127 0 ▶ if ( defined $s{timeout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s{"timeout"}) 0 0 0
128 0 ▶ user_error("Chain step '$id': timeout must be a whole number of seconds")
Branch: 1 branch
BranchTrueFalseTotal
unless qq[$s{"timeout"}] =~ /\A[0-9]+\z/ 0 0 0
129 unless "$s{timeout}" =~ /\A[0-9]+\z/;
130 0 $s{timeout} += 0;
131 }
132  
133 # Parsed at write time so a precheck that cannot be read is a planning error
134 # the planner still hears about, rather than a step the runner later cannot
135 # decide about.
136 0 ▶ $self->parse_precheck( $s{precheck}, "Chain step '$id'" ) if defined $s{precheck};
Branch: 1 branch
BranchTrueFalseTotal
if defined $s{"precheck"} 0 0 0
137  
138 0 ▶ my $state = $s{state} // 'pending';
Condition0%0/2
$s{"state"} // "pending"
l!l
00
MC/DC0%0/1
$s{"state"} // "pending"
$s{"state"}
Truth table0%0/2
A
A= $s{"state"}
Aresult
11
00
139 user_error("Chain step '$id' has unknown state '$state' (expected: "
140 . join( ', ', sort keys %STEP_STATE ) . ')')
141 0 ▶ unless $STEP_STATE{$state};
Branch: 1 branch
BranchTrueFalseTotal
unless $STEP_STATE{$state} 0 0 0
142 0 $s{state} = $state;
143  
144 0 for my $key ( sort keys %s ) {
145 0 ▶ next if $STEP_KEY{$key} || $key =~ /\Aon_/;
Branch: 1 branch
BranchTrueFalseTotal
if $STEP_KEY{$key} or $key =~ /\Aon_/ 0 0 0
Condition0%0/3
$STEP_KEY{$key} or $key =~ /\Aon_/
l!l&&r!l&&!r
000
MC/DC0%0/2
$STEP_KEY{$key} or $key =~ /\Aon_/
$STEP_KEY{$key} $key =~ /\Aon_/
Truth table0%0/3
A or B
A= $STEP_KEY{$key}B= $key =~ /\Aon_/
ABresult
1X1
011
000
146 0 warn "karr-foundation: chain step '$id': unknown key '$key'\n";
147 }
148 0 for my $key ( grep { /\Aon_/ } sort keys %s ) {
149 user_error("Chain step '$id': policy '$key' must be a plain value")
150 0 ▶ if ref $s{$key};
Branch: 1 branch
BranchTrueFalseTotal
if ref $s{$key} 0 0 0
151 }
152  
153 0 return \%s;
154 }
155  
156  
157 0 ▶sub write_chain {
Subroutine write_chain: not called CC=5 SCAR=34.0
Pod documented
158 0 my ( $self, $steps, %opt ) = @_;
159 0 my @validated = @{ $self->validate_chain( $steps, %opt ) };
160  
161 0 my $chain_id = _new_chain_id();
162 0 $self->_clear_steps;
163 0 for my $step ( @validated ) {
164 0 $step->{chain} = $chain_id;
165 0 $self->git->write_ref( $self->_step_ref( $step->{id} ), yaml_dump($step) );
166 }
167  
168 my %header = (
169 id => $chain_id,
170 created => _now(),
171 ( defined $opt{limits} ? ( limits => $opt{limits} ) : () ),
172 ( defined $opt{note} ? ( note => $opt{note} ) : () ),
173 0 ▶ ( defined $opt{planner} ? ( planner => $opt{planner} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $opt{'limits'} ? : 0 0 0
defined $opt{'note'} ? : 0 0 0
defined $opt{'planner'} ? : 0 0 0
174 );
175 0 $self->git->write_ref( CHAIN_META, yaml_dump( \%header ) );
176 0 return $chain_id;
177 }
178  
179  
180 0 ▶sub validate_chain {
Subroutine validate_chain: not called CC=11 SCAR=48.8
Pod documented
181 0 my ( $self, $steps, %opt ) = @_;
182 0 ▶ user_error('A chain needs at least one step')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $steps eq "ARRAY" and @$steps 0 0 0
Condition0%0/3
ref $steps eq "ARRAY" and @$steps
!ll&&!rl&&r
000
MC/DC0%0/2
ref $steps eq "ARRAY" and @$steps
ref $steps eq "ARRAY" @$steps
Truth table0%0/3
A and B
A= ref $steps eq "ARRAY"B= @$steps
ABresult
0X0
100
111
183 unless ref $steps eq 'ARRAY' && @$steps;
184  
185 0 my @validated = map { $self->_validate_step($_) } @$steps;
186 0 my %by_id;
187 0 for my $step ( @validated ) {
188 user_error("Chain step id '$step->{id}' is used twice")
189 0 ▶ if $by_id{ $step->{id} }++;
Branch: 1 branch
BranchTrueFalseTotal
if $by_id{$step->{"id"}}++ 0 0 0
190 }
191 0 for my $step ( @validated ) {
192 0 for my $need ( @{ $step->{needs} } ) {
193 user_error("Chain step '$step->{id}' needs '$need', which is not in this chain")
194 0 ▶ unless $by_id{$need};
Branch: 1 branch
BranchTrueFalseTotal
unless $by_id{$need} 0 0 0
195 }
196 }
197 0 _refuse_cycle( \@validated );
198  
199 0 ▶ unless ( $opt{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"force"}) 0 0 0
200 0 ▶ my @running = grep { ( $_->{state} // '' ) eq 'running' } $self->steps;
Condition0%0/2
$_->{"state"} // ""
l!l
00
MC/DC0%0/1
$_->{"state"} // ""
$_->{"state"}
Truth table0%0/2
A
A= $_->{"state"}
Aresult
11
00
201 user_error( 'The chain still has ' . @running . ' running step(s) ('
202 0 ▶ . join( ', ', map { $_->{id} } @running )
Branch: 1 branch
BranchTrueFalseTotal
if @running 0 0 0
203 . '); pass force to replace it anyway' ) if @running;
204 }
205  
206 0 return \@validated;
207 }
208  
209 # What a chain document may say beside its steps. Closed, where the per-step
210 # key list is not: a step's on_* policies are the runner's open vocabulary,
211 # while a document header has exactly these, and an agent that misspells one is
212 # better told than warned -- a chain written with a 'limit:' typo would run at
213 # the wrong concurrency and look perfectly healthy doing it.
214 1 my %DOCUMENT_KEY = map { $_ => 1 } qw( steps limits note planner );
215  
216  
217 0 ▶sub parse_chain_document {
Subroutine parse_chain_document: not called CC=14 SCAR=53.5
Pod documented
218 0 my ( $self, $doc ) = @_;
219 0 ▶ user_error( 'A chain document is a list of steps, or a mapping with a '
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc eq "ARRAY" or ref $doc eq "HASH" 0 0 0
Condition0%0/3
ref $doc eq "ARRAY" or ref $doc eq "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $doc eq "ARRAY" or ref $doc eq "HASH"
ref $doc eq "ARRAY" ref $doc eq "HASH"
Truth table0%0/3
A or B
A= ref $doc eq "ARRAY"B= ref $doc eq "HASH"
ABresult
1X1
011
000
220 . "'steps:' list in it" )
221 unless ref $doc eq 'ARRAY' || ref $doc eq 'HASH';
222  
223 0 ▶ return ( $doc ) if ref $doc eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $doc eq "ARRAY" 0 0 0
224  
225 0 for my $key ( sort keys %$doc ) {
226 0 ▶ next if $DOCUMENT_KEY{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $DOCUMENT_KEY{$key} 0 0 0
227 0 ▶ user_error( "A chain document has no '$key' key (it takes: "
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'force' ? : 0 0 0
228 . join( ', ', sort keys %DOCUMENT_KEY ) . ')'
229 . ( $key eq 'force'
230 ? '; replacing a chain that is still running is --force on the '
231 . 'command line, not something the plan grants itself'
232 : '' ) );
233 }
234  
235 0 my $steps = $doc->{steps};
236 0 ▶ user_error("A chain document needs a 'steps:' list") unless defined $steps;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $steps 0 0 0
237 0 ▶ user_error("A chain document's 'steps:' must be a list of steps")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $steps eq "ARRAY" 0 0 0
238 unless ref $steps eq 'ARRAY';
239  
240 0 my %opt;
241 0 ▶ if ( defined $doc->{limits} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc->{"limits"}) 0 0 0
242 user_error( "A chain document's 'limits:' must be a mapping, for example "
243 0 ▶ . "'concurrent: 4'" ) unless ref $doc->{limits} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc->{"limits"} eq "HASH" 0 0 0
244 0 $opt{limits} = $doc->{limits};
245 }
246 0 for my $key ( qw( note planner ) ) {
247 0 ▶ next unless defined $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} 0 0 0
248 user_error("A chain document's '$key:' must be a plain value")
249 0 ▶ if ref $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if ref $doc->{$key} 0 0 0
250 0 $opt{$key} = $doc->{$key};
251 }
252 0 return ( $steps, %opt );
253 }
254  
255 # Kahn's algorithm, run for its refusal rather than for its order: a chain
256 # whose steps cannot all be peeled off has a cycle, and a cycle is a chain
257 # where nothing ever becomes ready.
258 0 ▶sub _refuse_cycle {
Subroutine _refuse_cycle: not called CC=6 SCAR=37.4
259 0 my ( $steps ) = @_;
260 0 ▶ my %pending = map { $_->{id} => { map { $_ => 1 } @{ $_->{needs} || [] } } } @$steps;
Condition0%0/2
$_->{'needs'} || []
l!l
00
MC/DC0%0/1
$_->{'needs'} || []
$_->{'needs'}
Truth table0%0/2
A
A= $_->{'needs'}
Aresult
11
00
261 0 my $moved = 1;
262 0 ▶ while ( $moved ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($moved) 0 0 0
263 0 $moved = 0;
264 0 for my $id ( sort keys %pending ) {
265 0 ▶ next if grep { $pending{$_} } keys %{ $pending{$id} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$pending{$_};} keys %{$pending{$id};} 0 0 0
266 0 delete $pending{$id};
267 0 $moved = 1;
268 }
269 }
270 0 ▶ user_error( 'The chain has a cycle through step(s) '
Branch: 1 branch
BranchTrueFalseTotal
if %pending 0 0 0
271 . join( ', ', sort keys %pending ) ) if %pending;
272 0 return;
273 }
274  
275 0 ▶sub _new_chain_id {
Subroutine _new_chain_id: not called CC=1 SCAR=6.9
276 0 return strftime( '%Y%m%dT%H%M%SZ', gmtime() ) . '-'
277 . substr( md5_hex( $$, time, rand ), 0, 6 );
278 }
279  
280  
281 0 ▶sub header {
Subroutine header: not called CC=2 SCAR=17.9
Pod documented
282 0 my ( $self ) = @_;
283 0 ▶ return $self->_read_yaml( CHAIN_META, 'chain header' ) // {};
Condition0%0/2
$self->_read_yaml("refs/karr-foundation/chain/meta", "chain header") // {}
l!l
00
MC/DC0%0/1
$self->_read_yaml("refs/karr-foundation/chain/meta", "chain header") // {}
$self->_read_yaml("refs/karr-foundation/chain/meta", "chain header")
Truth table0%0/2
A
A= $self->_read_yaml("refs/karr-foundation/chain/meta", "chain header")
Aresult
11
00
284 }
285  
286  
287 0 ▶sub steps {
Subroutine steps: not called CC=4 SCAR=30.0
Pod documented
288 0 my ( $self ) = @_;
289 0 my @steps;
290 0 for my $ref ( $self->git->list_refs(STEP_ROOT) ) {
291 0 my $id = substr $ref, length STEP_ROOT;
292 0 ▶ next unless _valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_id($id) 0 0 0
293 0 ▶ my $step = $self->_read_yaml( $ref, "chain step '$id'" ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $step = $self->_read_yaml($ref, "chain step '${id}'") or next 0 0 0
294 0 push @steps, _as_step( $step, $id );
295 }
296 # Assigned before it is returned: sort() in scalar context is undefined
297 # behaviour, and a caller asking how many steps there are is ordinary.
298 0 my @sorted = sort { _id_sort_key( $a->{id} ) cmp _id_sort_key( $b->{id} ) } @steps;
299 0 return @sorted;
300 }
301  
302 # What a step ref holds, as the rest of this class expects to see it: the id
303 # filled in from the ref name it was found under, and needs always a list. A
304 # ref written by hand may spell a one-element needs as a scalar, and every
305 # reader here dereferences it -- normalising once on the way in is cheaper than
306 # guarding at each of them.
307 0 ▶sub _as_step {
Subroutine _as_step: not called CC=5 SCAR=34.0
308 0 my ( $step, $id ) = @_;
309 0 ▶ $step->{id} //= "$id";
Condition0%0/2
$step->{"id"} //= "$id"
l!l
00
MC/DC0%0/1
$step->{"id"} //= "$id"
$step->{"id"}
Truth table0%0/2
A
A= $step->{"id"}
Aresult
11
00
310 0 my $needs = $step->{needs};
311 0 ▶ $step->{needs} = ref $needs eq 'ARRAY' ? $needs
Branch: 2 branches
BranchTrueFalseTotal
ref $needs eq 'ARRAY' ? : 0 0 0
defined $needs && length "$needs" ? : 0 0 0
Condition0%0/3
defined $needs and length "$needs"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $needs and length "$needs"
defined $needs length "$needs"
Truth table0%0/3
A and B
A= defined $needsB= length "$needs"
ABresult
0X0
100
111
312 : ( defined $needs && length "$needs" ? [ $needs ] : [] );
313 0 return $step;
314 }
315  
316 # A total order over ids that are usually numbers and occasionally not: pad
317 # the numbers so they sort among themselves numerically, and leave names to
318 # sort after them (digits sort before letters).
319 0 ▶sub _id_sort_key {
Subroutine _id_sort_key: not called CC=3 SCAR=24.8
320 0 my ( $id ) = @_;
321 0 ▶ $id = defined $id ? "$id" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 0 0 0
322 0 ▶ return $id =~ /\A[0-9]+\z/ ? sprintf( '%020d', $id ) : "~$id";
Branch: 1 branch
BranchTrueFalseTotal
$id =~ /\A[0-9]+\z/ ? : 0 0 0
323 }
324  
325  
326 0 ▶sub step {
Subroutine step: not called CC=3 SCAR=24.8
Pod documented
327 0 my ( $self, $id ) = @_;
328 0 ▶ return undef unless _valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_id($id) 0 0 0
329 0 ▶ my $step = $self->_read_yaml( $self->_step_ref($id), "chain step '$id'" )
Branch: 1 branch
BranchTrueFalseTotal
my $step = $self->_read_yaml($self->_step_ref($id), "chain step '${id}'") or return undef 0 0 0
330 or return undef;
331 0 return _as_step( $step, $id );
332 }
333  
334  
335 0 ▶sub update_step {
Subroutine update_step: not called CC=2 SCAR=17.9
Pod documented
336 0 my ( $self, $id, $code ) = @_;
337 0 ▶ return undef unless _valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_id($id) 0 0 0
338 0 my $ref = $self->_step_ref($id);
339  
340 # retry_contended reads an empty return as "lost the race, read again"; a
341 # one-element (undef) return is a committed answer and comes back as undef.
342 0 ▶ return scalar $self->git->retry_contended( "chain step $id", sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
343 0 my ( $oid, $content ) = $self->git->read_ref_with_oid($ref);
344 0 ▶ return undef unless defined $oid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $oid 0 0 0
345 0 ▶ my $step = $self->_decode_yaml( $content, "chain step '$id'" ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $step = $self->_decode_yaml($content, "chain step '${id}'") or return undef 0 0 0
346 0 $step = _as_step( $step, $id );
347  
348 0 my $updated = $code->($step);
349 0 ▶ return undef unless ref $updated eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $updated eq "HASH" 0 0 0
350 0 my $valid = $self->_validate_step($updated);
351 user_error("Chain step '$id' cannot be renamed to '$valid->{id}'")
352 0 ▶ unless $valid->{id} eq "$id";
Branch: 1 branch
BranchTrueFalseTotal
unless $valid->{"id"} eq "$id" 0 0 0
353  
354 0 ▶ return $self->git->write_ref_cas( $ref, yaml_dump($valid), $oid )
Branch: 1 branch
BranchTrueFalseTotal
$self->git->write_ref_cas($ref, yaml_dump($valid), $oid) ? : 0 0 0
355 ? $valid : ();
356 0 } );
357 }
358  
359  
360 0 ▶sub mark_stale {
Subroutine mark_stale: not called CC=1 SCAR=6.9
Pod documented
361 0 my ( $self, $id, $reason ) = @_;
362 0 ▶ return $self->update_step( $id, sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
363 0 my ( $step ) = @_;
364 0 ▶ return undef if ( $step->{state} // 'pending' ) eq 'stale';
Branch: 1 branch
BranchTrueFalseTotal
if ($step->{"state"} // "pending") eq "stale" 0 0 0
Condition0%0/2
$step->{"state"} // "pending"
l!l
00
MC/DC0%0/1
$step->{"state"} // "pending"
$step->{"state"}
Truth table0%0/2
A
A= $step->{"state"}
Aresult
11
00
365 0 $step->{state} = 'stale';
366 0 $step->{stale_at} = _now();
367 0 ▶ if ( defined $reason && length $reason ) { $step->{stale_reason} = $reason }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason and length $reason) { } 0 0 0
Condition0%0/3
defined $reason and length $reason
!ll&&!rl&&r
000
MC/DC0%0/2
defined $reason and length $reason
defined $reason length $reason
Truth table0%0/3
A and B
A= defined $reasonB= length $reason
ABresult
0X0
100
111
368 0 else { delete $step->{stale_reason} }
369 0 return $step;
370 0 } );
371 }
372  
373  
374 0 ▶sub ready_steps {
Subroutine ready_steps: not called CC=7 SCAR=40.3
Pod documented
375 0 my ( $self ) = @_;
376 0 my $chain = $self->header->{id};
377 0 ▶ return () unless defined $chain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chain 0 0 0
378  
379 0 ▶ my @steps = grep { ( $_->{chain} // '' ) eq $chain } $self->steps;
Condition0%0/2
$_->{"chain"} // ""
l!l
00
MC/DC0%0/1
$_->{"chain"} // ""
$_->{"chain"}
Truth table0%0/2
A
A= $_->{"chain"}
Aresult
11
00
380 0 ▶ my %state = map { $_->{id} => ( $_->{state} // 'pending' ) } @steps;
Condition0%0/2
$_->{"state"} // "pending"
l!l
00
MC/DC0%0/1
$_->{"state"} // "pending"
$_->{"state"}
Truth table0%0/2
A
A= $_->{"state"}
Aresult
11
00
381 return grep {
382 0 my $step = $_;
383 ( $state{ $step->{id} } eq 'pending' )
384 0 ▶ && !grep { ( $state{$_} // '' ) ne 'done' } @{ $step->{needs} || [] };
Condition0%0/7
$state{$step->{'id'}} eq 'pending' && !grep({($state{$_} // '') ne 'done';} @{$step->{'needs'} or [];})
!ll&&!rl&&r
000
$state{$_} // ""
l!l
00
$step->{'needs'} || []
l!l
00
MC/DC0%0/2
$state{$step->{'id'}} eq 'pending' && !grep({($state{$_} // '') ne 'done';} @{$step->{'needs'} or [];})
$state{$step->{'id'}} eq 'pending' !grep({($state{$_} // '') ne 'done';} @{$step->{'needs'} or [];})
MC/DC0%0/1
$state{$_} // ""
$state{$_}
MC/DC0%0/1
$step->{'needs'} || []
$step->{'needs'}
Truth table0%0/3
A && not(B)
A= $state{$step->{'id'}} eq 'pending'B= !grep({($state{$_} // '') ne 'done';} @{$step->{'needs'} or [];})
ABresult
0X0
100
111
Truth table0%0/2
A
A= $state{$_}
Aresult
11
00
Truth table0%0/2
A
A= $step->{'needs'}
Aresult
11
00
385 } @steps;
386 }
387  
388  
389 0 ▶sub clear_chain {
Subroutine clear_chain: not called CC=2 SCAR=17.9
Pod documented
390 0 my ( $self ) = @_;
391 0 ▶ my $removed = $self->git->delete_ref(CHAIN_META) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->git->delete_ref('refs/karr-foundation/chain/meta') ? : 0 0 0
392 0 return $removed + $self->_clear_steps;
393 }
394  
395 0 ▶sub _clear_steps {
Subroutine _clear_steps: not called CC=3 SCAR=24.8
396 0 my ( $self ) = @_;
397 0 my $removed = 0;
398 0 for my $ref ( $self->git->list_refs(STEP_ROOT) ) {
399 0 ▶ $removed += $self->git->delete_ref($ref) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->git->delete_ref($ref) ? : 0 0 0
400 }
401 0 return $removed;
402 }
403  
404 # ---------------------------------------------------------------------------
405 # Prechecks
406 # ---------------------------------------------------------------------------
407  
408  
409 0 ▶sub parse_precheck {
Subroutine parse_precheck: not called CC=6 SCAR=37.4
Pod documented
410 0 my ( $self, $expr, $what ) = @_;
411 0 ▶ return undef unless defined $expr && $expr =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr and $expr =~ /\S/ 0 0 0
Condition0%0/3
defined $expr and $expr =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $expr and $expr =~ /\S/
defined $expr $expr =~ /\S/
Truth table0%0/3
A and B
A= defined $exprB= $expr =~ /\S/
ABresult
0X0
100
111
412 0 ▶ $what ||= 'Precheck';
Condition0%0/2
$what ||= "Precheck"
l!l
00
MC/DC0%0/1
$what ||= "Precheck"
$what
Truth table0%0/2
A
A= $what
Aresult
11
00
413 0 ▶ user_error("$what: a precheck must be an expression, not a structure")
Branch: 1 branch
BranchTrueFalseTotal
if ref $expr 0 0 0
414 if ref $expr;
415  
416 0 ▶ my ( $fact, $op, $value ) =
Branch: 1 branch
BranchTrueFalseTotal
() = "$expr" =~ /\A\s*([A-Za-z_][A-Za-z0-9_.]*)\s*(==|!=)\s*(.+?)\s*\z/ or user_error("${what}: cannot read precheck '${expr}' " . '(expected: <fact> == <value>, or !=)') 0 0 0
417 "$expr" =~ /\A\s*([A-Za-z_][A-Za-z0-9_.]*)\s*(==|!=)\s*(.+?)\s*\z/
418 or user_error("$what: cannot read precheck '$expr' "
419 . '(expected: <fact> == <value>, or !=)');
420 0 $value =~ s/\A(['"])(.*)\1\z/$2/s;
421 0 return { fact => $fact, op => $op, value => $value };
422 }
423  
424  
425 0 ▶sub precheck_holds {
Subroutine precheck_holds: not called CC=8 SCAR=42.8
Pod documented
426 0 my ( $self, $step, $facts ) = @_;
427 0 ▶ my $p = $self->parse_precheck( ref $step eq 'HASH' ? $step->{precheck} : $step )
Branch: 2 branches
BranchTrueFalseTotal
my $p = $self->parse_precheck(ref $step eq "HASH" ? $step->{"precheck"} : $step) or return 1 0 0 0
ref $step eq 'HASH' ? : 0 0 0
428 or return 1;
429 0 ▶ my $have = ref $facts eq 'HASH' ? $facts->{ $p->{fact} } : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $facts eq 'HASH' ? : 0 0 0
430 0 ▶ return 0 unless defined $have;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $have 0 0 0
431 return $p->{op} eq '==' ? ( "$have" eq $p->{value} ? 1 : 0 )
432 0 ▶ : ( "$have" ne $p->{value} ? 1 : 0 );
Branch: 3 branches
BranchTrueFalseTotal
$p->{'op'} eq '==' ? : 0 0 0
"$have" eq $p->{'value'} ? : 0 0 0
"$have" ne $p->{'value'} ? : 0 0 0
433 }
434  
435 # ---------------------------------------------------------------------------
436 # Run logs
437 # ---------------------------------------------------------------------------
438  
439  
440 has segment_max_bytes => (
441 is => 'ro',
442 0 ▶ default => sub { SEGMENT_MAX_BYTES },
Subroutine __ANON__: not called CC=1 SCAR=6.9
443 1 );
444  
445  
446 has keep_days => (
447 is => 'ro',
448 0 ▶ default => sub { KEEP_DAYS },
Subroutine __ANON__: not called CC=1 SCAR=6.9
449 1 );
450  
451  
452 has keep_runs => (
453 is => 'ro',
454 0 ▶ default => sub { KEEP_RUNS },
Subroutine __ANON__: not called CC=1 SCAR=6.9
455 1 );
456  
457  
458 has auto_prune => (
459 is => 'ro',
460 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
461 1 );
462  
463  
464 0 ▶sub new_run_id {
Subroutine new_run_id: not called CC=1 SCAR=6.9
Pod documented
465 0 my ( $self ) = @_;
466 # One gmtime call, not two: a run minted in the last second of a day would
467 # otherwise take its date from before midnight and its time from after.
468 0 return strftime( '%Y-%m-%d-%H%M%S', gmtime() )
469 . substr( md5_hex( $$, time, rand ), 0, 6 );
470 }
471  
472 0 ▶sub _valid_run {
Subroutine _valid_run: not called CC=2 SCAR=17.9
473 0 my ( $run ) = @_;
474 0 ▶ return defined $run && "$run" =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9A-Za-z_]+\z/;
Condition0%0/3
defined $run && "$run" =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9A-Za-z_]+\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $run && "$run" =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9A-Za-z_]+\z/
defined $run "$run" =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9A-Za-z_]+\z/
Truth table0%0/3
A && B
A= defined $runB= "$run" =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9A-Za-z_]+\z/
ABresult
0X0
100
111
475 }
476  
477 0 ▶sub _run_base { return LOG_ROOT . $_[1] }
Subroutine _run_base: not called CC=1 SCAR=6.9
478  
479 # The segment refs of one run, oldest first. The glob is the base name, so it
480 # also catches a run whose name merely starts with this one's; the anchored
481 # regex is what makes the match exact.
482 0 ▶sub _run_segments {
Subroutine _run_segments: not called CC=4 SCAR=30.0
483 0 my ( $self, $run ) = @_;
484 0 my $base = $self->_run_base($run);
485 0 my %index;
486 0 for my $ref ( $self->git->list_refs($base) ) {
487 0 ▶ $index{$ref} = ( $1 // 0 ) + 0 if $ref =~ /\A\Q$base\E(?:\+([0-9]+))?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $ref =~ /\A\Q$base\E(?:\+([0-9]+))?\z/ 0 0 0
Condition0%0/2
$1 // 0
l!l
00
MC/DC0%0/1
$1 // 0
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
488 }
489 0 my @sorted = sort { $index{$a} <=> $index{$b} } keys %index;
490 0 return @sorted;
491 }
492  
493  
494 0 ▶sub log_run {
Subroutine log_run: not called CC=5 SCAR=34.0
Pod documented
495 0 my ( $self, $run, %entry ) = @_;
496 0 ▶ user_error("'$run' is not a run name (expected <date>-<id>)")
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_run($run) 0 0 0
497 unless _valid_run($run);
498 0 my $base = $self->_run_base($run);
499  
500 0 ▶ $self->prune_logs if $self->auto_prune && !$self->git->ref_exists($base);
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_prune and not $self->git->ref_exists($base) 0 0 0
Condition0%0/3
$self->auto_prune and not $self->git->ref_exists($base)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->auto_prune and not $self->git->ref_exists($base)
$self->auto_prune not $self->git->ref_exists($base)
Truth table0%0/3
A and not(B)
A= $self->auto_pruneB= not $self->git->ref_exists($base)
ABresult
0X0
100
111
501  
502 0 ▶ $entry{ts} //= _now();
Condition0%0/3
$entry{"ts"} //= _now()
l!l&&r!l&&!r
000
MC/DC0%0/2
$entry{"ts"} //= _now()
$entry{"ts"} _now()
Truth table0%0/3
A //= B
A= $entry{"ts"}B= _now()
ABresult
1X1
011
000
503 0 my $line = json_encode( \%entry );
504  
505 0 ▶ return try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
506 0 ▶ $self->git->retry_contended( "run log $run", sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
507 0 my @segments = $self->_run_segments($run);
508 0 ▶ my $segment = @segments ? $segments[-1] : $base;
Branch: 1 branch
BranchTrueFalseTotal
@segments ? : 0 0 0
509 0 ▶ my ( $oid, $content ) = @segments
Branch: 1 branch
BranchTrueFalseTotal
@segments ? : 0 0 0
510 ? $self->git->read_ref_with_oid($segment) : ( undef, '' );
511 0 ▶ $content //= '';
Condition0%0/2
$content //= ""
l!l
00
MC/DC0%0/1
$content //= ""
$content
Truth table0%0/2
A
A= $content
Aresult
11
00
512  
513 # Rotate before the append, never after: no segment is written past the
514 # cap, no entry is written twice, and a full segment is left alone from
515 # then on. The next segment is opened with expected_old => undef, so two
516 # writers rotating at once cannot clobber one another -- the loser
517 # re-reads and appends to the segment the winner opened.
518 0 ▶ if ( length($content)
Branch: 1 branch
BranchTrueFalseTotal
if (length $content and length($content) + 1 + length($line) > $self->segment_max_bytes) 0 0 0
Condition0%0/3
length $content and length($content) + 1 + length($line) > $self->segment_max_bytes
!ll&&!rl&&r
000
MC/DC0%0/2
length $content and length($content) + 1 + length($line) > $self->segment_max_bytes
length $content length($content) + 1 + length($line) > $self->segment_max_bytes
Truth table0%0/3
A and B
A= length $contentB= length($content) + 1 + length($line) > $self->segment_max_bytes
ABresult
0X0
100
111
519 && length($content) + 1 + length($line) > $self->segment_max_bytes )
520 {
521 0 my ( $index ) = $segment =~ /\+([0-9]+)\z/;
522 0 ▶ $segment = sprintf( '%s+%06d', $base, ( $index // 0 ) + 1 );
Condition0%0/2
$index // 0
l!l
00
MC/DC0%0/1
$index // 0
$index
Truth table0%0/2
A
A= $index
Aresult
11
00
523 0 ( $oid, $content ) = ( undef, '' );
524 }
525  
526 0 ▶ my $new = length $content ? "$content\n$line" : $line;
Branch: 1 branch
BranchTrueFalseTotal
length $content ? : 0 0 0
527 0 ▶ return $self->git->write_ref_cas( $segment, $new, $oid ) ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->git->write_ref_cas($segment, $new, $oid) ? : 0 0 0
528 0 } );
529 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
530 0 warn "karr-foundation: run log write to '$base' failed: " . clean_error($_) . "\n";
531 0 0;
532 0 };
533 }
534  
535  
536 0 ▶sub run_ids {
Subroutine run_ids: not called CC=3 SCAR=24.8
Pod documented
537 0 my ( $self ) = @_;
538 0 my %run;
539 0 for my $ref ( $self->git->list_refs(LOG_ROOT) ) {
540 0 my $name = substr $ref, length LOG_ROOT;
541 0 $name =~ s/\+[0-9]+\z//;
542 0 ▶ $run{$name} = 1 if _valid_run($name);
Branch: 1 branch
BranchTrueFalseTotal
if _valid_run($name) 0 0 0
543 }
544 0 my @runs = sort keys %run;
545 0 return @runs;
546 }
547  
548  
549 0 ▶sub run_entries {
Subroutine run_entries: not called CC=8 SCAR=42.8
Pod documented
550 0 my ( $self, $run ) = @_;
551 0 ▶ return () unless _valid_run($run);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_run($run) 0 0 0
552 0 my @entries;
553 0 for my $ref ( $self->_run_segments($run) ) {
554 0 my $content = $self->git->read_ref($ref);
555 0 ▶ next unless defined $content && length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 0 0 0
Condition0%0/3
defined $content and length $content
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table0%0/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
556 0 for my $line ( split /\n/, $content ) {
557 0 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 0 0
558 0 ▶ my $decoded = try { json_decode($line) } catch { undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
559 0 ▶ push @entries, $decoded if $decoded;
Branch: 1 branch
BranchTrueFalseTotal
if $decoded 0 0 0
560 }
561 }
562 0 return @entries;
563 }
564  
565  
566 0 ▶sub prune_logs {
Subroutine prune_logs: not called CC=10 SCAR=47.0
Pod documented
567 0 my ( $self, %opt ) = @_;
568 0 ▶ my $keep_days = defined $opt{keep_days} ? $opt{keep_days} : $self->keep_days;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'keep_days'} ? : 0 0 0
569 0 ▶ my $keep_runs = defined $opt{keep_runs} ? $opt{keep_runs} : $self->keep_runs;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'keep_runs'} ? : 0 0 0
570  
571 0 my @runs = $self->run_ids;
572 0 my %doomed;
573 0 ▶ if ( $keep_days ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_days) 0 0 0
574 0 my $cutoff = strftime( '%Y-%m-%d', gmtime( time - $keep_days * 86400 ) );
575 0 $doomed{$_} = 1 for grep { substr( $_, 0, 10 ) lt $cutoff } @runs;
576 }
577 0 ▶ if ( $keep_runs && @runs > $keep_runs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_runs and @runs > $keep_runs) 0 0 0
Condition0%0/3
$keep_runs and @runs > $keep_runs
!ll&&!rl&&r
000
MC/DC0%0/2
$keep_runs and @runs > $keep_runs
$keep_runs @runs > $keep_runs
Truth table0%0/3
A and B
A= $keep_runsB= @runs > $keep_runs
ABresult
0X0
100
111
578 0 $doomed{$_} = 1 for @runs[ 0 .. $#runs - $keep_runs ];
579 }
580  
581 0 my @gone;
582 0 for my $run ( sort keys %doomed ) {
583 0 $self->git->delete_ref($_) for $self->_run_segments($run);
584 0 push @gone, $run;
585 }
586 0 return @gone;
587 }
588  
589 # ---------------------------------------------------------------------------
590 # Reading refs
591 # ---------------------------------------------------------------------------
592  
593 0 ▶sub _read_yaml {
Subroutine _read_yaml: not called CC=3 SCAR=24.8
594 0 my ( $self, $ref, $what ) = @_;
595 0 my $content = $self->git->read_ref($ref);
596 0 ▶ return undef unless defined $content && length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 0 0 0
Condition0%0/3
defined $content and length $content
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table0%0/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
597 0 return $self->_decode_yaml( $content, $what );
598 }
599  
600 # A ref that does not parse is skipped with a warning rather than dying: one
601 # hand-edited step must not make the whole chain unreadable, and the runner's
602 # answer to a step it cannot see is to leave it alone.
603 0 ▶sub _decode_yaml {
Subroutine _decode_yaml: not called CC=2 SCAR=17.9
604 0 my ( $self, $content, $what ) = @_;
605 0 ▶ my $data = try { yaml_load($content) } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
606 0 warn "karr-foundation: cannot read $what: " . clean_error($_) . "\n";
607 0 undef;
608 0 };
609 0 ▶ return ref $data eq 'HASH' ? $data : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 0 0 0
610 }
611  
612 1 1;
613  
614 __END__
« blib/lib/App/karr/Foundation/Questions.pm ↑ Summary blib/lib/App/karr/Cmd/Metrics.pm »