-
Notifications
You must be signed in to change notification settings - Fork 57
Description
- Minion version: 10.22
- Perl version: v5.30.0
- Operating system: Ubuntu 21.04
Steps to reproduce the behavior
- Enqueue a job that should fail after a set number of attempts
- Wait for it to progress to a failed state, meaning all attempts should be exhaused
- Check if
attemptshas reached0
The following code demonstrates the issue:
#!/usr/bin/env perl
use Mojo::Base -strict;
use Mojo::Pg;
use Mojolicious::Lite;
use Test::Mojo;
use Test2::V0;
my $t = Test::Mojo->new;
helper pg => sub { state $pg = Mojo::Pg->new('postgresql:///') };
plugin Minion => { Pg => app->pg };
my $attempts = 0;
app->minion->add_task(dies => sub { $attempts++; shift->fail(['ouch']) });
app->minion->backoff(sub { 0 });
my $id = app->minion->enqueue(dies => [] => { attempts => 10});
my $task = app->pg->db->select('minion_jobs', '*', { id => $id })->expand->hash;
my $expected = { attempts => 10, retries => 0, result => undef, state => 'inactive' };
like $task, $expected, 'task should be inactive and have 10 remaining attempts';
app->minion->perform_jobs_in_foreground();
$task = app->pg->db->select('minion_jobs', '*', { id => $id })->expand->hash;
$expected = { attempts => 0, retries => 9, result => ['ouch'], state => 'failed' };
like $task, $expected, 'task should have zero attempts remaining and be retried 9 times';
is $attempts, 10, 'ten attempts recorded';
done_testing();Expected behavior
I would expect attempts on the job to be set to 0 when all retries has been exhaused, and retries be set to <attempts> - 1 as that would be the actual number of retries. In other words: total number of attempts = the initial attempt + nine retries.
Expected output from the above script:
# Seeded srand with seed '20210809' from local date.
ok 1 - task should be inactive and have 10 remaining attempts
ok 2 - task should have zero attempts remaining and be retried 9 times
ok 3 - ten attempts recorded
1..3
Actual behavior
The task is marked with the correct number of performed retries, but remaining attempts stops at 1 instead of 0.
Actual output from the script:
# Seeded srand with seed '20210809' from local date.
ok 1 - task should be inactive and have 10 remaining attempts
not ok 2 - task should have zero attempts remaining and be retried 9 times
# Failed test 'task should have zero attempts remaining and be retried 9 times'
# at ./retries-test.t line 28.
# +------------+-----+----+-------+
# | PATH | GOT | OP | CHECK |
# +------------+-----+----+-------+
# | {attempts} | 1 | eq | 0 |
# +------------+-----+----+-------+
ok 3 - ten attempts recorded
1..3
# Looks like you failed 1 test of 3.
Given that attempts steadily decrease as the job is retried I would assume that attempts hit zero when no remaining attempts were left.
To me it seems like the meaning of attempts kind of changes during the task lifecycle. By that I mean that attempts starts out as "how many times the jobs is to be attempted before transitioning to a failed state" but after the task has transitioned to failed the meaning is suddenly "indication that the task had one initial attempt" so that if you summarize attempts and retries you get the same number as initially passed in the attempts-attribute to ->enqueue().