move status tracking and checking inside job instance

This commit is contained in:
Oliver Gorwits
2017-11-06 21:26:01 +00:00
parent 4436150bf4
commit 25907d3544
8 changed files with 165 additions and 100 deletions

View File

@@ -1,5 +1,7 @@
package App::Netdisco::Backend::Job;
use aliased 'App::Netdisco::Worker::Status';
use Moo;
use namespace::clean;
@@ -17,6 +19,10 @@ foreach my $slot (qw/
userip
log
debug
_phase
_namespace
_priority
/) {
has $slot => (
@@ -24,40 +30,119 @@ foreach my $slot (qw/
);
}
has '_statuslist' => (
is => 'rw',
default => sub { [] },
);
=head1 METHODS
=head2 summary
An attempt to make a meaningful statement about the job.
An attempt to make a meaningful written statement about the job.
=cut
sub summary {
my $job = shift;
return join ' ',
$job->action,
($job->device || ''),
($job->port || '');
# ($job->subaction ? (q{'}. $job->subaction .q{'}) : '');
my $job = shift;
return join ' ',
$job->action,
($job->device || ''),
($job->port || '');
}
=head2 update_status
=head2 finalise_status
Passed an L<App::Netdisco::Worker::Status> will update this job's C<log> and
C<status> slots.
Find the best status and log it into the job's C<status> and C<log> slots.
=cut
sub update_status {
sub finalise_status {
my $job = shift;
my $status = shift or return;
$job->status( $status->status );
$job->log( $status->log );
return $job;
my $max_level = Status->error()->level;
# fallback
$job->status('error');
$job->log('failed to succeed at any worker!');
foreach my $status (@{ $self->_statuslist }) {
next unless $status->phase =~ m/^(?:early|main)$/;
if ($status->level >= $max_level) {
$job->status( $status->status );
$job->log( $status->log );
$max_level = $status->level;
}
}
}
=head2 check_passed
Returns true if at least one worker during the C<check> phase flagged status
C<done>.
=cut
sub check_passed {
my $job = shift;
foreach my $status (@{ $self->_statuslist }) {
next unless $status->phase eq 'check';
return 1 if $status->is_ok;
}
return 0;
}
=head2 namespace_passed( \%workerconf )
Returns true when, for the namespace specified in the passed configuration,
all workers of a higher priority level have succeeded.
=cut
sub namespace_passed {
my ($job, $workerconf) = @_;
if (defined $job->_namespace
and ($workerconf->{phase} eq $job->_phase)
and ($workerconf->{namespace} eq $job->_namespace)
and ($workerconf->{priority} != $job->_priority)) {
foreach my $status (@{ $self->_statuslist }) {
next unless ($status->phase eq $job->_phase)
and ($staus->namespace eq $job->_namespace)
and ($status->priority == $job->_priority);
return 1 if $status->is_ok;
}
}
$job->_phase( $workerconf->{phase} );
$job->_namespace( $workerconf->{namespace} );
$job->_priority( $workerconf->{priority} );
return 0;
}
=head2 add_status
Passed an L<App::Netdisco::Worker::Status> will add it to this job's internal
store.
=cut
sub add_status {
my ($job, $status) = @_;
return unless ref $status eq 'App::Netdisco::Worker::Status';
push @{ $self->_statuslist }, $status;
}
=head1 ADDITIONAL COLUMNS
=head2 id
Alias for the C<job> column.
=cut
sub id { (shift)->job }
=head2 extra
Alias for the C<subaction> column.

View File

@@ -5,12 +5,12 @@ use Dancer qw/:moose :syntax :script/;
use List::Util 'sum';
use App::Netdisco::Util::MCE;
use Role::Tiny;
use namespace::clean;
use App::Netdisco::JobQueue
qw/jq_locked jq_getsome jq_getsomep jq_lock jq_warm_thrusters/;
use Role::Tiny;
use namespace::clean;
sub worker_begin {
my $self = shift;
my $wid = $self->wid;
@@ -58,7 +58,7 @@ sub worker_body {
# mark job as running
next unless jq_lock($job);
info sprintf "mgr (%s): job %s booked out for this processing node",
$wid, $job->job;
$wid, $job->id;
# copy job to local queue
$self->{queue}->enqueuep(100, $job);
@@ -75,7 +75,7 @@ sub worker_body {
# mark job as running
next unless jq_lock($job);
info sprintf "mgr (%s): job %s booked out for this processing node",
$wid, $job->job;
$wid, $job->id;
# copy job to local queue
$self->{queue}->enqueue($job);

View File

@@ -5,12 +5,12 @@ use Dancer qw/:moose :syntax :script/;
use Try::Tiny;
use App::Netdisco::Util::MCE;
use Role::Tiny;
use namespace::clean;
use Time::HiRes 'sleep';
use App::Netdisco::JobQueue qw/jq_defer jq_complete/;
use Role::Tiny;
use namespace::clean;
# add dispatch methods for poller tasks
with 'App::Netdisco::Worker::Runner';
@@ -29,9 +29,9 @@ sub worker_body {
try {
$job->started(scalar localtime);
prctl sprintf 'nd2: #%s poll: #%s: %s',
$wid, $job->job, $job->summary;
$wid, $job->id, $job->summary;
info sprintf "pol (%s): starting %s job(%s) at %s",
$wid, $job->action, $job->job, $job->started;
$wid, $job->action, $job->id, $job->started;
$self->run($job);
}
catch {
@@ -51,7 +51,7 @@ sub close_job {
my $now = scalar localtime;
info sprintf "pol (%s): wrapping up %s job(%s) - status %s at %s",
$self->wid, $job->action, $job->job, $job->status, $now;
$self->wid, $job->action, $job->id, $job->status, $now;
try {
if ($job->status eq 'defer') {

View File

@@ -5,11 +5,11 @@ use Dancer qw/:moose :syntax :script/;
use Algorithm::Cron;
use App::Netdisco::Util::MCE;
use App::Netdisco::JobQueue qw/jq_insert/;
use Role::Tiny;
use namespace::clean;
use App::Netdisco::JobQueue qw/jq_insert/;
sub worker_begin {
my $self = shift;
my $wid = $self->wid;