Skip to content

feat(queue): add unique job deduplication support#197

Closed
romanstingler wants to merge 2 commits intostackkit:masterfrom
romanstingler:feature/unique-job-deduplication
Closed

feat(queue): add unique job deduplication support#197
romanstingler wants to merge 2 commits intostackkit:masterfrom
romanstingler:feature/unique-job-deduplication

Conversation

@romanstingler
Copy link
Copy Markdown

In CloudTasksQueue.php, the taskName() method unconditionally prepended a Str::ulid() to every task name, which bypassed Laravel's uniqueFor/uniqueId() mechanism. This caused duplicate tasks because Cloud Tasks saw each dispatch as a unique task name, even when the underlying job was the same.

Changes made:
taskName() method (line 240-259): Now checks if the job is a unique job via isUniqueJob(), meaning it has a uniqueId() method and a truthy uniqueFor property. If so, $job->uniqueId() is used as the task name without the ULID prefix. Non-unique jobs retain the original ULID-prefixed behavior.

pushToCloudTasks() method (line 222-232): Wraps CloudTasksApi::createTask() in a try-catch for ApiException. If the error is a 409 (ALREADY_EXISTS) and the job is unique, the exception is swallowed and the existing UUID is returned, matching Laravel's expected unique job behavior where dispatching a duplicate is a no-op.

isUniqueJob() helper (line 262-268): Extracted the duck-typing check into a single private method to avoid duplication between taskName() and pushToCloudTasks().

With this fix, a HandleOrder job with uniqueFor = 3600 and uniqueId() returning Handle-Order-10-11479956619529 will now create task names like:

projects/.../tasks/Handle-Order-10-11479956619529
Instead of the previous behavior:

projects/.../tasks/01KQWQQPH0902D52ERCJMEE872-Handle-Order-10-11479956619529

Cloud Tasks will reject duplicates with ALREADY_EXISTS, preventing multiple identical tasks from being queued.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant