File tree Expand file tree Collapse file tree 3 files changed +29
-3
lines changed Expand file tree Collapse file tree 3 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -122,7 +122,16 @@ private function handleTask(array $task): void
122
122
123
123
$ this ->loadQueueRetryConfig ($ job );
124
124
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
+
126
135
$ job ->setMaxTries ($ this ->retryConfig ->getMaxAttempts ());
127
136
128
137
// If the job is being attempted again we also check if a
Original file line number Diff line number Diff line change @@ -389,7 +389,6 @@ public function test_failing_method_on_job()
389
389
// Arrange
390
390
CloudTasksApi::fake ();
391
391
CloudTasksApi::partialMock ()->shouldReceive ('getRetryConfig ' )->andReturn (
392
- // -1 is a valid option in Cloud Tasks to indicate there is no max.
393
392
(new RetryConfig ())->setMaxAttempts (1 )
394
393
);
395
394
Original file line number Diff line number Diff line change @@ -439,7 +439,6 @@ public function failing_jobs_are_released()
439
439
public function attempts_are_tracked_internally ()
440
440
{
441
441
// Arrange
442
- CloudTasksApi::fake ();
443
442
OpenIdVerificator::fake ();
444
443
Event::fake ($ this ->getJobReleasedAfterExceptionEvent ());
445
444
@@ -459,4 +458,23 @@ public function attempts_are_tracked_internally()
459
458
return $ event ->job ->attempts () === 2 ;
460
459
});
461
460
}
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
+ }
462
480
}
You can’t perform that action at this time.
0 commit comments