From d3c83c395da448964c9c533ff607ff4e947e8cd9 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sun, 11 Feb 2018 11:16:14 +0000 Subject: [PATCH] cancel jobs when it is known fresh device is already discovered --- lib/App/Netdisco/Backend/Job.pm | 18 ++++++++++++++++++ lib/App/Netdisco/Worker/Plugin.pm | 2 ++ .../Worker/Plugin/Discover/CanonicalIP.pm | 18 +++++++++++++----- .../Worker/Plugin/Discover/WithNodes.pm | 3 +-- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/App/Netdisco/Backend/Job.pm b/lib/App/Netdisco/Backend/Job.pm index a3ffa250..39f1ff8e 100644 --- a/lib/App/Netdisco/Backend/Job.pm +++ b/lib/App/Netdisco/Backend/Job.pm @@ -22,6 +22,7 @@ foreach my $slot (qw/ log device_key job_priority + is_cancelled _current_phase _last_namespace @@ -45,6 +46,10 @@ sub BUILD { $job->action($1); $job->only_namespace($2); } + + if (!defined $job->subaction) { + $job->subaction(''); + } } =head1 METHODS @@ -63,6 +68,19 @@ sub display_name { ($job->port || ''); } +=head2 cancel + +Log a status and prevent other stages from runnning. + +=cut + +sub cancel { + my ($job, $msg) = @_; + $msg ||= 'unknown reason for cancelled job'; + $job->is_cancelled(true); + return Status->info($msg); +} + =head2 finalise_status Find the best status and log it into the job's C and C slots. diff --git a/lib/App/Netdisco/Worker/Plugin.pm b/lib/App/Netdisco/Worker/Plugin.pm index 453e918d..8f05a7c4 100644 --- a/lib/App/Netdisco/Worker/Plugin.pm +++ b/lib/App/Netdisco/Worker/Plugin.pm @@ -36,6 +36,8 @@ register 'register_worker' => sub { debug sprintf '-> run worker %s/%s/%s', @$workerconf{qw/phase namespace priority/}; + return if $job->is_cancelled; + # check to see if this namespace has already passed at higher priority # and also update job's record of namespace and priority return $job->add_status( Status->info('skip: namespace passed at higher priority') ) diff --git a/lib/App/Netdisco/Worker/Plugin/Discover/CanonicalIP.pm b/lib/App/Netdisco/Worker/Plugin/Discover/CanonicalIP.pm index 443acc0e..7e80cc62 100644 --- a/lib/App/Netdisco/Worker/Plugin/Discover/CanonicalIP.pm +++ b/lib/App/Netdisco/Worker/Plugin/Discover/CanonicalIP.pm @@ -69,17 +69,25 @@ register_worker({ phase => 'main', driver => 'snmp' }, sub { return if $new_ip eq $old_ip; - schema('netdisco')->txn_do(sub { - # delete target device with the same vendor and serial number - schema('netdisco')->resultset('Device')->search({ + return schema('netdisco')->txn_do(sub { + my $existing = schema('netdisco')->resultset('Device')->search({ ip => $new_ip, vendor => $device->vendor, serial => $device->serial, - })->delete; + }); + + if (($job->subaction eq 'with-nodes') and $existing->count) { + $device->delete; + return $job->cancel( + " [$old_ip] device - cancelling fresh discover: already known as $new_ip"); + } + + # discover existing device but change IP, need to remove existing device + $existing->delete; # if target device exists then this will die $device->renumber($new_ip) or die "cannot renumber to: $new_ip"; # rollback - # is not done in renumber but required otherwise confusing at job end! + # is not done in renumber, but required, otherwise confusing at job end! schema('netdisco')->resultset('Admin') ->find({job => $job->id})->update({device => $new_ip}) if $job->id; diff --git a/lib/App/Netdisco/Worker/Plugin/Discover/WithNodes.pm b/lib/App/Netdisco/Worker/Plugin/Discover/WithNodes.pm index cc76bd22..17528b6e 100644 --- a/lib/App/Netdisco/Worker/Plugin/Discover/WithNodes.pm +++ b/lib/App/Netdisco/Worker/Plugin/Discover/WithNodes.pm @@ -13,8 +13,7 @@ register_worker({ phase => 'main' }, sub { # if requested, and the device has not yet been # arpniped/macsucked, queue those jobs now - return unless $device->in_storage - and $job->subaction and $job->subaction eq 'with-nodes'; + return unless $device->in_storage and $job->subaction eq 'with-nodes'; if (!defined $device->last_macsuck and $device->has_layer(2)) { jq_insert({