diff --git a/lib/App/Netdisco/DB/Result/DeviceSkip.pm b/lib/App/Netdisco/DB/Result/DeviceSkip.pm index ddd3db53..238ae36d 100644 --- a/lib/App/Netdisco/DB/Result/DeviceSkip.pm +++ b/lib/App/Netdisco/DB/Result/DeviceSkip.pm @@ -18,6 +18,25 @@ __PACKAGE__->add_columns( "skipover", { data_type => "boolean", is_nullable => 1, default_value => \'false' }, ); + __PACKAGE__->set_primary_key("backend", "device", "action"); +__PACKAGE__->add_unique_constraint( + device_skip_pkey => [qw/backend device action/]); + +=head1 METHODS + +=head2 increment_deferrals + +Increments the C field in the row, only if the row is in storage. +There is a race in the update, but this is not worrying for now. + +=cut + +sub increment_deferrals { + my $row = shift; + return unless $row->in_storage; + return $row->update({ deferrals => ($row->deferrals + 1) }); +} + 1; diff --git a/lib/App/Netdisco/JobQueue/PostgreSQL.pm b/lib/App/Netdisco/JobQueue/PostgreSQL.pm index fd1ad255..48d62f62 100644 --- a/lib/App/Netdisco/JobQueue/PostgreSQL.pm +++ b/lib/App/Netdisco/JobQueue/PostgreSQL.pm @@ -189,11 +189,16 @@ sub jq_lock { sub jq_defer { my $job = shift; + my $fqdn = hostfqdn || 'localhost'; my $happy = false; try { - # lock db row and update to show job is available schema('netdisco')->txn_do(sub { + schema('netdisco')->resultset('DeviceSkip')->find_or_create({ + backend => $fqdn, device => $job->device, action => $job->action, + },{ key => 'device_skip_pkey' })->increment_deferrals; + + # lock db row and update to show job is available schema('netdisco')->resultset('Admin') ->find($job->job, {for => 'update'}) ->update({ status => 'queued', started => undef });