Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Leverage Google Cloud Task deduplication mechanism #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/CloudTasksQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ function ($payload, $queue) use ($job) {
);
}

protected function createPayloadArray($job, $queue, $data = '')
{
$payload = parent::createPayloadArray($job, $queue, $data);

$cloudTaskId = $this->getCloudTaskId($job);
if (!empty($cloudTaskId)) {
$payload['cloudTaskId'] = $cloudTaskId;
}

return $payload;
}

protected function getCloudTaskId($job)
{
return property_exists($job, 'cloudTaskId') ? $job->cloudTaskId : null;
}

/**
* Push a raw payload onto the queue.
*
Expand Down Expand Up @@ -165,7 +182,11 @@ protected function pushToCloudTasks($queue, $payload, $delay, mixed $job)

$payload = (array) json_decode($payload, true);

$task = tap(new Task)->setName($this->taskName($queue, $payload['displayName']));
if (isset($payload['cloudTaskId'])) {
$task = tap(new Task())->setName($this->taskNameWithId($queue, $payload['cloudTaskId']));
} else {
$task = tap(new Task())->setName($this->taskName($queue, $payload['displayName']));
}

$payload = $this->enrichPayloadWithAttempts($payload);

Expand Down Expand Up @@ -199,6 +220,16 @@ private function taskName(string $queueName, string $displayName): string
);
}

private function taskNameWithId(string $queueName, string $taskId): string
{
return CloudTasksClient::taskName(
$this->config['project'],
$this->config['location'],
$queueName,
$taskId,
);
}

private function enrichPayloadWithAttempts(
array $payload,
): array {
Expand Down
Loading