blib/lib/App/Netdisco/Backend/Role/Scheduler.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 148 CC 10 SCAR 31.3
CC
10
Cov
0%
job_specs
56.0
worker_begin
12.0
worker_body
6.0
CRAP 22.8
1 package App::Netdisco::Backend::Role::Scheduler;
2  
3 use Dancer qw/:moose :syntax :script/;
4  
5 use NetAddr::IP;
6 use JSON::PP ();
7 use Algorithm::Cron;
8  
9 use App::Netdisco::Util::MCE;
10 use App::Netdisco::JobQueue qw/jq_insert/;
11  
12 use Role::Tiny;
13 use namespace::clean;
14  
15 sub worker_begin {
16 my $self = shift;
17 my $wid = $self->wid;
18  
19 debug "entering Scheduler ($wid) worker_begin()";
20  
21 foreach my $action (keys %{ setting('schedule') }) {
22 my $config = setting('schedule')->{$action}
23 or next;
24  
25 if (not $config->{when}) {
26 error sprintf 'sch (%s): schedule %s is missing time spec',
27 $wid, $action;
28 next;
29 }
30  
31 # accept either single crontab format, or individual time fields
32 $config->{when} = Algorithm::Cron->new(
33 base => 'local',
34 %{
35 (ref {} eq ref $config->{when})
36 ? $config->{when}
37 : {crontab => $config->{when}}
38 }
39 );
40 }
41 }
42  
43 sub worker_body {
44 my $self = shift;
45 my $wid = $self->wid;
46  
47 while (1) {
48 # sleep until some point in the next minute
49 my $naptime = 60 - (time % 60) + int(rand(45));
50  
51 prctl sprintf 'nd2: #%s sched: idle', $wid;
52 debug "sched ($wid): sleeping for $naptime seconds";
53  
54 sleep $naptime;
55 prctl sprintf 'nd2: #%s sched: queueing', $wid;
56  
57 # NB next_time() returns the next *after* win_start
58 my $win_start = time - (time % 60) - 1;
59 my $win_end = $win_start + 60;
60  
61 # if any job is due, add it to the queue
62 foreach my $action (keys %{ setting('schedule') }) {
63 my $sched = setting('schedule')->{$action} or next;
64 my $real_action = ($sched->{action} || $action);
65  
66 # next occurence of job must be in this minute's window
67 debug sprintf "sched ($wid): $real_action: win_start: %s, win_end: %s, next: %s",
68 $win_start, $win_end, $sched->{when}->next_time($win_start);
69 next unless $sched->{when}->next_time($win_start) <= $win_end;
70  
71 my @job_specs = job_specs($action, $sched);
72  
73 info sprintf 'sched (%s): queueing %s %s jobs',
74 $wid, (scalar @job_specs), $real_action;
75 jq_insert( \@job_specs );
76 }
77 }
78 }
79  
80 # the jobs to queue for one due schedule entry, $action being its key
81 sub job_specs {
82 my ($action, $sched) = @_;
83 my $real_action = ($sched->{action} || $action);
84 my $coder = JSON::PP->new->utf8(0)->allow_nonref(1)->allow_unknown(1);
85  
86 my @job_specs = ();
87  
88 if ($sched->{only} or $sched->{no}) {
89 # without "when": worker_begin made it an Algorithm::Cron
90 # object, which JSON::PP refuses to encode, and the scheduler
91 # died before queueing anything
92 my %entry = (%$sched, label => $action);
93 delete $entry{when};
94 push @job_specs, {
95 action => 'scheduler',
96 subaction => $coder->encode(\%entry),
97 };
98 }
99 else {
100 my @hostlist = ();
101  
102 foreach my $target (ref $sched->{device} eq ref [] ? @{ $sched->{device} }
103 : $sched->{device}) {
104 # sanity checking
105 my $net = NetAddr::IP->new($target);
106 next if ($target
107 and (!$net or $net->num == 0 or $net->addr eq '0.0.0.0'));
108  
109 if (scalar grep {$real_action eq $_} @{ setting('job_targets_prefix') }) {
110 push @hostlist, $target;
111 }
112 else {
113 @hostlist = map { (ref $_) ? $_->addr : undef }
114 (defined $target ? ($net->hostenum) : (undef));
115 }
116 }
117  
118 foreach my $host (@hostlist) {
119 push @job_specs, {
120 action => $real_action,
121 device => $host,
122 port => $sched->{port},
123 subaction => ($sched->{extra} || $sched->{subaction}),
124 };
125 }
126 }
127  
128 return @job_specs;
129 }
130  
131 1;