diff --git a/src/lib/Hydra/Schema/ResultSet/TaskRetries.pm b/src/lib/Hydra/Schema/ResultSet/TaskRetries.pm index 86d16b9f..2284c64e 100644 --- a/src/lib/Hydra/Schema/ResultSet/TaskRetries.pm +++ b/src/lib/Hydra/Schema/ResultSet/TaskRetries.pm @@ -5,6 +5,7 @@ use warnings; use utf8; use base 'DBIx::Class::ResultSet'; use List::Util qw(max); +use Hydra::Math qw(exponential_backoff); =head2 get_seconds_to_next_retry @@ -38,4 +39,32 @@ sub get_seconds_to_next_retry { } } +=head2 save_task + +Save a failing L in the database, with a retry scheduled +for a few seconds away. + +Arguments: + +=over 1 + +=item C<$task> + +L The failing task to retry. + +=back + +=cut +sub save_task { + my ($self, $task) = @_; + + return $self->create({ + channel => $task->{"event"}->{"channel_name"}, + pluginname => $task->{"plugin_name"}, + payload => $task->{"event"}->{"payload"}, + attempts => 1, + retry_at => time() + exponential_backoff(1), + }); +} + 1; diff --git a/t/Schema/ResultSet/TaskRetries.t b/t/Schema/ResultSet/TaskRetries.t index 237ef20a..1fcb4776 100644 --- a/t/Schema/ResultSet/TaskRetries.t +++ b/t/Schema/ResultSet/TaskRetries.t @@ -4,6 +4,8 @@ use Setup; my %ctx = test_init(); +use Hydra::Event; +use Hydra::Task; require Hydra::Schema; require Hydra::Model::DB; @@ -42,4 +44,20 @@ subtest "get_seconds_to_next_retry" => sub { } }; +subtest "save_task" => sub { + my $event = Hydra::Event->new_event("build_started", "1"); + my $task = Hydra::Task->new( + $event, + "FooPluginName", + ); + + my $retry = $taskretries->save_task($task); + + is($retry->channel, "build_started", "Channel name should match"); + is($retry->pluginname, "FooPluginName", "Plugin name should match"); + is($retry->payload, "1", "Payload should match"); + is($retry->attempts, 1, "We've had one attempt"); + is($retry->retry_at, within(time() + 1, 2), "The retry at should be approximately one second away"); +}; + done_testing;