move capacity check to Queue package
This commit is contained in:
		| @@ -5,7 +5,7 @@ use Dancer::Plugin::DBIC 'schema'; | ||||
|  | ||||
| use base 'Exporter'; | ||||
| our @EXPORT = (); | ||||
| our @EXPORT_OK = qw/ add_jobs take_jobs reset_jobs /; | ||||
| our @EXPORT_OK = qw/ add_jobs capacity_for take_jobs reset_jobs /; | ||||
| our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | ||||
|  | ||||
| schema('daemon')->deploy; | ||||
| @@ -16,6 +16,30 @@ sub add_jobs { | ||||
|   $queue->populate($jobs); | ||||
| } | ||||
|  | ||||
| sub capacity_for { | ||||
|   my ($action) = @_; | ||||
|  | ||||
|   my $action_map = { | ||||
|     Interactive => [qw/location contact portcontrol portname vlan power/] | ||||
|   }; | ||||
|  | ||||
|   my $role_map = { | ||||
|     map {$_ => 'Interactive'} @{ $action_map->{Interactive} } | ||||
|   }; | ||||
|  | ||||
|   my $setting_map = { | ||||
|     Poller => 'daemon_pollers', | ||||
|     Interactive => 'daemon_interactives', | ||||
|   }; | ||||
|  | ||||
|   my $role = $role_map->{$action}; | ||||
|   my $setting = $setting_map->{$role}; | ||||
|  | ||||
|   my $current = $queue->search({role => $role})->count; | ||||
|  | ||||
|   return ($current < setting($setting)); | ||||
| } | ||||
|  | ||||
| sub take_jobs { | ||||
|   my ($wid, $role, $max) = @_; | ||||
|   $max ||= 1; | ||||
|   | ||||
| @@ -12,13 +12,9 @@ use namespace::clean; | ||||
|  | ||||
| my $fqdn = hostfqdn || 'localhost'; | ||||
|  | ||||
| # forward and reverse mappings for worker role to Netdisco job type (action) | ||||
| # this needs updating when we invent new job types | ||||
| my $action_map = { | ||||
|   Interactive => [qw/location contact portcontrol portname vlan power/] | ||||
| }; | ||||
| my $role_map = { | ||||
|   map {$_ => 'Interactive'} @{ $action_map->{Interactive} } | ||||
|   map {$_ => 'Interactive'} | ||||
|       qw/location contact portcontrol portname vlan power/ | ||||
| }; | ||||
|  | ||||
| sub worker_begin { | ||||
| @@ -47,7 +43,7 @@ sub worker_body { | ||||
|           next unless is_discoverable($job->device); | ||||
|  | ||||
|           # check for available local capacity | ||||
|           next unless $self->capacity_for($job); | ||||
|           next unless $self->do('capacity_for', $job->action); | ||||
|  | ||||
|           # mark job as running | ||||
|           next unless $self->lock_job($job); | ||||
| @@ -68,23 +64,6 @@ sub worker_body { | ||||
|   } | ||||
| } | ||||
|  | ||||
| sub capacity_for { | ||||
|   my ($self, $job) = @_; | ||||
|  | ||||
|   my $setting_map = { | ||||
|     Poller => 'daemon_pollers', | ||||
|     Interactive => 'daemon_interactives', | ||||
|   }; | ||||
|  | ||||
|   my $role = $role_map->{$job->action}; | ||||
|   my $setting = $setting_map->{$role}; | ||||
|  | ||||
|   my $current = schema('daemon')->resultset('Admin') | ||||
|     ->search({role => $role})->count; | ||||
|  | ||||
|   return ($current < setting($setting)); | ||||
| } | ||||
|  | ||||
| sub lock_job { | ||||
|   my ($self, $job) = @_; | ||||
|   my $happy = 0; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user