From 147fa4d029a6fcbbb4d63c12812fac66773e8742 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 26 Aug 2021 17:32:32 -0400 Subject: [PATCH] Result::TaskRetries: Teach about requeue --- src/lib/Hydra/Schema/Result/TaskRetries.pm | 12 +++++++- t/Schema/Result/TaskRetries.t | 35 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 t/Schema/Result/TaskRetries.t diff --git a/src/lib/Hydra/Schema/Result/TaskRetries.pm b/src/lib/Hydra/Schema/Result/TaskRetries.pm index 08c7e8f6..538252de 100644 --- a/src/lib/Hydra/Schema/Result/TaskRetries.pm +++ b/src/lib/Hydra/Schema/Result/TaskRetries.pm @@ -105,6 +105,16 @@ __PACKAGE__->set_primary_key("id"); # Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-08-26 16:30:59 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:4MC8UnsgrvJVRrIURvSH5A +use Hydra::Math qw(exponential_backoff); + +sub requeue { + my ($self) = @_; + + $self->update({ + attempts => $self->attempts + 1, + retry_at => time() + exponential_backoff($self->attempts + 1), + }); + +} -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/t/Schema/Result/TaskRetries.t b/t/Schema/Result/TaskRetries.t new file mode 100644 index 00000000..0425f11c --- /dev/null +++ b/t/Schema/Result/TaskRetries.t @@ -0,0 +1,35 @@ +use strict; +use warnings; +use Setup; + +my %ctx = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; + +use Test2::V0; + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +my $taskretries = $db->resultset('TaskRetries'); + +subtest "requeue" => sub { + my $task = $taskretries->create({ + channel => "bogus", + pluginname => "bogus", + payload => "bogus", + attempts => 1, + retry_at => time(), + }); + + $task->requeue(); + is($task->attempts, 2, "We should have stored a second retry"); + is($task->retry_at, within(time() + 4, 2), "Delayed two exponential backoff step"); + + $task->requeue(); + is($task->attempts, 3, "We should have stored a third retry"); + is($task->retry_at, within(time() + 8, 2), "Delayed a third exponential backoff step"); +}; + +done_testing;