add log messages to the Daemon

This commit is contained in:
Oliver Gorwits
2013-02-24 20:20:22 +00:00
parent cfea15773b
commit 13c7ee81ec
7 changed files with 45 additions and 5 deletions

View File

@@ -24,11 +24,13 @@ my $queue = schema('daemon')->resultset('Admin');
sub add_jobs {
my ($jobs) = @_;
info sprintf "adding %s jobs to local queue", scalar @$jobs;
$queue->populate($jobs);
}
sub capacity_for {
my ($action) = @_;
debug "checking local capacity for action $action";
my $action_map = {
Interactive => [qw/location contact portcontrol portname vlan power/]
@@ -56,8 +58,10 @@ sub take_jobs {
$max ||= 1;
# asking for more jobs means the current ones are done
debug "removing complete jobs for worker $wid from local queue";
$queue->search({wid => $wid})->delete;
debug "searching for $max new jobs for worker $wid (role $role)";
my $rs = $queue->search(
{role => $role, wid => 0},
{rows => $max},
@@ -66,6 +70,7 @@ sub take_jobs {
return [] if $rs->count == 0;
my @rows = $rs->all;
debug sprintf "booking out %s jobs to worker %s", scalar @rows, $wid;
$rs->update({wid => $wid});
return [ map {{$_->get_columns}} @rows ];
@@ -73,6 +78,7 @@ sub take_jobs {
sub reset_jobs {
my ($wid) = @_;
debug "resetting jobs owned by worked $wid to be available";
return unless $wid > 1;
$queue->search({wid => $wid})
->update({wid => 0});

View File

@@ -13,8 +13,10 @@ with 'App::Netdisco::Daemon::Worker::Interactive::DeviceActions',
sub worker_body {
my $self = shift;
my $wid = $self->wid;
while (1) {
debug "int ($wid): asking for a job";
my $jobs = $self->do('take_jobs', $self->wid, 'Interactive');
foreach my $candidate (@$jobs) {
@@ -23,14 +25,17 @@ sub worker_body {
# (will throw an exception)
my $job = schema('daemon')->resultset('Admin')
->new_result($candidate);
my $jid = $job->id;
my $target = 'set_'. $job->action;
next unless $self->can($target);
info "int ($wid): can ${target}() for job $jid";
# do job
my ($status, $log);
try {
$job->started(scalar localtime);
debug sprintf "int (%s): starting job %s at %s", $wid, $jid, $job->started;
($status, $log) = $self->$target($job);
}
catch {
@@ -39,9 +44,11 @@ sub worker_body {
$self->sendto('stderr', $log ."\n");
};
info "int ($wid): wrapping up job $jid - status $status";
$self->close_job($job, $status, $log);
}
debug "int ($wid): sleeping now...";
sleep( setting('daemon_sleep_time') || 5 );
}
}

View File

@@ -19,19 +19,27 @@ my $role_map = {
sub worker_begin {
my $self = shift;
my $wid = $self->wid;
debug "entering Manager ($wid) worker_begin()";
# requeue jobs locally
debug "mgr ($wid): searching for jobs booked to this processing node";
my $rs = schema('netdisco')->resultset('Admin')
->search({status => "queued-$fqdn"});
my @jobs = map {{$_->get_columns}} $rs->all;
map { $_->{role} = $role_map->{$_->{action}} } @jobs;
$self->do('add_jobs', \@jobs);
if (scalar @jobs) {
info sprintf "mgr (%s): found %s jobs booked to this processing node", $wid, scalar @jobs;
map { $_->{role} = $role_map->{$_->{action}} } @jobs;
$self->do('add_jobs', \@jobs);
}
}
sub worker_body {
my $self = shift;
my $wid = $self->wid;
my $num_slots = $self->do('num_workers');
# get some pending jobs
@@ -42,15 +50,23 @@ sub worker_body {
);
while (1) {
debug "mgr ($wid): getting potential jobs for $num_slots workers";
while (my $job = $rs->next) {
my $jid = $job->job;
# filter for discover_*
next unless is_discoverable($job->device);
info sprintf "mgr (%s): job %s is discoverable", $wid, $jid;
# check for available local capacity
next unless $self->do('capacity_for', $job->action);
info sprintf "mgr (%s): processing node has capacity for job %s (%s)",
$wid, $jid, $job->action;
# mark job as running
next unless $self->lock_job($job);
info sprintf "mgr (%s): job %s booked out for this processing node",
$wid, $jid;
my $local_job = { $job->get_columns };
$local_job->{role} = $role_map->{$job->action};
@@ -64,6 +80,7 @@ sub worker_body {
# TODO also check for stale jobs in Netdisco DB
debug "mgr ($wid): sleeping now...";
sleep( setting('daemon_sleep_time') || 5 );
}
}