From b1879132afa70afb65676b2e08e3eb02b3a5a071 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Tue, 7 Sep 2021 10:56:52 -0400 Subject: [PATCH] ResultSet::TaskRetries: add missing method, get_retryable_task Yet again, manual testing is proving to be insufficient. I'm pretty sure I wrote this code but lost it in a rebase, or perhaps the switch to result classes. At any rate, this implements the actual "fetch a retry row and run it" for the hydra-notify daemon. Tested by hand. --- src/lib/Hydra/Schema/ResultSet/TaskRetries.pm | 43 +++++++++++++++++ src/script/hydra-notify | 5 +- t/Schema/ResultSet/TaskRetries.t | 46 ++++++++++++++++++- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/lib/Hydra/Schema/ResultSet/TaskRetries.pm b/src/lib/Hydra/Schema/ResultSet/TaskRetries.pm index 526d3f40..539b3a3b 100644 --- a/src/lib/Hydra/Schema/ResultSet/TaskRetries.pm +++ b/src/lib/Hydra/Schema/ResultSet/TaskRetries.pm @@ -68,4 +68,47 @@ sub save_task { }); } +=head2 get_retryable_task +=cut +sub get_retryable_task { + my ($self) = @_; + + my $row = $self->get_retryable_taskretries_row(); + if (!defined($row)) { + return undef; + } + + my $event = Hydra::Event->new_event( + $row->get_column("channel"), + $row->get_column("payload") + ); + + my $task = Hydra::Task->new($event, $row->get_column("pluginname")); + $task->{"record"} = $row; + + return $task; +} + + +=head2 get_retryable_taskretries_row + +Fetch the next task to retry. + +=cut +sub get_retryable_taskretries_row { + my ($self) = @_; + + my $next_retry = $self->search( + { + 'retry_at' => { '<=', time() } + }, # any task + { + order_by => { + -asc => 'retry_at' + }, + rows => 1, + } + )->first; +} + 1; diff --git a/src/script/hydra-notify b/src/script/hydra-notify index 3459cf2c..ec95a144 100755 --- a/src/script/hydra-notify +++ b/src/script/hydra-notify @@ -131,9 +131,8 @@ while (!$queued_only) { } } - my $task = $taskretries->getRetryableTask(); + my $task = $taskretries->get_retryable_task(); if (defined($task)) { - $task_dispatcher->dispatchTask($task); + $task_dispatcher->dispatch_task($task); } - } diff --git a/t/Schema/ResultSet/TaskRetries.t b/t/Schema/ResultSet/TaskRetries.t index 1fcb4776..4555832c 100644 --- a/t/Schema/ResultSet/TaskRetries.t +++ b/t/Schema/ResultSet/TaskRetries.t @@ -41,7 +41,51 @@ subtest "get_seconds_to_next_retry" => sub { retry_at => time() - 100, }); is($taskretries->get_seconds_to_next_retry(), 0, "We should retry immediately"); - } + }; + + $taskretries->delete_all(); +}; + +subtest "get_retryable_taskretries_row" => sub { + subtest "Without any records in the database" => sub { + is($taskretries->get_retryable_taskretries_row(), undef, "Without any records we have no tasks to retry."); + is($taskretries->get_retryable_task(), undef, "Without any records we have no tasks to retry."); + }; + + subtest "With only tasks whose retry timestamps are in the future" => sub { + $taskretries->create({ + channel => "bogus", + pluginname => "bogus", + payload => "bogus", + attempts => 1, + retry_at => time() + 100, + }); + is($taskretries->get_retryable_taskretries_row(), undef, "We still have nothing to do"); + is($taskretries->get_retryable_task(), undef, "We still have nothing to do"); + }; + + subtest "With tasks whose retry timestamp are in the past" => sub { + $taskretries->create({ + channel => "build_started", + pluginname => "bogus plugin", + payload => "123", + attempts => 1, + retry_at => time() - 100, + }); + + my $row = $taskretries->get_retryable_taskretries_row(); + isnt($row, undef, "We should retry immediately"); + is($row->channel, "build_started", "Channel name should match"); + is($row->pluginname, "bogus plugin", "Plugin name should match"); + is($row->payload, "123", "Payload should match"); + is($row->attempts, 1, "We've had one attempt"); + + my $task = $taskretries->get_retryable_task(); + is($task->{"event"}->{"channel_name"}, "build_started"); + is($task->{"plugin_name"}, "bogus plugin"); + is($task->{"event"}->{"payload"}, "123"); + is($task->{"record"}->get_column("id"), $row->get_column("id")); + }; }; subtest "save_task" => sub {