diff --git a/src/CloudTasksQueue.php b/src/CloudTasksQueue.php index c066687..dcb9e51 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -11,6 +11,7 @@ use Google\Protobuf\Timestamp; use Illuminate\Contracts\Queue\Queue as QueueContract; use Illuminate\Queue\Queue as LaravelQueue; +use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Stackkit\LaravelGoogleCloudTasksQueue\Events\TaskCreated; use function Safe\json_decode; @@ -197,7 +198,7 @@ private function taskName(string $queueName, array $payload): string $this->config['project'], $this->config['location'], $queueName, - $displayName . '-' . $payload['uuid'] + $displayName . '-' . $payload['uuid'] . '-' . Carbon::now()->getTimestamp(), ); } diff --git a/tests/TaskHandlerTest.php b/tests/TaskHandlerTest.php index 74edc17..da2114c 100644 --- a/tests/TaskHandlerTest.php +++ b/tests/TaskHandlerTest.php @@ -4,9 +4,11 @@ use Firebase\JWT\ExpiredException; use Google\Cloud\Tasks\V2\RetryConfig; +use Google\Cloud\Tasks\V2\Task; use Google\Protobuf\Duration; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Log; use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi; @@ -475,4 +477,33 @@ public function attempts_are_copied_from_x_header() return $event->job->attempts() === 7; }); } + + /** + * @test + */ + public function retried_jobs_get_a_new_name() + { + // Arrange + OpenIdVerificator::fake(); + Event::fake($this->getJobReleasedAfterExceptionEvent()); + CloudTasksApi::fake(); + + // Act & Assert + Carbon::setTestNow(Carbon::createFromTimestamp(1685035628)); + $job = $this->dispatch(new FailingJob()); + Carbon::setTestNow(Carbon::createFromTimestamp(1685035629)); + + $job->run(); + + // Assert + CloudTasksApi::assertCreatedTaskCount(2); + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + [$timestamp] = array_reverse(explode('-', $task->getName())); + return $timestamp === '1685035628'; + }); + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + [$timestamp] = array_reverse(explode('-', $task->getName())); + return $timestamp === '1685035629'; + }); + } }