| 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
| |||||||||||||
| 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
| |||||||||||||
| 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 // ""
MC/DC0%0/1
$ctx->input // ""
$ctx->input
Truth table50%1/2
A
A= $ctx->input
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 79 | 2 | my $continue = $self->continue_while->($ctx, $iteration, $result); | |||||||||||
| 80 | 2 | ▶ | last unless $continue; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | } | ||||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 3 | ▶ | $ctx->metadata->{loop_iterations} = $ctx->state->{loop_iteration} // 0; | ||||||||||
|
Condition50%1/2
$ctx->state->{"loop_iteration"} // 0
MC/DC0%0/1
$ctx->state->{"loop_iteration"} // 0
$ctx->state->{"loop_iteration"}
Truth table50%1/2
A
A= $ctx->state->{"loop_iteration"}
| |||||||||||||
| 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 | ||||||||||||