Minion/Util.pm

filter: Statementstmt 98.9% 91 / 92 Branchbran 98.1% 53 / 54 Conditioncond 88.8% 24 / 27 MC/DCmcdc 81.2% 13 / 16 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 96.0% 195 / 203 CC 42 SCAR 20.9
CC
42
Cov
97%
_parse_field
14.0
next_cron_time
10.0
desired_tasks
6.0
CRAP 8.1
« Minion/Job.pm ↑ Summary Minion/Backend/Pg.pm »
1 package Minion::Util;
2 3 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Time::Local qw(timegm);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our @EXPORT_OK = qw(desired_tasks next_cron_time parse_cron);
9  
10 3 my %NICKNAMES = (
11 '@yearly' => '0 0 1 1 *',
12 '@annually' => '0 0 1 1 *',
13 '@monthly' => '0 0 1 * *',
14 '@weekly' => '0 0 * * 0',
15 '@daily' => '0 0 * * *',
16 '@midnight' => '0 0 * * *',
17 '@hourly' => '0 * * * *',
18 );
19  
20 3 my %MONTH_NAMES = (
21 jan => 1,
22 feb => 2,
23 mar => 3,
24 apr => 4,
25 may => 5,
26 jun => 6,
27 jul => 7,
28 aug => 8,
29 sep => 9,
30 oct => 10,
31 nov => 11,
32 dec => 12
33 );
34 3 my %DAY_NAMES = (sun => 0, mon => 1, tue => 2, wed => 3, thu => 4, fri => 5, sat => 6);
35  
36 3 my @FIELDS = (
37 ['minute', 0, 59],
38 ['hour', 0, 23],
39 ['day-of-month', 1, 31],
40 ['month', 1, 12, \%MONTH_NAMES],
41 ['day-of-week', 0, 7, \%DAY_NAMES],
42 );
43  
44 7 ▶sub desired_tasks {
Subroutine desired_tasks: called CC=6 SCAR=17.9
Pod documented
45 7 my ($limits, $available_tasks, $active_tasks) = @_;
46  
47 7 my %count;
48 7 $count{$_}++ for @$active_tasks;
49  
50 7 my @desired;
51 7 for my $task (@$available_tasks) {
52 13 ▶ my $count = $count{$task} // 0;
Condition100%2/2
$count{$task} // 0
l!l
76
MC/DC100%1/1
$count{$task} // 0
$count{$task}
Truth table100%2/2
A
A= $count{$task}
Aresult
11
00
53 13 my $limit = $limits->{$task};
54 13 ▶ push @desired, $task if !defined($limit) || $count < $limit;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $limit or $count < $limit 6 7 13
Condition66%2/3
not defined $limit or $count < $limit
l!l&&r!l&&!r
607
MC/DC50%1/2
not defined $limit or $count < $limit
not defined $limit $count < $limit
Truth table66%2/3
not(A) or B
A= not defined $limitB= $count < $limit
ABresult
1X1
011
000
55 }
56  
57 7 return \@desired;
58 }
59  
60 17 ▶sub next_cron_time {
Subroutine next_cron_time: called CC=10 SCAR=23.0
Pod documented
61 17 my ($cron, $from) = @_;
62 17 ▶ my $parsed = ref $cron ? $cron : parse_cron($cron);
Branch: 1 branch
BranchTrueFalseTotal
ref $cron ? : 2 15 17
63  
64 17 my $t = int($from / 60) * 60 + 60;
65 17 my $limit = $t + 5 * 366 * 86400;
66 17 ▶ while ($t < $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t < $limit) 100 0 100
67 100 my (undef, $min, $hour, $mday, $mon, $year, $wday) = gmtime $t;
68 100 $mon++;
69  
70 # Month
71 100 ▶ if (!$parsed->[3]{set}{$mon}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed->[3]{"set"}{$mon}) 5 95 100
72 5 my $next = _next_value($parsed->[3]{values}, $mon);
73 $t
74 = defined $next
75 ? timegm(0, 0, 0, 1, $next - 1, $year)
76 5 ▶ : timegm(0, 0, 0, 1, $parsed->[3]{values}[0] - 1, $year + 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 1 4 5
77 5 next;
78 }
79  
80 # Day of month / day of week (Vixie OR semantics)
81 95 ▶ if (!_day_match($parsed, $mday, $wday)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_day_match($parsed, $mday, $wday)) 66 29 95
82 66 $t = timegm(0, 0, 0, $mday, $mon - 1, $year) + 86400;
83 66 next;
84 }
85  
86 # Hour
87 29 ▶ if (!$parsed->[1]{set}{$hour}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed->[1]{"set"}{$hour}) 8 21 29
88 8 my $next = _next_value($parsed->[1]{values}, $hour);
89 8 ▶ $t
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 4 4 8
90 = defined $next ? timegm(0, 0, $next, $mday, $mon - 1, $year) : timegm(0, 0, 0, $mday, $mon - 1, $year) + 86400;
91 8 next;
92 }
93  
94 # Minute
95 21 ▶ if (!$parsed->[0]{set}{$min}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed->[0]{"set"}{$min}) 4 17 21
96 4 my $next = _next_value($parsed->[0]{values}, $min);
97 4 ▶ $t
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 3 1 4
98 = defined $next
99 ? timegm(0, $next, $hour, $mday, $mon - 1, $year)
100 : timegm(0, 0, $hour, $mday, $mon - 1, $year) + 3600;
101 4 next;
102 }
103  
104 17 return $t;
105 }
106  
107 0 croak qq{No matching time found for cron expression};
108 }
109  
110 59 ▶sub parse_cron {
Subroutine parse_cron: called CC=5 SCAR=16.1
Pod documented
111 59 ▶ my $expr = shift // '';
Condition50%1/2
shift() // ""
l!l
590
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
112  
113 59 ▶ if ($expr =~ /^@/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^@/u) 11 48 59
114 11 ▶ croak qq{Unknown cron nickname "$expr"} unless exists $NICKNAMES{$expr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $NICKNAMES{$expr} 1 10 11
115 10 $expr = $NICKNAMES{$expr};
116 }
117  
118 58 my @fields = split /\s+/, $expr;
119 58 ▶ croak qq{Invalid cron expression "$expr": expected 5 fields} unless @fields == 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields == 5 2 56 58
120  
121 56 return [map { _parse_field($fields[$_], @{$FIELDS[$_]}) } 0 .. 4];
122 }
123  
124 95 ▶sub _day_match {
Subroutine _day_match: called CC=6 SCAR=17.9
125 95 my ($parsed, $mday, $wday) = @_;
126 95 ▶ return 1 if $parsed->[2]{is_star} && $parsed->[4]{is_star};
Branch: 1 branch
BranchTrueFalseTotal
if $parsed->[2]{"is_star"} and $parsed->[4]{"is_star"} 17 78 95
Condition100%3/3
$parsed->[2]{"is_star"} and $parsed->[4]{"is_star"}
!ll&&!rl&&r
661217
MC/DC100%2/2
$parsed->[2]{"is_star"} and $parsed->[4]{"is_star"}
$parsed->[2]{"is_star"} $parsed->[4]{"is_star"}
Truth table100%3/3
A and B
A= $parsed->[2]{"is_star"}B= $parsed->[4]{"is_star"}
ABresult
0X0
100
111
127 78 ▶ return $parsed->[4]{set}{$wday} if $parsed->[2]{is_star};
Branch: 1 branch
BranchTrueFalseTotal
if $parsed->[2]{"is_star"} 12 66 78
128 66 ▶ return $parsed->[2]{set}{$mday} if $parsed->[4]{is_star};
Branch: 1 branch
BranchTrueFalseTotal
if $parsed->[4]{"is_star"} 60 6 66
129 6 ▶ return $parsed->[2]{set}{$mday} || $parsed->[4]{set}{$wday};
Condition66%2/3
$parsed->[2]{'set'}{$mday} || $parsed->[4]{'set'}{$wday}
l!l&&r!l&&!r
015
MC/DC50%1/2
$parsed->[2]{'set'}{$mday} || $parsed->[4]{'set'}{$wday}
$parsed->[2]{'set'}{$mday} $parsed->[4]{'set'}{$wday}
Truth table66%2/3
A || B
A= $parsed->[2]{'set'}{$mday}B= $parsed->[4]{'set'}{$wday}
ABresult
1X1
011
000
130 }
131  
132 17 ▶sub _next_value {
Subroutine _next_value: called CC=3 SCAR=11.0
133 17 my ($values, $after) = @_;
134 17 ▶ for my $v (@$values) { return $v if $v >= $after }
Branch: 1 branch
BranchTrueFalseTotal
if $v >= $after 8 13 21
135 9 return undef;
136 }
137  
138 254 ▶sub _parse_field {
Subroutine _parse_field: called CC=14 SCAR=26.4
139 254 my ($field, $name, $min, $max, $names) = @_;
140  
141 254 ▶ my $is_star = $field eq '*' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$field eq '*' ? : 139 115 254
142 254 my %set;
143 254 for my $part (split /,/, $field) {
144 258 my ($range, $step) = split m{/}, $part, 2;
145 258 ▶ $step //= 1;
Condition100%2/2
$step //= 1
l!l
7251
MC/DC100%1/1
$step //= 1
$step
Truth table100%2/2
A
A= $step
Aresult
11
00
146 258 ▶ croak qq{Invalid step "$step" in $name field} unless $step =~ /^[1-9]\d*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $step =~ /^[1-9]\d*$/u 1 257 258
147  
148 257 my ($a, $b);
149 257 ▶ if ($range eq '*') { ($a, $b) = ($min, $max) }
Branch: 3 branches
BranchTrueFalseTotal
if ($range eq '*') { } 143 114 257
elsif ($range =~ /^(\w+)-(\w+)$/u) { } 8 106 114
elsif ($range =~ /^(\w+)$/u) { } 105 1 106
150 8 elsif ($range =~ /^(\w+)-(\w+)$/) { ($a, $b) = (_resolve($1, $name, $names), _resolve($2, $name, $names)) }
151 105 elsif ($range =~ /^(\w+)$/) { $a = $b = _resolve($1, $name, $names) }
152 1 else { croak qq{Invalid $name field "$part"} }
153 254 ▶ croak qq{Value out of range in $name field "$part" ($min-$max)} if $a < $min || $b > $max || $a > $b;
Branch: 1 branch
BranchTrueFalseTotal
if $a < $min or $b > $max or $a > $b 6 248 254
Condition100%6/6
$a < $min or $b > $max or $a > $b
l!l&&r!l&&!r
51248
$a < $min or $b > $max
l!l&&r!l&&!r
14249
MC/DC100%3/3
$a < $min or $b > $max or $a > $b
$a < $min $b > $max $a > $b
Truth table100%4/4
A or B or C
A= $a < $minB= $b > $maxC= $a > $b
ABCresult
1XX1
01X1
0011
0000
154  
155 248 ▶ for (my $v = $a; $v <= $b; $v += $step) { $set{$v} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($v <= $b) 3122 248 3370
156 }
157  
158 # Day-of-week 7 is an alias for Sunday (0)
159 244 ▶ $set{0} = 1 if $name eq 'day-of-week' && delete $set{7};
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "day-of-week" and delete $set{7} 39 205 244
Condition100%3/3
$name eq "day-of-week" and delete $set{7}
!ll&&!rl&&r
198739
MC/DC100%2/2
$name eq "day-of-week" and delete $set{7}
$name eq "day-of-week" delete $set{7}
Truth table100%3/3
A and B
A= $name eq "day-of-week"B= delete $set{7}
ABresult
0X0
100
111
160  
161 244 return {set => \%set, values => [sort { $a <=> $b } keys %set], is_star => $is_star};
162 }
163  
164 121 ▶sub _resolve {
Subroutine _resolve: called CC=4 SCAR=13.9
165 121 my ($value, $name, $names) = @_;
166 121 ▶ return $value + 0 if $value =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\d+$/u 109 12 121
167 12 ▶ croak qq{Invalid name "$value" in $name field} unless $names && exists $names->{lc $value};
Branch: 1 branch
BranchTrueFalseTotal
unless $names and exists $$names{lc $value} 2 10 12
Condition100%3/3
$names and exists $$names{lc $value}
!ll&&!rl&&r
1110
MC/DC100%2/2
$names and exists $$names{lc $value}
$names exists $$names{lc $value}
Truth table100%3/3
A and B
A= $namesB= exists $$names{lc $value}
ABresult
0X0
100
111
168 10 return $names->{lc $value};
169 }
170  
171 3 1;
172  
173 =encoding utf8
174  
175 =head1 NAME
176  
177 Minion::Util - Minion utility functions
178  
179 =head1 SYNOPSIS
180  
181 use Minion::Util qw(desired_tasks next_cron_time parse_cron);
182  
183 =head1 DESCRIPTION
184  
185 L<Minion::Util> provides utility functions for L<Minion>.
186  
187 =head1 FUNCTIONS
188  
189 L<Minion::Util> implements the following functions, which can be imported individually.
190  
191 =head2 desired_tasks
192  
193 my $desired_tasks = desired_tasks $limits, $available_tasks, $active_tasks;
194  
195 Enforce limits and generate list of currently desired tasks.
196  
197 # ['bar']
198 desired_tasks {foo => 2}, ['foo', 'bar'], ['foo', 'foo'];
199  
200 =head2 next_cron_time
201  
202 my $epoch = next_cron_time $expr, $from;
203 my $epoch = next_cron_time $parsed, $from;
204  
205 Compute the next epoch time matching a five field cron expression, strictly after the given epoch. All times are
206 interpreted in B<UTC>, not local time. Accepts either a cron expression string or a structure returned by
207 L</"parse_cron">, so the parsed form can be reused across calls without reparsing.
208  
209 # 1747051500 (next 12:05 UTC after 1747051200)
210 next_cron_time '*/5 * * * *', 1747051200;
211  
212 # Reuse the parsed structure
213 my $cron = parse_cron '0 4 * * *';
214 my $next = next_cron_time $cron, time;
215  
216 =head2 parse_cron
217  
218 my $parsed = parse_cron $expr;
219  
220 Parse a five field cron expression into a structure suitable for matching. Croaks on invalid input. The supported
221 syntax covers C<*>, C<*/N>, C<a-b>, C<a-b/N> and comma separated lists for the fields C<minute>, C<hour>,
222 C<day-of-month>, C<month> and C<day-of-week>. The C<month> field also accepts the names C<JAN>-C<DEC>, and the
223 C<day-of-week> field accepts C<SUN>-C<SAT> and treats both C<0> and C<7> as Sunday (names are case-insensitive). When
224 both C<day-of-month> and C<day-of-week> are restricted, jobs run when B<either> matches, following the standard Vixie
225 L<cron(8)> behavior. The nicknames C<@yearly>, C<@annually>, C<@monthly>, C<@weekly>, C<@daily>, C<@midnight> and
226 C<@hourly> expand to the equivalent five field expression.
227  
228 # Every weekday at 9 in the morning
229 parse_cron '0 9 * * MON-FRI';
230  
231 # Every day at midnight
232 parse_cron '@daily';
233  
234 =head1 SEE ALSO
235  
236 L<Minion>, L<Minion::Guide>, L<https://minion.pm>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
237  
238 =cut
« Minion/Job.pm ↑ Summary Minion/Backend/Pg.pm »