blib/lib/Langertha/Raider/Hall/Cron.pm

filter: Statementstmt 91.1% 62 / 68 Branchbran 66.6% 12 / 18 Conditioncond 51.6% 16 / 31 MC/DCmcdc 38.8% 7 / 18 Subroutinesub 73.3% 11 / 15 Podpod 0.0% 0 / 3 totaltotal 70.5% 108 / 153 CC 24 SCAR 23.1
CC
24
Cov
77%
start
42.0
_fire
7.1
add_job
6.3
CRAP 10.1
« blib/lib/Langertha/Raider/Hall/Telegram.pm ↑ Summary blib/lib/Langertha/Raider/Hall/ACP.pm »
1 package Langertha::Raider::Hall::Cron;
2 73 our $VERSION = '0.503';
3 # ABSTRACT: Internal non-blocking cron scheduler for the raider hall
4  
5  
6 # Non-blocking cron: for each entry we compute the next execution time via
7 # Schedule::Cron and arm an IO::Async::Timer::Absolute. When it fires we
8 # spawn the raider, then re-arm for the following occurrence. Overlap is
9 # controlled per-entry: by default an occurrence waits in the hall's queues
10 # behind the previous run; opt-in coalesce drops it while the previous run
11 # is still running or waiting.
12  
13 73 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 73 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 73 ▶use Schedule::Cron;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 73 ▶use IO::Async::Timer::Absolute;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 73 has hall => (
19 is => 'ro',
20 isa => 'Langertha::Raider::Hall',
21 required => 1,
22 weak_ref => 1,
23 );
24  
25 has _jobs => (
26 is => 'ro',
27 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 73 );
29  
30 # A single parsing helper — we only use Schedule::Cron to compute the next
31 # time from the expression; we never call its own run loop.
32 10 ▶sub _next_time_for {
Subroutine _next_time_for: called CC=1 SCAR=0.0
33 10 my ($self, $expr) = @_;
34 10 ▶ my $sc = Schedule::Cron->new(sub { }, nofork => 1);
Subroutine __ANON__: not called CC=1 SCAR=0.0
35 10 ▶ my $idx = $sc->add_entry($expr, sub { });
Subroutine __ANON__: not called CC=1 SCAR=0.0
36 10 return $sc->get_next_execution_time($expr);
37 }
38  
39 4 ▶sub add_job {
Subroutine add_job: called CC=6 SCAR=18.5
Pod undocumented
40 4 my ($self, %args) = @_;
41 4 ▶ my $id = $args{id} // die "need id";
Condition50%1/2
$args{"id"} // die("need id")
l!l
40
MC/DC0%0/1
$args{"id"} // die("need id")
$args{"id"}
Truth table50%1/2
A
A= $args{"id"}
Aresult
11
00
42 4 ▶ my $cron_expr = $args{cron} // die "need cron expr";
Condition50%1/2
$args{"cron"} // die("need cron expr")
l!l
40
MC/DC0%0/1
$args{"cron"} // die("need cron expr")
$args{"cron"}
Truth table50%1/2
A
A= $args{"cron"}
Aresult
11
00
43 4 ▶ my $name = $args{name} // die "need name";
Condition50%1/2
$args{"name"} // die("need name")
l!l
40
MC/DC0%0/1
$args{"name"} // die("need name")
$args{"name"}
Truth table50%1/2
A
A= $args{"name"}
Aresult
11
00
44 4 ▶ my $mission = $args{mission} // '';
Condition50%1/2
$args{"mission"} // ""
l!l
40
MC/DC0%0/1
$args{"mission"} // ""
$args{"mission"}
Truth table50%1/2
A
A= $args{"mission"}
Aresult
11
00
45 4 ▶ my $coalesce = $args{coalesce} // 0;
Condition100%2/2
$args{"coalesce"} // 0
l!l
22
MC/DC100%1/1
$args{"coalesce"} // 0
$args{"coalesce"}
Truth table100%2/2
A
A= $args{"coalesce"}
Aresult
11
00
46  
47 4 $self->_jobs->{$id} = {
48 id => $id,
49 cron => $cron_expr,
50 name => $name,
51 mission => $mission,
52 coalesce => $coalesce,
53 };
54 4 $self->_arm($id);
55 4 return $id;
56 }
57  
58 10 ▶sub _arm {
Subroutine _arm: called CC=3 SCAR=11.3
59 10 my ($self, $id) = @_;
60 10 ▶ my $job = $self->_jobs->{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $job = $self->_jobs->{$id} or return 10 0 10
61 10 my $when = eval { $self->_next_time_for($job->{cron}) };
62 10 ▶ return unless $when;
Branch: 1 branch
BranchTrueFalseTotal
unless $when 0 10 10
63  
64 my $timer = IO::Async::Timer::Absolute->new(
65 time => $when,
66 0 ▶ on_expire => sub { $self->_fire($id) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 10 );
68 10 $job->{timer} = $timer;
69 10 $self->hall->loop->add($timer);
70 }
71  
72 # One occurrence of a job: spawn its raider on the job's own session
73 # binding (cron:ID), then re-arm for the next one.
74 8 ▶sub _fire {
Subroutine _fire: called CC=7 SCAR=19.6
75 8 my ($self, $id) = @_;
76 8 my $t = $self->_jobs->{$id};
77 8 ▶ return unless $t; # cancelled
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 8 8
78 8 ▶ if ($t->{coalesce} && $self->_in_flight($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'coalesce'} and $self->_in_flight($id)) { } 2 6 8
Condition100%3/3
$t->{"coalesce"} and $self->_in_flight($id)
!ll&&!rl&&r
332
MC/DC100%2/2
$t->{"coalesce"} and $self->_in_flight($id)
$t->{"coalesce"} $self->_in_flight($id)
Truth table100%3/3
A and B
A= $t->{"coalesce"}B= $self->_in_flight($id)
ABresult
0X0
100
111
79 2 $self->hall->_emit('cron.coalesced', { id => $id, name => $t->{name} });
80 } else {
81 6 my $res = eval { $self->hall->spawn(name => $t->{name}, mission => $t->{mission}, binding => 'cron:'.$id) };
82 6 ▶ $t->{raider_id} = $res && $res->{id};
Condition66%2/3
$res && $res->{'id'}
!ll&&!rl&&r
033
MC/DC50%1/2
$res && $res->{'id'}
$res $res->{'id'}
Truth table66%2/3
A && B
A= $resB= $res->{'id'}
ABresult
0X0
100
111
83 $self->hall->_emit('cron.fired', {
84 id => $id, name => $t->{name},
85 6 ▶ ($res && $res->{id} ? (raider_id => $res->{id}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$res && $res->{'id'} ? : 3 3 6
Condition66%2/3
$res and $res->{"id"}
!ll&&!rl&&r
033
MC/DC50%1/2
$res and $res->{"id"}
$res $res->{"id"}
Truth table66%2/3
A and B
A= $resB= $res->{"id"}
ABresult
0X0
100
111
86 });
87 }
88 8 $self->_arm($id); # re-schedule next occurrence
89 }
90  
91 # Whether the job's previous occurrence has not ended yet, as the hall sees
92 # it: the raider it spawned is still running (also when the binding's
93 # session could not be had and it runs unbound), or a run on the job's
94 # binding is running or waiting in a queue.
95 5 ▶sub _in_flight {
Subroutine _in_flight: called CC=4 SCAR=13.9
96 5 my ($self, $id) = @_;
97 5 my $hall = $self->hall;
98 5 my $raider_id = $self->_jobs->{$id}{raider_id};
99 5 ▶ return 1 if defined $raider_id && $hall->raiders->{$raider_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $raider_id and $hall->raiders->{$raider_id} 1 4 5
Condition100%3/3
defined $raider_id and $hall->raiders->{$raider_id}
!ll&&!rl&&r
311
MC/DC100%2/2
defined $raider_id and $hall->raiders->{$raider_id}
defined $raider_id $hall->raiders->{$raider_id}
Truth table100%3/3
A and B
A= defined $raider_idB= $hall->raiders->{$raider_id}
ABresult
0X0
100
111
100 4 my $binding = 'cron:'.$id;
101 4 ▶ return 1 if $hall->_binding_busy($binding);
Branch: 1 branch
BranchTrueFalseTotal
if $hall->_binding_busy($binding) 0 4 4
102 4 return scalar grep { $_ eq $binding } $hall->_waiting_bindings;
103 }
104  
105 0 ▶sub start {
Subroutine start: not called CC=6 SCAR=37.4
Pod undocumented
106 0 my ($self) = @_;
107 0 my $conf = $self->hall->config;
108 0 ▶ my $cron_list = $conf->{cron} // [];
Condition0%0/2
$conf->{"cron"} // []
l!l
00
MC/DC0%0/1
$conf->{"cron"} // []
$conf->{"cron"}
Truth table0%0/2
A
A= $conf->{"cron"}
Aresult
11
00
109 0 for my $entry (@$cron_list) {
110 $self->add_job(
111 id => $entry->{id} // $entry->{name},
112 cron => $entry->{cron},
113 name => $entry->{name},
114 mission => $entry->{mission} // '',
115 0 ▶ coalesce => $entry->{coalesce} // 0,
Condition0%0/7
$entry->{"id"} // $entry->{"name"}
l!l&&r!l&&!r
000
$entry->{"mission"} // ""
l!l
00
$entry->{"coalesce"} // 0
l!l
00
MC/DC0%0/2
$entry->{"id"} // $entry->{"name"}
$entry->{"id"} $entry->{"name"}
MC/DC0%0/1
$entry->{"mission"} // ""
$entry->{"mission"}
MC/DC0%0/1
$entry->{"coalesce"} // 0
$entry->{"coalesce"}
Truth table0%0/3
A // B
A= $entry->{"id"}B= $entry->{"name"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $entry->{"mission"}
Aresult
11
00
Truth table0%0/2
A
A= $entry->{"coalesce"}
Aresult
11
00
116 );
117 }
118 }
119  
120 3 ▶sub cancel_job {
Subroutine cancel_job: called CC=3 SCAR=11.3
Pod undocumented
121 3 my ($self, $id) = @_;
122 3 ▶ my $job = delete $self->_jobs->{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $job = delete $self->_jobs->{$id} or return 3 0 3
123 3 ▶ if ($job->{timer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{"timer"}) 3 0 3
124 3 eval { $self->hall->loop->remove($job->{timer}) };
125 }
126 }
127  
128 73 __PACKAGE__->meta->make_immutable;
129  
130 73 1;
131  
132 __END__
133  
134 =pod
135  
136 =encoding UTF-8
137  
138 =head1 NAME
139  
140 Langertha::Raider::Hall::Cron - Internal non-blocking cron scheduler for the raider hall
141  
142 =head1 VERSION
143  
144 version 0.503
145  
146 =head1 DESCRIPTION
147  
148 B<Internal module.> Its interface may change without notice.
149  
150 Arms an L<IO::Async::Timer::Absolute> per configured cron entry of a
151 L<Langertha::Raider::Hall> and spawns the named raider when it fires.
152 Every job runs in a session of its own, bound as C<cron:ID>, which each
153 occurrence continues. An occurrence whose previous run has not ended yet
154 waits for it in the hall's queues; with C<coalesce: true> it is dropped
155 instead and the hall emits C<cron.coalesced>.
156  
157 =head1 SUPPORT
158  
159 =head2 Issues
160  
161 Please report bugs and feature requests on GitHub at
162 L<https://github.com/Getty/langertha-raider/issues>.
163  
164 =head2 IRC
165  
166 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
167  
168 =head1 CONTRIBUTING
169  
170 Contributions are welcome! Please fork the repository and submit a pull request.
171  
172 =head1 AUTHOR
173  
174 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
175  
176 =head1 COPYRIGHT AND LICENSE
177  
178 This software is copyright (c) 2026 by Torsten Raudssus.
179  
180 This is free software; you can redistribute it and/or modify it under
181 the same terms as the Perl 5 programming language system itself.
182  
183 =cut
« blib/lib/Langertha/Raider/Hall/Telegram.pm ↑ Summary blib/lib/Langertha/Raider/Hall/ACP.pm »