« blib/lib/App/Netdisco/Worker/Plugin/Renumber.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Show.pm »
| 1 | package App::Netdisco::Worker::Plugin::Scheduler; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use App::Netdisco::JobQueue 'jq_insert'; | ||
| 8 | use Dancer::Plugin::DBIC 'schema'; | ||
| 9 | |||
| 10 | use App::Netdisco::Util::Device 'schedule_entry_denies'; | ||
| 11 | use JSON::PP (); | ||
| 12 | |||
| 13 | register_worker({ phase => 'check' }, sub { | ||
| 14 | my ($job, $workerconf) = @_; | ||
| 15 | |||
| 16 | return Status->error("Missing data of Scheduler entry") | ||
| 17 | unless $job->extra; | ||
| 18 | |||
| 19 | return Status->defer("scheduler skipped: have not yet primed skiplist") | ||
| 20 | unless schema(vars->{'tenant'})->resultset('DeviceSkip') | ||
| 21 | ->search({ | ||
| 22 | backend => setting('workers')->{'BACKEND'}, | ||
| 23 | device => '255.255.255.255', | ||
| 24 | })->count(); | ||
| 25 | |||
| 26 | return Status->done('Scheduler is able to run'); | ||
| 27 | }); | ||
| 28 | |||
| 29 | register_worker({ phase => 'main' }, sub { | ||
| 30 | my ($job, $workerconf) = @_; | ||
| 31 | |||
| 32 | my $coder = JSON::PP->new->utf8(0)->allow_nonref(1)->allow_unknown(1); | ||
| 33 | my $sched = $coder->decode( $job->extra || {} ); | ||
| 34 | my $action = $sched->{action} || $sched->{label}; | ||
| 35 | |||
| 36 | return Status->error("Missing label of Scheduler entry") | ||
| 37 | unless $action; | ||
| 38 | |||
| 39 | my @walk = schema(vars->{'tenant'})->resultset('Virtual::WalkJobs') | ||
| 40 | ->search(undef,{ bind => [ | ||
| 41 | $action, ('scheduled-'. $sched->{label}), | ||
| 42 | setting('workers')->{'max_deferrals'}, | ||
| 43 | setting('workers')->{'retry_after'}, | ||
| 44 | '', 0, | ||
| 45 | ]})->get_column('ip')->all; | ||
| 46 | |||
| 47 | # the skip list behind WalkJobs is primed from the schedule each backend | ||
| 48 | # has in its own configuration, and NETDISCO_NO_SCHEDULER removes it from | ||
| 49 | # the workers, so the entry's device, only and no are applied here too | ||
| 50 | if (@walk and ($sched->{device} or $sched->{only} or $sched->{no})) { | ||
| 51 | @walk = map { $_->ip } | ||
| 52 | grep { not schedule_entry_denies($_, $sched) } | ||
| 53 | schema(vars->{'tenant'})->resultset('Device') | ||
| 54 | ->search({ ip => { -in => \@walk } })->all; | ||
| 55 | } | ||
| 56 | |||
| 57 | jq_insert([ | ||
| 58 | map {{ | ||
| 59 | device => $_, | ||
| 60 | action => $action, | ||
| 61 | port => $sched->{port}, | ||
| 62 | subaction => ($sched->{extra} || $sched->{subaction}), | ||
| 63 | username => $job->username, | ||
| 64 | userip => $job->userip, | ||
| 65 | }} (@walk) | ||
| 66 | ]); | ||
| 67 | |||
| 68 | return Status->done(sprintf 'Queued %s job for all devices', $action); | ||
| 69 | }); | ||
| 70 | |||
| 71 | true; |