| 1 | package Langertha::Raid::Loop; | ||||||||||||
| 2 | # ABSTRACT: Looping Raid orchestrator | ||||||||||||
| 3 | 2 | our $VERSION = '0.502'; | |||||||||||
| 4 | 2 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use Carp qw( croak ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 2 | ▶ | use Langertha::Result; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 2 | extends 'Langertha::Raid'; | |||||||||||
| 11 | |||||||||||||
| 12 | |||||||||||||
| 13 | 2 | has max_loops => ( | |||||||||||
| 14 | is => 'ro', | ||||||||||||
| 15 | isa => 'Int', | ||||||||||||
| 16 | default => 1, | ||||||||||||
| 17 | ); | ||||||||||||
| 18 | |||||||||||||
| 19 | |||||||||||||
| 20 | 2 | has max_iterations => ( | |||||||||||
| 21 | is => 'ro', | ||||||||||||
| 22 | isa => 'Int', | ||||||||||||
| 23 | predicate => 'has_max_iterations', | ||||||||||||
| 24 | ); | ||||||||||||
| 25 | |||||||||||||
| 26 | |||||||||||||
| 27 | 2 | has continue_while => ( | |||||||||||
| 28 | is => 'ro', | ||||||||||||
| 29 | isa => 'CodeRef', | ||||||||||||
| 30 | predicate => 'has_continue_while', | ||||||||||||
| 31 | ); | ||||||||||||
| 32 | |||||||||||||
| 33 | |||||||||||||
| 34 | sub BUILD { | ||||||||||||
| 35 | 5 | ▶ | my ( $self ) = @_; | ||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||
| 36 | 5 | ▶ | croak "Loop requires max_loops/max_iterations >= 1" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | if $self->_loop_limit < 1; | ||||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | sub _loop_limit { | ||||||||||||
| 41 | 10 | ▶ | my ( $self ) = @_; | ||||||||||
|
Subroutine
_loop_limit: called CC=2 SCAR=7.2
| |||||||||||||
| 42 | 10 | ▶ | return $self->has_max_iterations ? $self->max_iterations : $self->max_loops; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 5 | ▶ | async sub run_f { | ||||||||||
|
Subroutine
run_f: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 46 | 5 | my ( $self, $ctx ) = @_; | |||||||||||
| 47 | 5 | $ctx = $self->_coerce_context($ctx); | |||||||||||
| 48 | |||||||||||||
| 49 | 5 | my $limit = $self->_loop_limit; | |||||||||||
| 50 | 5 | ▶ | my $last = Langertha::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 | 5 | $ctx->add_trace({ | |||||||||||
| 53 | node => ref($self), | ||||||||||||
| 54 | event => 'loop_start', | ||||||||||||
| 55 | max_loops => $limit, | ||||||||||||
| 56 | }); | ||||||||||||
| 57 | |||||||||||||
| 58 | 5 | for my $iteration (1..$limit) { | |||||||||||
| 59 | 9 | $ctx->state->{loop_iteration} = $iteration; | |||||||||||
| 60 | 9 | $ctx->add_trace({ | |||||||||||
| 61 | node => ref($self), | ||||||||||||
| 62 | event => 'loop_iteration_start', | ||||||||||||
| 63 | iteration => $iteration, | ||||||||||||
| 64 | }); | ||||||||||||
| 65 | |||||||||||||
| 66 | 9 | my $result = await $self->_run_steps_sequentially_f( | |||||||||||
| 67 | $ctx, | ||||||||||||
| 68 | loop_iteration => $iteration, | ||||||||||||
| 69 | ); | ||||||||||||
| 70 | |||||||||||||
| 71 | 9 | ▶ | if (!$result->is_final) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 2 | $ctx->metadata->{loop_iterations} = $iteration; | |||||||||||
| 73 | 2 | return $self->_with_context_result($result, $ctx); | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 7 | $last = $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, $ctx); | |||||||||||
| 86 | 2 | } | |||||||||||
| 87 | |||||||||||||
| 88 | |||||||||||||
| 89 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 90 | |||||||||||||
| 91 | 2 | 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.502 | ||||||||||||
| 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 C<question>, C<pause>, or C<abort> | ||||||||||||
| 126 | |||||||||||||
| 127 | =item * optional C<continue_while> callback returns false | ||||||||||||
| 128 | |||||||||||||
| 129 | =back | ||||||||||||
| 130 | |||||||||||||
| 131 | =head2 max_loops | ||||||||||||
| 132 | |||||||||||||
| 133 | Maximum number of loop iterations (default limit). | ||||||||||||
| 134 | |||||||||||||
| 135 | =head2 max_iterations | ||||||||||||
| 136 | |||||||||||||
| 137 | Alias/override for C<max_loops> when explicitly provided. | ||||||||||||
| 138 | |||||||||||||
| 139 | =head2 continue_while | ||||||||||||
| 140 | |||||||||||||
| 141 | Optional callback C<< sub ($ctx, $iteration, $result) >> controlling whether | ||||||||||||
| 142 | the loop should continue after each final iteration. | ||||||||||||
| 143 | |||||||||||||
| 144 | =head2 run_f | ||||||||||||
| 145 | |||||||||||||
| 146 | my $result = await $raid->run_f($ctx); | ||||||||||||
| 147 | |||||||||||||
| 148 | Executes loop iterations with explicit iteration state and safe stop rules. | ||||||||||||
| 149 | |||||||||||||
| 150 | =head1 SUPPORT | ||||||||||||
| 151 | |||||||||||||
| 152 | =head2 Issues | ||||||||||||
| 153 | |||||||||||||
| 154 | Please report bugs and feature requests on GitHub at | ||||||||||||
| 155 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||
| 156 | |||||||||||||
| 157 | =head2 IRC | ||||||||||||
| 158 | |||||||||||||
| 159 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||
| 160 | |||||||||||||
| 161 | =head1 CONTRIBUTING | ||||||||||||
| 162 | |||||||||||||
| 163 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||
| 164 | |||||||||||||
| 165 | =head1 AUTHOR | ||||||||||||
| 166 | |||||||||||||
| 167 | Torsten Raudssus <getty@cpan.org> | ||||||||||||
| 168 | |||||||||||||
| 169 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 170 | |||||||||||||
| 171 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||
| 172 | |||||||||||||
| 173 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 174 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 175 | |||||||||||||
| 176 | =cut | ||||||||||||