Skip to content

Commit 281a077

Browse files
committed
Provide fallback for older style jobs and ensure their attempt number is not lost
1 parent e45ba17 commit 281a077

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/TaskHandler.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,16 @@ private function handleTask(array $task): void
122122

123123
$this->loadQueueRetryConfig($job);
124124

125-
$job->setAttempts($task['internal']['attempts']);
125+
// If the task has a [X-CloudTasks-TaskRetryCount] header higher than 0, then
126+
// we know the job was created using an earlier version of the package. This
127+
// job does not have the attempts tracked internally yet.
128+
$taskRetryCountHeader = request()->header('X-CloudTasks-TaskRetryCount');
129+
if ($taskRetryCountHeader && (int) $taskRetryCountHeader > 0) {
130+
$job->setAttempts((int) $taskRetryCountHeader);
131+
} else {
132+
$job->setAttempts($task['internal']['attempts']);
133+
}
134+
126135
$job->setMaxTries($this->retryConfig->getMaxAttempts());
127136

128137
// If the job is being attempted again we also check if a

tests/QueueTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ public function test_failing_method_on_job()
389389
// Arrange
390390
CloudTasksApi::fake();
391391
CloudTasksApi::partialMock()->shouldReceive('getRetryConfig')->andReturn(
392-
// -1 is a valid option in Cloud Tasks to indicate there is no max.
393392
(new RetryConfig())->setMaxAttempts(1)
394393
);
395394

tests/TaskHandlerTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ public function failing_jobs_are_released()
439439
public function attempts_are_tracked_internally()
440440
{
441441
// Arrange
442-
CloudTasksApi::fake();
443442
OpenIdVerificator::fake();
444443
Event::fake($this->getJobReleasedAfterExceptionEvent());
445444

@@ -459,4 +458,23 @@ public function attempts_are_tracked_internally()
459458
return $event->job->attempts() === 2;
460459
});
461460
}
461+
462+
/**
463+
* @test
464+
*/
465+
public function attempts_are_copied_from_x_header()
466+
{
467+
// Arrange
468+
OpenIdVerificator::fake();
469+
Event::fake([JobProcessing::class]);
470+
471+
// Act & Assert
472+
$job = $this->dispatch(new SimpleJob());
473+
request()->headers->set('X-CloudTasks-TaskRetryCount', 6);
474+
$job->run();
475+
476+
Event::assertDispatched(JobProcessing::class, function (JobProcessing $event) {
477+
return $event->job->attempts() === 7;
478+
});
479+
}
462480
}

0 commit comments

Comments
 (0)