« blib/lib/App/Netdisco/Worker/Plugin/Power.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Psql.pm »
| 1 | package App::Netdisco::Worker::Plugin::PrimeSkiplist; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC 'schema'; | ||
| 5 | |||
| 6 | use App::Netdisco::Worker::Plugin; | ||
| 7 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 8 | |||
| 9 | use App::Netdisco::Util::Device 'get_denied_actions'; | ||
| 10 | use App::Netdisco::Backend::Queues 'total_workers'; | ||
| 11 | use App::Netdisco::Backend::Job; | ||
| 12 | |||
| 13 | use Try::Tiny; | ||
| 14 | |||
| 15 | register_worker({ phase => 'main' }, sub { | ||
| 16 | my ($job, $workerconf) = @_; | ||
| 17 | my $happy = false; | ||
| 18 | |||
| 19 | my $devices = schema(vars->{'tenant'})->resultset('Device'); | ||
| 20 | my $rs = schema(vars->{'tenant'})->resultset('DeviceSkip'); | ||
| 21 | my %actionset = (); | ||
| 22 | |||
| 23 | while (my $d = $devices->next) { | ||
| 24 | my @badactions = get_denied_actions($d); | ||
| 25 | $actionset{$d->ip} = \@badactions if scalar @badactions; | ||
| 26 | } | ||
| 27 | |||
| 28 | debug sprintf 'priming device action skip list for %d devices', | ||
| 29 | scalar keys %actionset; | ||
| 30 | |||
| 31 | my $max_workers = total_workers(); | ||
| 32 | |||
| 33 | try { | ||
| 34 | schema(vars->{'tenant'})->txn_do(sub { | ||
| 35 | $rs->update_or_create({ | ||
| 36 | backend => setting('workers')->{'BACKEND'}, | ||
| 37 | device => $_, | ||
| 38 | actionset => $actionset{$_}, | ||
| 39 | }, { key => 'primary' }) for keys %actionset; | ||
| 40 | }); | ||
| 41 | |||
| 42 | # add one faux record to allow *walk actions to see there is a backend running | ||
| 43 | $rs->update_or_create({ | ||
| 44 | backend => setting('workers')->{'BACKEND'}, | ||
| 45 | device => '255.255.255.255', | ||
| 46 | last_defer => \'LOCALTIMESTAMP', | ||
| 47 | deferrals => $max_workers, | ||
| 48 | }, { key => 'primary' }); | ||
| 49 | |||
| 50 | $happy = true; | ||
| 51 | } | ||
| 52 | catch { | ||
| 53 | error $_; | ||
| 54 | }; | ||
| 55 | |||
| 56 | if ($happy) { | ||
| 57 | return Status->done("Primed device action skip list"); | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | return Status->error("Failed to prime device action skip list"); | ||
| 61 | } | ||
| 62 | }); | ||
| 63 | |||
| 64 | true; |