change to init, first, second stages

This commit is contained in:
Oliver Gorwits
2017-09-10 16:09:45 +01:00
parent 5f2da69697
commit 64a9491115
34 changed files with 74 additions and 63 deletions

View File

@@ -29,8 +29,8 @@ register 'register_worker' => sub {
else { return error "worker Package does not match standard naming" } else { return error "worker Package does not match standard naming" }
$workerconf->{action} = $action; $workerconf->{action} = $action;
$workerconf->{phase} = ($phase || '00init'); $workerconf->{phase} = ($phase || '');
$workerconf->{primary} = ($workerconf->{primary} ? true : false); $workerconf->{stage} = ($workerconf->{stage} || 'second');
my $worker = sub { my $worker = sub {
my $job = shift or return Status->error('missing job param'); my $job = shift or return Status->error('missing job param');
@@ -64,18 +64,16 @@ register 'register_worker' => sub {
return $code->($job, $workerconf); return $code->($job, $workerconf);
}; };
my $primary = ($workerconf->{primary} ? '_primary' : '');
my $hook = 'nd2_'. $action .'_'. $workerconf->{phase} . $primary;
my $store = Dancer::Factory::Hook->instance(); my $store = Dancer::Factory::Hook->instance();
my $hook = 'nd2_'. $action . ($phase ? ('_'. $phase) : '');
if (not $store->hook_is_registered($hook)) { if (not $store->hook_is_registered($hook)) {
$store->install_hooks($hook); $store->install_hooks($hook);
# track just the basic phase names which are used push @{ setting('_nd2worker_hooks') }, $hook;
push @{ setting('_nd2worker_hooks') }, $hook
if $workerconf->{phase} ne '00init' and 0 == length($primary);
} }
# D::Factory::Hook::register_hook() does not work?! # D::Factory::Hook::register_hook() does not work?!
$hook = $hook .'_'. $workerconf->{stage};
hook $hook => $worker; hook $hook => $worker;
}; };

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Device 'is_arpnipable_now'; use App::Netdisco::Util::Device 'is_arpnipable_now';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -11,7 +11,7 @@ use Dancer::Plugin::DBIC 'schema';
use Time::HiRes 'gettimeofday'; use Time::HiRes 'gettimeofday';
use NetAddr::MAC (); use NetAddr::MAC ();
register_worker({ primary => true, driver => 'snmp' }, sub { register_worker({ stage => 'init', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -10,7 +10,7 @@ use Dancer::Plugin::DBIC 'schema';
use NetAddr::IP::Lite ':lower'; use NetAddr::IP::Lite ':lower';
use Time::HiRes 'gettimeofday'; use Time::HiRes 'gettimeofday';
register_worker({ primary => false, driver => 'snmp' }, sub { register_worker({ stage => 'second', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::JobQueue qw/jq_queued jq_insert/; use App::Netdisco::JobQueue qw/jq_queued jq_insert/;
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my %queued = map {$_ => 1} jq_queued('arpnip'); my %queued = map {$_ => 1} jq_queued('arpnip');

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Device 'delete_device'; use App::Netdisco::Util::Device 'delete_device';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my ($device, $port, $extra) = map {$job->$_} qw/device port extra/; my ($device, $port, $extra) = map {$job->$_} qw/device port extra/;
return Status->error('Missing device (-d).') if !defined $device; return Status->error('Missing device (-d).') if !defined $device;

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Device 'is_discoverable_now'; use App::Netdisco::Util::Device 'is_discoverable_now';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -9,7 +9,7 @@ use App::Netdisco::Util::Permission 'check_acl_only';
use App::Netdisco::Util::DNS 'ipv4_from_hostname'; use App::Netdisco::Util::DNS 'ipv4_from_hostname';
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => false, driver => 'snmp' }, sub { register_worker({ stage => 'second', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -8,7 +8,7 @@ use App::Netdisco::Transport::SNMP ();
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
use Encode; use Encode;
register_worker({ primary => false, driver => 'snmp' }, sub { register_worker({ stage => 'second', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -8,7 +8,7 @@ use App::Netdisco::Transport::SNMP ();
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
use Encode; use Encode;
register_worker({ primary => false, driver => 'snmp' }, sub { register_worker({ stage => 'first', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -30,7 +30,7 @@ immediately queued (subject to the filtering by the C<discover_*> settings).
=cut =cut
register_worker({ primary => false, driver => 'snmp' }, sub { register_worker({ stage => 'second', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Transport::SNMP (); use App::Netdisco::Transport::SNMP ();
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => false, driver => 'snmp' }, sub { register_worker({ stage => 'second', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -12,7 +12,7 @@ use Dancer::Plugin::DBIC 'schema';
use NetAddr::IP::Lite ':lower'; use NetAddr::IP::Lite ':lower';
use Encode; use Encode;
register_worker({ primary => true, driver => 'snmp' }, sub { register_worker({ stage => 'init', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Transport::SNMP (); use App::Netdisco::Transport::SNMP ();
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => false, driver => 'snmp' }, sub { register_worker({ stage => 'second', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Transport::SNMP (); use App::Netdisco::Transport::SNMP ();
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => false, driver => 'snmp' }, sub { register_worker({ stage => 'second', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => false }, sub { register_worker({ stage => 'second' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::JobQueue qw/jq_queued jq_insert/; use App::Netdisco::JobQueue qw/jq_queued jq_insert/;
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my %queued = map {$_ => 1} jq_queued('discover'); my %queued = map {$_ => 1} jq_queued('discover');

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
use App::Netdisco::Util::Statistics 'update_stats'; use App::Netdisco::Util::Statistics 'update_stats';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
if (setting('expire_devices') and setting('expire_devices') > 0) { if (setting('expire_devices') and setting('expire_devices') > 0) {

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
return Status->error('nbtstat failed: unable to interpret device param') return Status->error('nbtstat failed: unable to interpret device param')

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Graph (); use App::Netdisco::Util::Graph ();
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
App::Netdisco::Util::Graph::graph(); App::Netdisco::Util::Graph::graph();
return Status->done('Generated graph data.'); return Status->done('Generated graph data.');

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Device 'is_macsuckable_now'; use App::Netdisco::Util::Device 'is_macsuckable_now';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -14,7 +14,7 @@ use Dancer::Plugin::DBIC 'schema';
use Time::HiRes 'gettimeofday'; use Time::HiRes 'gettimeofday';
use Scope::Guard 'guard'; use Scope::Guard 'guard';
register_worker({ primary => true, driver => 'snmp' }, sub { register_worker({ stage => 'init', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -8,7 +8,7 @@ use App::Netdisco::Transport::SNMP ();
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
use Time::HiRes 'gettimeofday'; use Time::HiRes 'gettimeofday';
register_worker({ primary => false, driver => 'snmp' }, sub { register_worker({ stage => 'second', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::JobQueue qw/jq_queued jq_insert/; use App::Netdisco::JobQueue qw/jq_queued jq_insert/;
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my %queued = map {$_ => 1} jq_queued('macsuck'); my %queued = map {$_ => 1} jq_queued('macsuck');

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::NodeMonitor (); use App::Netdisco::Util::NodeMonitor ();
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
App::Netdisco::Util::NodeMonitor::monitor(); App::Netdisco::Util::NodeMonitor::monitor();
return Status->done('Generated monitor data.'); return Status->done('Generated monitor data.');

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Device 'is_macsuckable'; use App::Netdisco::Util::Device 'is_macsuckable';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
return Status->error('nbtstat failed: unable to interpret device param') return Status->error('nbtstat failed: unable to interpret device param')

View File

@@ -9,7 +9,7 @@ use App::Netdisco::Util::Node 'is_nbtstatable';
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
use Time::HiRes 'gettimeofday'; use Time::HiRes 'gettimeofday';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $host = $job->device->ip; my $host = $job->device->ip;

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::JobQueue qw/jq_queued jq_insert/; use App::Netdisco::JobQueue qw/jq_queued jq_insert/;
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my %queued = map {$_ => 1} jq_queued('nbtstat'); my %queued = map {$_ => 1} jq_queued('nbtstat');

View File

@@ -4,7 +4,7 @@ use Dancer ':syntax';
use App::Netdisco::Worker::Plugin; use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status'; use aliased 'App::Netdisco::Worker::Status';
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my ($device, $port, $extra) = map {$job->$_} qw/device port extra/; my ($device, $port, $extra) = map {$job->$_} qw/device port extra/;

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use NetAddr::IP qw/:rfc3021 :lower/; use NetAddr::IP qw/:rfc3021 :lower/;
use App::Netdisco::Util::Device qw/get_device renumber_device/; use App::Netdisco::Util::Device qw/get_device renumber_device/;
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my ($device, $port, $extra) = map {$job->$_} qw/device port extra/; my ($device, $port, $extra) = map {$job->$_} qw/device port extra/;
return Status->error('Missing device (-d).') if !defined $device; return Status->error('Missing device (-d).') if !defined $device;

View File

@@ -7,7 +7,7 @@ use aliased 'App::Netdisco::Worker::Status';
use Data::Printer (); use Data::Printer ();
use App::Netdisco::Transport::SNMP; use App::Netdisco::Transport::SNMP;
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my ($device, $port, $extra) = map {$job->$_} qw/device port extra/; my ($device, $port, $extra) = map {$job->$_} qw/device port extra/;
return Status->error('Missing device (-d).') if !defined $device; return Status->error('Missing device (-d).') if !defined $device;

View File

@@ -6,7 +6,7 @@ use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Statistics (); use App::Netdisco::Util::Statistics ();
register_worker({ primary => true }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
App::Netdisco::Util::Statistics::update_stats(); App::Netdisco::Util::Statistics::update_stats();
return Status->done('Updated statistics.'); return Status->done('Updated statistics.');

View File

@@ -4,16 +4,28 @@ use Dancer ':syntax';
use App::Netdisco::Worker::Plugin; use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status'; use aliased 'App::Netdisco::Worker::Status';
register_worker({ primary => true }, sub { register_worker({ stage => 'second' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
debug 'Test (primary) ran successfully.'; debug 'Test (second) ran successfully.';
return Status->done('Test (primary) ran successfully.'); return Status->done('Test (second) ran successfully.');
}); });
register_worker({ primary => false }, sub { register_worker({ stage => 'init' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
debug 'Test ran successfully.'; debug 'Test (init) ran successfully.';
return Status->done('Test ran successfully.'); return Status->done('Test (init) ran successfully.');
});
register_worker({ stage => 'first' }, sub {
my ($job, $workerconf) = @_;
debug 'Test (first) ran successfully.';
return Status->done('Test (first) ran successfully.');
});
register_worker(sub {
my ($job, $workerconf) = @_;
debug 'Test (undefined) ran successfully.';
return Status->done('Test (undefined) ran successfully.');
}); });
true; true;

View File

@@ -59,32 +59,32 @@ sub run {
my $guard = guard { set(device_auth => \@userconf) }; my $guard = guard { set(device_auth => \@userconf) };
set(device_auth => \@newuserconf); set(device_auth => \@newuserconf);
my $store = Dancer::Factory::Hook->instance();
my @phase_hooks = grep { m/^nd2_${action}_/ } my @phase_hooks = grep { m/^nd2_${action}_/ }
@{ (setting('_nd2worker_hooks') || []) }; @{ (setting('_nd2worker_hooks') || []) };
# run 00init primary foreach my $stage (qw/init first second/) {
my $store = Dancer::Factory::Hook->instance(); my $hookname = "nd2_${action}_${stage}";
my $initprimary = "nd2_${action}_00init_primary"; next unless scalar @{ $store->get_hooks_for($hookname) };
if (scalar @{ $store->get_hooks_for($initprimary) }) { $self->run_workers($hookname);
$self->run_workers($initprimary); last if $stage eq 'init' and $self->jobstat->not_ok;
return if $self->jobstat->not_ok;
} }
# run each 00init worker foreach my $phase (@phase_hooks) {
$self->run_workers("nd2_${action}_00init"); foreach my $stage (qw/init first second/) {
my $hookname = "${phase}_${stage}";
# run primary next unless scalar @{ $store->get_hooks_for($hookname) };
$self->run_workers("${_}_primary") for (@phase_hooks); $self->run_workers($hookname);
last if $stage eq 'init' and $self->jobstat->not_ok;
# run each worker }
$self->run_workers($_) for (@phase_hooks); }
} }
sub run_workers { sub run_workers {
my $self = shift; my $self = shift;
my $hook = shift or return $self->jobstat->error('missing hook param'); my $hook = shift or return $self->jobstat->error('missing hook param');
my $store = Dancer::Factory::Hook->instance(); my $store = Dancer::Factory::Hook->instance();
my $primary = ($hook =~ m/_primary$/); my $init = ($hook =~ m/_init$/);
return unless scalar @{ $store->get_hooks_for($hook) }; return unless scalar @{ $store->get_hooks_for($hook) };
debug "running workers for hook: $hook"; debug "running workers for hook: $hook";
@@ -93,11 +93,12 @@ sub run_workers {
try { try {
my $retval = $worker->($self->job); my $retval = $worker->($self->job);
# could die or return undef or a scalar or Status or another class # could die or return undef or a scalar or Status or another class
$self->jobstat($retval) if ref $retval eq 'App::Netdisco::Worker::Status'; $self->jobstat($retval)
if $init and ref $retval eq 'App::Netdisco::Worker::Status';
} }
catch { $self->jobstat->error($_) }; catch { $self->jobstat->error($_) if $init };
last if $primary and $self->jobstat->is_ok; last if $init and $self->jobstat->is_ok;
} }
} }