Skip to content

Commit

Permalink
fix: Take next job with the oldest perform_at
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Apr 6, 2023
1 parent 7bf78bc commit ebc484a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/models/dao/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function findNextJob($queue)
AND perform_at <= :perform_at
AND (number_attempts <= 25 OR frequency != '')
{$queue_placeholder}
ORDER BY frequency, created_at;
ORDER BY perform_at ASC
SQL;

$statement = $this->prepare($sql);
Expand Down
39 changes: 4 additions & 35 deletions tests/models/dao/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ public function testFindNextJobReturnsJobTakingTheOldestOne()
$this->freeze($now);
$minutes_ago = $this->fake('randomNumber');
$number_attempts = $this->fake('numberBetween', 0, 25);
$created_at_1 = \Minz\Time::ago($minutes_ago, 'minutes');
$created_at_2 = \Minz\Time::ago($minutes_ago + 5, 'minutes');
$perform_at_1 = \Minz\Time::ago($minutes_ago, 'minutes');
$perform_at_2 = \Minz\Time::ago($minutes_ago + 5, 'minutes');
$job_dao = new Job();
$job_id_1 = $this->create('job', [
'created_at' => $created_at_1->format(\Minz\Model::DATETIME_FORMAT),
'perform_at' => \Minz\Time::ago($minutes_ago, 'minutes')->format(\Minz\Model::DATETIME_FORMAT),
'perform_at' => $perform_at_1->format(\Minz\Model::DATETIME_FORMAT),
'locked_at' => null,
'number_attempts' => $number_attempts,
'frequency' => '',
]);
$job_id_2 = $this->create('job', [
'created_at' => $created_at_2->format(\Minz\Model::DATETIME_FORMAT),
'perform_at' => \Minz\Time::ago($minutes_ago, 'minutes')->format(\Minz\Model::DATETIME_FORMAT),
'perform_at' => $perform_at_2->format(\Minz\Model::DATETIME_FORMAT),
'locked_at' => null,
'number_attempts' => $number_attempts,
'frequency' => '',
Expand All @@ -57,35 +55,6 @@ public function testFindNextJobReturnsJobTakingTheOldestOne()
$this->assertSame($job_id_2, $db_job['id']);
}

public function testFindNextJobGivesPriorityToJobsWithNoFrequency()
{
$now = $this->fake('dateTime');
$this->freeze($now);
$minutes_ago = $this->fake('randomNumber');
$number_attempts = $this->fake('numberBetween', 0, 25);
$created_at_1 = \Minz\Time::ago($minutes_ago, 'minutes');
$created_at_2 = \Minz\Time::ago($minutes_ago + 5, 'minutes');
$job_dao = new Job();
$job_id_1 = $this->create('job', [
'created_at' => $created_at_1->format(\Minz\Model::DATETIME_FORMAT),
'perform_at' => \Minz\Time::ago($minutes_ago, 'minutes')->format(\Minz\Model::DATETIME_FORMAT),
'locked_at' => null,
'number_attempts' => $number_attempts,
'frequency' => '',
]);
$job_id_2 = $this->create('job', [
'created_at' => $created_at_2->format(\Minz\Model::DATETIME_FORMAT),
'perform_at' => \Minz\Time::ago($minutes_ago, 'minutes')->format(\Minz\Model::DATETIME_FORMAT),
'locked_at' => null,
'number_attempts' => $number_attempts,
'frequency' => '+5 minutes',
]);

$db_job = $job_dao->findNextJob('all');

$this->assertSame($job_id_1, $db_job['id']);
}

public function testFindNextJobReturnsJobInGivenQueue()
{
$now = $this->fake('dateTime');
Expand Down

0 comments on commit ebc484a

Please sign in to comment.