further work on retval handling from workers

This commit is contained in:
Oliver Gorwits
2017-09-04 21:54:37 +01:00
parent 4c1fdf4f92
commit 75abdad812
2 changed files with 18 additions and 18 deletions

View File

@@ -4,9 +4,9 @@ use Dancer ':syntax';
use Dancer::Plugin;
use Dancer::Factory::Hook;
use App::Netdisco::Util::Permission qw/check_acl_no check_acl_only/;
use Scope::Guard;
use Try::Tiny;
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' => [] );
@@ -31,7 +31,7 @@ register 'register_worker' => sub {
$workerconf->{primary} = $workerconf->{primary};
my $worker = sub {
my $job = shift or return false;
my $job = shift or return Status->error('missing job param');
my $no = (exists $workerconf->{no} ? $workerconf->{no} : undef);
my $only = (exists $workerconf->{only} ? $workerconf->{only} : undef);
@@ -51,18 +51,11 @@ register 'register_worker' => sub {
}
# back up and restore device_auth
return false unless scalar @newuserconf;
my $guard = guard { set(device_auth => \@userconf) };
set(device_auth => \@newuserconf);
# run worker
my $happy = false;
try {
$code->($job, $workerconf);
$happy = true;
}
catch { debug $_ };
return $happy;
return $code->($job, $workerconf);
};
my $primary = ($workerconf->{primary} ? '_primary' : '');

View File

@@ -16,7 +16,8 @@ sub run {
die 'bad job to run()' unless ref $job eq 'App::Netdisco::Backend::Job';
my $action = $job->action;
my @phase_hooks = @{ (setting('_nd2worker_hooks') || []) };
my @phase_hooks = grep { m/^nd2worker_${action}_/ }
@{ (setting('_nd2worker_hooks') || []) };
# run 00init primary
my $status = _run_first("nd2worker_${action}_00init_primary", $job);
@@ -26,12 +27,10 @@ sub run {
_run_all("nd2worker_${action}_00init", $job);
# run primary
_run_first($_.'_primary', $job)
for (grep { m/^nd2worker_${action}_/ } @phase_hooks);
_run_first($_.'_primary', $job) for (@phase_hooks);
# run each worker
_run_all($_, $job)
for (grep { m/^nd2worker_${action}_/ } @phase_hooks);
_run_all($_, $job) for (@phase_hooks);
return true;
}
@@ -45,8 +44,16 @@ sub _run_first {
or return Status->error("no such hook: $hook");
foreach my $worker (@{ $store->get_hooks_for($hook) }) {
my $status = $worker->($job);
return $status if $status->ok;
my $retval = false;
try {
$retval = $worker->($job);
}
catch {
$retval = Status->error($_);
};
$retval ||= Status->done('no status supplied');
return $retval if $retval->ok;
}
return Status->error('no worker was successful');