From 4def0af0b02e4d2d1776c1374b6d6f2349716582 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sun, 3 Sep 2017 17:58:03 +0100 Subject: [PATCH] better use of new status class --- bin/netdisco-do | 13 +++++------ lib/App/Netdisco/Backend/Status.pm | 28 +++++++++++++++++++++++ lib/App/Netdisco/Backend/Worker/Poller.pm | 5 ++-- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/bin/netdisco-do b/bin/netdisco-do index 34581a6e..396188c1 100755 --- a/bin/netdisco-do +++ b/bin/netdisco-do @@ -254,21 +254,20 @@ foreach my $host (@hostlist) { ); # do job - my ($status, $log); try { - info sprintf '%s: %s started at %s', $action, $actiontext, scalar localtime; - ($status, $log) = $worker->$action($job); + my $status = $worker->$action($job); # TODO + $status->update_job($job); } catch { - $status = 'error'; - $log = "error running job: $_"; + $job->status('error'); + $job->log("error running job: $_"); }; info sprintf '%s: finished at %s', $action, scalar localtime; - info sprintf '%s: status %s: %s', $action, $status, $log; - $exitstatus = 1 if !defined $status or $status eq 'error'; + info sprintf '%s: status %s: %s', $action, $job->status, $job->log; + $exitstatus = 1 if !$exitstatus and $job->status ne 'done'; } exit $exitstatus; diff --git a/lib/App/Netdisco/Backend/Status.pm b/lib/App/Netdisco/Backend/Status.pm index 7e78251e..3c4e304b 100644 --- a/lib/App/Netdisco/Backend/Status.pm +++ b/lib/App/Netdisco/Backend/Status.pm @@ -36,4 +36,32 @@ Returns the logical inversion of C. sub not_ok { return (not $_[0]->ok) } +=head2 status + +Returns text equivalent of C, C, or C. + +=cut + +sub status { + my $self = shift; + return ( + $self->done ? 'done' + : $self->defer ? 'defer' + : 'error'; + ); +} + +=head2 update_job + +Updates an L with status and message. + +=cut + +sub update_job { + my $self = shift; + my $job = shift or return; + $job->status( $self->status ); + $job->log( $self->message ); +} + 1; diff --git a/lib/App/Netdisco/Backend/Worker/Poller.pm b/lib/App/Netdisco/Backend/Worker/Poller.pm index 97713218..9af441b7 100644 --- a/lib/App/Netdisco/Backend/Worker/Poller.pm +++ b/lib/App/Netdisco/Backend/Worker/Poller.pm @@ -39,9 +39,8 @@ sub worker_body { $wid, $job->job, $job->summary; info sprintf "pol (%s): starting %s job(%s) at %s", $wid, $action, $job->job, $job->started; - my ($status, $log) = $self->$action($job); - $job->status($status); - $job->log($log); + my $status = $self->$action($job); # TODO + $status->update_job($job); } catch { $job->status('error');