« blib/lib/App/Netdisco/Worker/Plugin/Arpnip/Subnets.pm
↑ Summary
blib/lib/App/Netdisco/Transport/SSH.pm »
| 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 | my $coder = JSON::PP->new->utf8(0)->allow_nonref(1)->allow_unknown(1); | ||
| 47 | |||
| 48 | while (1) { | ||
| 49 | # sleep until some point in the next minute | ||
| 50 | my $naptime = 60 - (time % 60) + int(rand(45)); | ||
| 51 | |||
| 52 | prctl sprintf 'nd2: #%s sched: idle', $wid; | ||
| 53 | debug "sched ($wid): sleeping for $naptime seconds"; | ||
| 54 | |||
| 55 | sleep $naptime; | ||
| 56 | prctl sprintf 'nd2: #%s sched: queueing', $wid; | ||
| 57 | |||
| 58 | # NB next_time() returns the next *after* win_start | ||
| 59 | my $win_start = time - (time % 60) - 1; | ||
| 60 | my $win_end = $win_start + 60; | ||
| 61 | |||
| 62 | # if any job is due, add it to the queue | ||
| 63 | foreach my $action (keys %{ setting('schedule') }) { | ||
| 64 | my $sched = setting('schedule')->{$action} or next; | ||
| 65 | my $real_action = ($sched->{action} || $action); | ||
| 66 | |||
| 67 | # next occurence of job must be in this minute's window | ||
| 68 | debug sprintf "sched ($wid): $real_action: win_start: %s, win_end: %s, next: %s", | ||
| 69 | $win_start, $win_end, $sched->{when}->next_time($win_start); | ||
| 70 | next unless $sched->{when}->next_time($win_start) <= $win_end; | ||
| 71 | |||
| 72 | my @job_specs = (); | ||
| 73 | |||
| 74 | if ($sched->{only} or $sched->{no}) { | ||
| 75 | $sched->{label} = $action; | ||
| 76 | push @job_specs, { | ||
| 77 | action => 'scheduler', | ||
| 78 | subaction => $coder->encode($sched), | ||
| 79 | }; | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | my @hostlist = (); | ||
| 83 | |||
| 84 | foreach my $target (ref $sched->{device} eq ref [] ? @{ $sched->{device} } | ||
| 85 | : $sched->{device}) { | ||
| 86 | # sanity checking | ||
| 87 | my $net = NetAddr::IP->new($target); | ||
| 88 | next if ($target | ||
| 89 | and (!$net or $net->num == 0 or $net->addr eq '0.0.0.0')); | ||
| 90 | |||
| 91 | if (scalar grep {$real_action eq $_} @{ setting('job_targets_prefix') }) { | ||
| 92 | push @hostlist, $target; | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | @hostlist = map { (ref $_) ? $_->addr : undef } | ||
| 96 | (defined $target ? ($net->hostenum) : (undef)); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | foreach my $host (@hostlist) { | ||
| 101 | push @job_specs, { | ||
| 102 | action => $real_action, | ||
| 103 | device => $host, | ||
| 104 | port => $sched->{port}, | ||
| 105 | subaction => ($sched->{extra} || $sched->{subaction}), | ||
| 106 | }; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | info sprintf 'sched (%s): queueing %s %s jobs', | ||
| 111 | $wid, (scalar @job_specs), $real_action; | ||
| 112 | jq_insert( \@job_specs ); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; |