ResultSet::TaskRetries: add get_seconds_to_next_retry
Get the number of seconds before the next retriable task is ready. This number is specifically intended to be used as a timeout, where `undef` means never time out.
This commit is contained in:
41
src/lib/Hydra/Schema/ResultSet/TaskRetries.pm
Normal file
41
src/lib/Hydra/Schema/ResultSet/TaskRetries.pm
Normal file
@ -0,0 +1,41 @@
|
||||
package Hydra::Schema::ResultSet::TaskRetries;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
use base 'DBIx::Class::ResultSet';
|
||||
use List::Util qw(max);
|
||||
|
||||
=head2 get_seconds_to_next_retry
|
||||
|
||||
Query the database to identify how soon the next retryable task is due
|
||||
for being attempted again.
|
||||
|
||||
If there are no tasks to be reattempted it returns undef.
|
||||
|
||||
If a task's scheduled retry has passed, it returns 0.
|
||||
|
||||
Otherwise, returns the number of seconds from now to look for work.
|
||||
|
||||
=cut
|
||||
sub get_seconds_to_next_retry {
|
||||
my ($self) = @_;
|
||||
|
||||
my $next_retry = $self->search(
|
||||
{}, # any task
|
||||
{
|
||||
order_by => {
|
||||
-asc => 'retry_at'
|
||||
},
|
||||
rows => 1,
|
||||
}
|
||||
)->get_column('retry_at')->first;
|
||||
|
||||
if (defined($next_retry)) {
|
||||
return max(0, $next_retry - time());
|
||||
} else {
|
||||
return undef;
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
Reference in New Issue
Block a user