blib/lib/App/Netdisco/Worker/Plugin/PrimeSkiplist.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 66 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
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 my $backend = setting('workers')->{'BACKEND'};
34  
35 # one upsert in key order rather than a select-then-write per device:
36 # backends that share a BACKEND name, such as the replicas of one
37 # deployment, prime at the same time on start, and their writes raced
38 # into duplicate keys and deadlocks
39 try {
40 schema(vars->{'tenant'})->txn_do(sub {
41 $rs->upsert([ map {{
42 backend => $backend,
43 device => $_,
44 actionset => $actionset{$_},
45 }} keys %actionset ]);
46 });
47  
48 # add one faux record to allow *walk actions to see there is a backend running
49 $rs->upsert([{
50 backend => $backend,
51 device => '255.255.255.255',
52 last_defer => \'LOCALTIMESTAMP',
53 deferrals => $max_workers,
54 }]);
55  
56 $happy = true;
57 }
58 catch {
59 error $_;
60 };
61  
62 if ($happy) {
63 return Status->done("Primed device action skip list");
64 }
65 else {
66 return Status->error("Failed to prime device action skip list");
67 }
68 });
69  
70 true;