From 5bddfc73bad0a456a02d468ac24f5453fe5abb30 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sat, 7 Oct 2017 08:50:31 +0100 Subject: [PATCH] auto debug-log worker return messages --- lib/App/Netdisco/Worker/Plugin/Test.pm | 3 --- lib/App/Netdisco/Worker/Plugin/Test/Core.pm | 4 ---- lib/App/Netdisco/Worker/Runner.pm | 23 ++++++++++++++------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/App/Netdisco/Worker/Plugin/Test.pm b/lib/App/Netdisco/Worker/Plugin/Test.pm index 20f21a4a..6e1ba77d 100644 --- a/lib/App/Netdisco/Worker/Plugin/Test.pm +++ b/lib/App/Netdisco/Worker/Plugin/Test.pm @@ -6,19 +6,16 @@ use aliased 'App::Netdisco::Worker::Status'; register_worker({ stage => 'main' }, sub { my ($job, $workerconf) = @_; - debug 'Test (main) ran successfully.'; return Status->done('Test (main) ran successfully (1).'); }); register_worker({ stage => 'check' }, sub { my ($job, $workerconf) = @_; - debug 'Test (check) ran successfully.'; return Status->done('Test (check) ran successfully.'); }); register_worker({ stage => 'early' }, sub { my ($job, $workerconf) = @_; - debug 'Test (early) ran successfully.'; return Status->error('Test (early) ran successfully.'); }); diff --git a/lib/App/Netdisco/Worker/Plugin/Test/Core.pm b/lib/App/Netdisco/Worker/Plugin/Test/Core.pm index 9cb5203e..1f929d8e 100644 --- a/lib/App/Netdisco/Worker/Plugin/Test/Core.pm +++ b/lib/App/Netdisco/Worker/Plugin/Test/Core.pm @@ -6,25 +6,21 @@ use aliased 'App::Netdisco::Worker::Status'; register_worker({ stage => 'main' }, sub { my ($job, $workerconf) = @_; - debug 'Test (main) ran successfully.'; return Status->done('Test (main) ran successfully (2).'); }); register_worker({ stage => 'check' }, sub { my ($job, $workerconf) = @_; - debug 'Test (check) ran successfully.'; return Status->done('Test (check) ran successfully.'); }); register_worker({ stage => 'early' }, sub { my ($job, $workerconf) = @_; - debug 'Test (early) ran successfully.'; return Status->done('Test (early) ran successfully.'); }); register_worker(sub { my ($job, $workerconf) = @_; - debug 'Test (undefined) ran successfully.'; return Status->error('Test (undefined) ran successfully.'); }); diff --git a/lib/App/Netdisco/Worker/Runner.pm b/lib/App/Netdisco/Worker/Runner.pm index a1cc9d43..6963864a 100644 --- a/lib/App/Netdisco/Worker/Runner.pm +++ b/lib/App/Netdisco/Worker/Runner.pm @@ -62,11 +62,14 @@ sub run { my $guard = guard { set(device_auth => \@userconf) }; set(device_auth => \@newuserconf); + # run check phase + # optional - but if there are workers then one MUST return done my $store = Dancer::Factory::Hook->instance(); $self->run_workers('nd2_core_check'); return if scalar @{ $store->get_hooks_for('nd2_core_check') } and $self->jobstat->not_ok; + # run other phases $self->jobstat( Status->error('no worker succeeded during main phase') ); $self->run_workers("nd2_core_${_}") for qw/early main user/; } @@ -85,15 +88,21 @@ sub run_workers { # could die or return undef or a scalar or Status or another class my $retval = $worker->($self->job); - # update (save) the status if we're in check or main phases - #  check because it's a gatekeeper, main because it's the retval - $self->jobstat($retval) - if ($phase =~ m/^(?:check|main)$/) - and ref $retval eq 'App::Netdisco::Worker::Status' - and $retval->level >= $self->jobstat->level; + if (ref $retval eq 'App::Netdisco::Worker::Status') { + # update (save) the status if we're in check or main phases + # because these logs can end up in the job queue as status message + $self->jobstat($retval) + if ($phase =~ m/^(?:check|main)$/) + and $retval->level >= $self->jobstat->level; + + debug $retval->log if $retval->log; + } } # errors at most phases are ignored - catch { $self->jobstat->error($_) if $phase eq 'check' }; + catch { + $self->jobstat->error($_) if $phase eq 'check'; + debug $_ if $_; + }; # any successful check is a GO! last if $phase eq 'check' and $self->jobstat->is_ok;