Skip to content

Commit 4a3481f

Browse files
Merge pull request #106 from stackkit/bugfix/duplicate-job-name
Append timestamp to task name to prevent duplicates
2 parents 0aa376b + 40b2701 commit 4a3481f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/CloudTasksQueue.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Google\Protobuf\Timestamp;
1212
use Illuminate\Contracts\Queue\Queue as QueueContract;
1313
use Illuminate\Queue\Queue as LaravelQueue;
14+
use Illuminate\Support\Carbon;
1415
use Illuminate\Support\Str;
1516
use Stackkit\LaravelGoogleCloudTasksQueue\Events\TaskCreated;
1617
use function Safe\json_decode;
@@ -197,7 +198,7 @@ private function taskName(string $queueName, array $payload): string
197198
$this->config['project'],
198199
$this->config['location'],
199200
$queueName,
200-
$displayName . '-' . $payload['uuid']
201+
$displayName . '-' . $payload['uuid'] . '-' . Carbon::now()->getTimestamp(),
201202
);
202203
}
203204

tests/TaskHandlerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Firebase\JWT\ExpiredException;
66
use Google\Cloud\Tasks\V2\RetryConfig;
7+
use Google\Cloud\Tasks\V2\Task;
78
use Google\Protobuf\Duration;
89
use Illuminate\Queue\Events\JobProcessed;
910
use Illuminate\Queue\Events\JobProcessing;
11+
use Illuminate\Support\Carbon;
1012
use Illuminate\Support\Facades\Event;
1113
use Illuminate\Support\Facades\Log;
1214
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
@@ -475,4 +477,33 @@ public function attempts_are_copied_from_x_header()
475477
return $event->job->attempts() === 7;
476478
});
477479
}
480+
481+
/**
482+
* @test
483+
*/
484+
public function retried_jobs_get_a_new_name()
485+
{
486+
// Arrange
487+
OpenIdVerificator::fake();
488+
Event::fake($this->getJobReleasedAfterExceptionEvent());
489+
CloudTasksApi::fake();
490+
491+
// Act & Assert
492+
Carbon::setTestNow(Carbon::createFromTimestamp(1685035628));
493+
$job = $this->dispatch(new FailingJob());
494+
Carbon::setTestNow(Carbon::createFromTimestamp(1685035629));
495+
496+
$job->run();
497+
498+
// Assert
499+
CloudTasksApi::assertCreatedTaskCount(2);
500+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
501+
[$timestamp] = array_reverse(explode('-', $task->getName()));
502+
return $timestamp === '1685035628';
503+
});
504+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
505+
[$timestamp] = array_reverse(explode('-', $task->getName()));
506+
return $timestamp === '1685035629';
507+
});
508+
}
478509
}

0 commit comments

Comments
 (0)