blib/lib/Langertha/Raid/Loop.pm

filter: Statementstmt 100% 43 / 43 Branchbran 80.0% 8 / 10 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 50.0% 1 / 2 totaltotal 89.7% 61 / 68 CC 9 SCAR 14.1
CC
9
Cov
93%
run_f
7.0
BUILD
2.1
_loop_limit
2.1
CRAP 4.1
« blib/lib/Langertha/Raider/CLI/PromptBuilder.pm ↑ Summary blib/lib/Langertha/Raider/Approval.pm »
1 package Langertha::Raid::Loop;
2 # ABSTRACT: Looping Raid orchestrator
3 1 our $VERSION = '0.503';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Langertha::Raider::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 extends 'Langertha::Raid';
11  
12  
13 1 has max_loops => (
14 is => 'ro',
15 isa => 'Int',
16 default => 1,
17 );
18  
19  
20 1 has max_iterations => (
21 is => 'ro',
22 isa => 'Int',
23 predicate => 'has_max_iterations',
24 );
25  
26  
27 1 has continue_while => (
28 is => 'ro',
29 isa => 'CodeRef',
30 predicate => 'has_continue_while',
31 );
32  
33  
34 6 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.2
Pod undocumented
35 6 my ( $self ) = @_;
36 6 ▶ croak "Loop requires max_loops/max_iterations >= 1"
Branch: 1 branch
BranchTrueFalseTotal
if $self->_loop_limit < 1 0 6 6
37 if $self->_loop_limit < 1;
38 }
39  
40 12 ▶sub _loop_limit {
Subroutine _loop_limit: called CC=2 SCAR=7.2
41 12 my ( $self ) = @_;
42 12 ▶ return $self->has_max_iterations ? $self->max_iterations : $self->max_loops;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_max_iterations ? : 0 12 12
43 }
44  
45 6 ▶async sub run_f {
Subroutine run_f: called CC=7 SCAR=19.5
Pod documented
46 6 my ( $self, $ctx ) = @_;
47 6 $ctx = $self->_coerce_context($ctx);
48  
49 6 my $limit = $self->_loop_limit;
50 6 ▶ my $last_result = Langertha::Raider::Result->final($ctx->input // '');
Condition50%1/2
$ctx->input // ""
l!l
60
MC/DC0%0/1
$ctx->input // ""
$ctx->input
Truth table50%1/2
A
A= $ctx->input
Aresult
11
00
51  
52 6 $ctx->add_trace({
53 node => ref($self),
54 event => 'loop_start',
55 max_loops => $limit,
56 });
57  
58 6 for my $iteration (1..$limit) {
59 10 $ctx->state->{loop_iteration} = $iteration;
60 10 $ctx->add_trace({
61 node => ref($self),
62 event => 'loop_iteration_start',
63 iteration => $iteration,
64 });
65  
66 10 my $result = await $self->_run_steps_sequentially_f(
67 $ctx,
68 loop_iteration => $iteration,
69 );
70  
71 10 ▶ if (!$result->is_final) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->is_final) 3 7 10
72 3 $ctx->metadata->{loop_iterations} = $iteration;
73 3 return $self->_with_context_result($result, $ctx);
74 }
75  
76 7 $last_result = $result;
77  
78 7 ▶ if ($self->has_continue_while) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_continue_while) 2 5 7
79 2 my $continue = $self->continue_while->($ctx, $iteration, $result);
80 2 ▶ last unless $continue;
Branch: 1 branch
BranchTrueFalseTotal
unless $continue 1 1 2
81 }
82 }
83  
84 3 ▶ $ctx->metadata->{loop_iterations} = $ctx->state->{loop_iteration} // 0;
Condition50%1/2
$ctx->state->{"loop_iteration"} // 0
l!l
30
MC/DC0%0/1
$ctx->state->{"loop_iteration"} // 0
$ctx->state->{"loop_iteration"}
Truth table50%1/2
A
A= $ctx->state->{"loop_iteration"}
Aresult
11
00
85 3 return $self->_with_context_result($last_result, $ctx);
86 1 }
87  
88  
89 1 __PACKAGE__->meta->make_immutable;
90  
91 1 1;
92  
93 __END__
94  
95 =pod
96  
97 =encoding UTF-8
98  
99 =head1 NAME
100  
101 Langertha::Raid::Loop - Looping Raid orchestrator
102  
103 =head1 VERSION
104  
105 version 0.503
106  
107 =head1 SYNOPSIS
108  
109 my $raid = Langertha::Raid::Loop->new(
110 steps => [ $worker ],
111 max_loops => 3,
112 );
113  
114 my $result = await $raid->run_f($ctx);
115  
116 =head1 DESCRIPTION
117  
118 Repeats child step execution on orchestration level (not Raider's internal
119 tool loop). The loop stops when:
120  
121 =over 4
122  
123 =item * C<max_loops>/C<max_iterations> is reached
124  
125 =item * a step returns anything but a C<final> result (C<question>,
126 C<pause>, C<abort>, C<cancelled>)
127  
128 =item * optional C<continue_while> callback returns false
129  
130 =back
131  
132 =head2 max_loops
133  
134 Maximum number of loop iterations. Defaults to C<1>.
135  
136 =head2 max_iterations
137  
138 Alias/override for C<max_loops> when explicitly provided.
139  
140 =head2 continue_while
141  
142 Optional callback C<< sub ($ctx, $iteration, $result) >> controlling whether
143 the loop should continue after each final iteration.
144  
145 =head2 run_f
146  
147 my $result = await $raid->run_f($ctx);
148  
149 Executes loop iterations with explicit iteration state and safe stop rules.
150  
151 =head1 SUPPORT
152  
153 =head2 Issues
154  
155 Please report bugs and feature requests on GitHub at
156 L<https://github.com/Getty/langertha-raider/issues>.
157  
158 =head2 IRC
159  
160 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
161  
162 =head1 CONTRIBUTING
163  
164 Contributions are welcome! Please fork the repository and submit a pull request.
165  
166 =head1 AUTHOR
167  
168 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
169  
170 =head1 COPYRIGHT AND LICENSE
171  
172 This software is copyright (c) 2026 by Torsten Raudssus.
173  
174 This is free software; you can redistribute it and/or modify it under
175 the same terms as the Perl 5 programming language system itself.
176  
177 =cut
« blib/lib/Langertha/Raider/CLI/PromptBuilder.pm ↑ Summary blib/lib/Langertha/Raider/Approval.pm »