simplify to remove phases and fewer hooks

This commit is contained in:
Oliver Gorwits
2017-09-18 22:04:22 +01:00
parent 6f78032e28
commit 1969291719
14 changed files with 34 additions and 58 deletions

View File

@@ -8,29 +8,20 @@ use Scope::Guard 'guard';
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Permission qw/check_acl_no check_acl_only/;
# track the phases seen so we can recall them in order
set( '_nd2worker_hooks' => [] );
my $store = Dancer::Factory::Hook->instance();
foreach my $stage (qw/check early main user/) {
$store->install_hooks("nd2_core_${stage}");
}
register 'register_worker' => sub {
my ($self, $first, $second) = plugin_args(@_);
my $workerconf = (ref $first eq 'HASH' ? $first : {});
my $code = (ref $first eq 'CODE' ? $first : $second);
return error "bad param to register_worker"
unless ((ref sub {} eq ref $code) and (ref {} eq ref $workerconf));
# needs to be here for caller() context
my ($package, $action, $phase) = ((caller)[0], undef, undef);
if ($package =~ m/::Plugin::(\w+)$/) {
$action = lc $1;
}
elsif ($package =~ m/::Plugin::(\w+)::(\w+)/) {
$action = lc $1; $phase = lc $2;
}
else { return error "worker Package does not match standard naming" }
$workerconf->{action} = $action;
$workerconf->{phase} = ($phase || '');
$workerconf->{stage} = ($workerconf->{stage} || 'second');
$workerconf->{stage} ||= 'user';
my $worker = sub {
my $job = shift or return Status->error('missing job param');
@@ -64,16 +55,8 @@ register 'register_worker' => sub {
return $code->($job, $workerconf);
};
my $store = Dancer::Factory::Hook->instance();
my $hook = 'nd2_'. $action . ($phase ? ('_'. $phase) : '');
if (not $store->hook_is_registered($hook)) {
$store->install_hooks($hook);
push @{ setting('_nd2worker_hooks') }, $hook;
}
# D::Factory::Hook::register_hook() does not work?!
$hook = $hook .'_'. $workerconf->{stage};
my $hook = 'nd2_core_'. $workerconf->{stage};
hook $hook => $worker;
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -59,25 +59,18 @@ sub run {
my $guard = guard { set(device_auth => \@userconf) };
set(device_auth => \@newuserconf);
my $store = Dancer::Factory::Hook->instance();
my @phase_hooks = grep { m/^nd2_${action}_/ }
@{ (setting('_nd2worker_hooks') || []) };
foreach my $phase ("nd2_${action}", @phase_hooks) {
foreach my $stage (qw/check first second/) {
my $hookname = "${phase}_${stage}";
next unless scalar @{ $store->get_hooks_for($hookname) };
foreach my $stage (qw/check early main user/) {
my $hookname = "nd2_core_${stage}";
$self->run_workers($hookname);
return if $stage eq 'check' and $self->jobstat->not_ok;
}
}
}
sub run_workers {
my $self = shift;
my $hook = shift or return $self->jobstat->error('missing hook param');
my $store = Dancer::Factory::Hook->instance();
my $check = ($hook =~ m/_check$/);
my $check = ($hook eq 'nd2_core_check');
return unless scalar @{ $store->get_hooks_for($hook) };
debug "running workers for hook: $hook";