remove job type knowledge from code into config
This commit is contained in:
@@ -13,7 +13,7 @@ __PACKAGE__->add_columns(
|
||||
is_nullable => 0,
|
||||
},
|
||||
|
||||
"role", # Poller, Interactive, etc
|
||||
"type", # Poller, Interactive, etc
|
||||
{ data_type => "text", is_nullable => 0 },
|
||||
|
||||
"wid", # worker ID, only != 0 once taken
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package App::Netdisco::Daemon::Queue;
|
||||
package App::Netdisco::Daemon::LocalQueue;
|
||||
|
||||
use Dancer qw/:moose :syntax :script/;
|
||||
use Dancer::Plugin::DBIC 'schema';
|
||||
@@ -18,49 +18,25 @@ sub add_jobs {
|
||||
}
|
||||
|
||||
sub capacity_for {
|
||||
my ($action) = @_;
|
||||
my ($type) = @_;
|
||||
debug "checking local capacity for action $action";
|
||||
|
||||
my $action_map = {
|
||||
Poller => [
|
||||
qw/discoverall discover arpwalk arpnip macwalk macsuck nbtstat nbtwalk expire/
|
||||
],
|
||||
Interactive => [qw/location contact portcontrol portname vlan power/],
|
||||
};
|
||||
|
||||
my $role_map = {
|
||||
(map {$_ => 'Poller'} @{ $action_map->{Poller} }),
|
||||
(map {$_ => 'Interactive'} @{ $action_map->{Interactive} })
|
||||
};
|
||||
|
||||
my $setting_map = {
|
||||
Poller => 'pollers',
|
||||
Interactive => 'interactives',
|
||||
};
|
||||
|
||||
my $role = $role_map->{$action};
|
||||
my $setting = $setting_map->{$role};
|
||||
|
||||
my $current = $queue->search({role => $role})->count;
|
||||
|
||||
return ($current < setting('workers')->{$setting});
|
||||
my $setting = setting('workers')->{ $job_type_keys->{$type} };
|
||||
my $current = $queue->search({type => $type})->count;
|
||||
return ($current < $setting);
|
||||
}
|
||||
|
||||
sub take_jobs {
|
||||
my ($wid, $role, $max) = @_;
|
||||
$max ||= 1;
|
||||
my ($wid, $type, $max) = @_;
|
||||
|
||||
# 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;
|
||||
scrub_jobs($wid);
|
||||
|
||||
debug "searching for $max new jobs for worker $wid (role $role)";
|
||||
my $rs = $queue->search(
|
||||
{role => $role, wid => 0},
|
||||
{rows => $max},
|
||||
);
|
||||
|
||||
my @rows = $rs->all;
|
||||
debug "searching for $max new jobs for worker $wid (type $type)";
|
||||
my @rows = $queue->search(
|
||||
{type => $type, wid => 0},
|
||||
{rows => ($max || 1)},
|
||||
)->all;
|
||||
return [] if scalar @rows == 0;
|
||||
|
||||
debug sprintf "booking out %s jobs to worker %s", scalar @rows, $wid;
|
||||
@@ -79,7 +55,7 @@ sub reset_jobs {
|
||||
|
||||
sub scrub_jobs {
|
||||
my ($wid) = @_;
|
||||
debug "deleting jobs owned by worker $wid";
|
||||
debug "deleting dangling jobs owned by worker $wid";
|
||||
return unless $wid > 1;
|
||||
$queue->search({wid => $wid})->delete;
|
||||
}
|
||||
|
||||
@@ -11,13 +11,6 @@ use namespace::clean;
|
||||
|
||||
my $fqdn = hostfqdn || 'localhost';
|
||||
|
||||
my $role_map = {
|
||||
(map {$_ => 'Poller'}
|
||||
qw/discoverall discover arpwalk arpnip macwalk macsuck nbtstat nbtwalk expire/),
|
||||
(map {$_ => 'Interactive'}
|
||||
qw/location contact portcontrol portname vlan power/)
|
||||
};
|
||||
|
||||
sub worker_begin {
|
||||
my $self = shift;
|
||||
my $wid = $self->wid;
|
||||
@@ -32,7 +25,7 @@ sub worker_begin {
|
||||
|
||||
if (scalar @jobs) {
|
||||
info sprintf "mgr (%s): found %s jobs booked to this processing node", $wid, scalar @jobs;
|
||||
map { $_->{role} = $role_map->{$_->{action}} } @jobs;
|
||||
map { $_->{type} = setting('job_types')->{$_->{action}} } @jobs;
|
||||
|
||||
$self->do('add_jobs', \@jobs);
|
||||
}
|
||||
@@ -55,9 +48,10 @@ sub worker_body {
|
||||
debug "mgr ($wid): getting potential jobs for $num_slots workers";
|
||||
while (my $job = $rs->next) {
|
||||
my $jid = $job->job;
|
||||
my $job_type = setting('job_types')->{$job->action} or next;
|
||||
|
||||
# check for available local capacity
|
||||
next unless $self->do('capacity_for', $job->action);
|
||||
next unless $self->do('capacity_for', $job_type);
|
||||
debug sprintf "mgr (%s): processing node has capacity for job %s (%s)",
|
||||
$wid, $jid, $job->action;
|
||||
|
||||
@@ -67,7 +61,7 @@ sub worker_body {
|
||||
$wid, $jid;
|
||||
|
||||
my $local_job = { $job->get_columns };
|
||||
$local_job->{role} = $role_map->{$job->action};
|
||||
$local_job->{type} = $job_type;
|
||||
|
||||
# copy job to local queue
|
||||
$self->do('add_jobs', [$local_job]);
|
||||
|
||||
Reference in New Issue
Block a user