Skip to content

set a name for the cloud task based on display name and UUID #100

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
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
14 changes: 2 additions & 12 deletions src/CloudTasksApiFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public function getRetryConfig(string $queueName): RetryConfig

public function createTask(string $queueName, Task $task): Task
{
$task->setName(Str::uuid()->toString());

$this->createdTasks[] = compact('queueName', 'task');

return $task;
Expand All @@ -56,24 +54,16 @@ public function getRetryUntilTimestamp(Task $task): ?int

public function assertTaskDeleted(string $taskName): void
{
$taskUuids = array_map(function ($fullTaskName) {
return Arr::last(explode('/', $fullTaskName));
}, $this->deletedTasks);

Assert::assertTrue(
in_array($taskName, $taskUuids),
in_array($taskName, $this->deletedTasks),
'The task [' . $taskName . '] should have been deleted but it is not.'
);
}

public function assertTaskNotDeleted(string $taskName): void
{
$taskUuids = array_map(function ($fullTaskName) {
return Arr::last(explode('/', $fullTaskName));
}, $this->deletedTasks);

Assert::assertTrue(
! in_array($taskName, $taskUuids),
! in_array($taskName, $this->deletedTasks),
'The task [' . $taskName . '] should not have been deleted but it was.'
);
}
Expand Down
44 changes: 25 additions & 19 deletions src/CloudTasksQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,22 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
$httpRequest->setUrl($this->getHandler());
$httpRequest->setHttpMethod(HttpMethod::POST);

$payload = json_decode($payload, true);

// Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it.
// Since we are using and expecting the uuid in some places
// we will add it manually here if it's not present yet.
[$payload, $uuid] = $this->withUuid($payload);
$payload = $this->withUuid($payload);

// Since 3.x tasks are released back onto the queue after an exception has
// been thrown. This means we lose the native [X-CloudTasks-TaskRetryCount] header
// value and need to manually set and update the number of times a task has been attempted.
$payload = $this->withAttempts($payload);

$httpRequest->setBody($payload);
$httpRequest->setBody(json_encode($payload));

$task = $this->createTask();
$task->setName($this->taskName($queue, $payload));
$task->setHttpRequest($httpRequest);

// The deadline for requests sent to the app. If the app does not respond by
Expand All @@ -174,34 +177,37 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)

event((new TaskCreated)->queue($queue)->task($task));

return $uuid;
return $payload['uuid'];
}

private function withUuid(string $payload): array
private function withUuid(array $payload): array
{
/** @var array $decoded */
$decoded = json_decode($payload, true);

if (!isset($decoded['uuid'])) {
$decoded['uuid'] = (string) Str::uuid();
if (!isset($payload['uuid'])) {
$payload['uuid'] = (string) Str::uuid();
}

return [
json_encode($decoded),
$decoded['uuid'],
];
return $payload;
}

private function withAttempts(string $payload): string
private function taskName(string $queueName, array $payload): string
{
/** @var array $decoded */
$decoded = json_decode($payload, true);
$displayName = str_replace("\\", '-', $payload['displayName']);

return CloudTasksClient::taskName(
$this->config['project'],
$this->config['location'],
$queueName,
$displayName . '-' . $payload['uuid']
);
}

if (!isset($decoded['internal']['attempts'])) {
$decoded['internal']['attempts'] = 0;
private function withAttempts(array $payload): array
{
if (!isset($payload['internal']['attempts'])) {
$payload['internal']['attempts'] = 0;
}

return json_encode($decoded);
return $payload;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,23 @@ public function test_ignoring_jobs_with_deleted_models()
Log::assertLogged('UserJob:John');
CloudTasksApi::assertTaskNotDeleted($job->task->getName());
}

/**
* @test
*/
public function it_adds_a_task_name_based_on_the_display_name()
{
// Arrange
CloudTasksApi::fake();

// Act
$this->dispatch((new SimpleJob()));

// Assert
CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool {
$uuid = \Safe\json_decode($task->getHttpRequest()->getBody(), true)['uuid'];

return $task->getName() === 'projects/my-test-project/locations/europe-west6/queues/barbequeue/tasks/Tests-Support-SimpleJob-' . $uuid;
});
}
}
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public function dispatch($job)
$payloadAsArray = json_decode($payload, true);
$task = $event->task;

request()->headers->set('X-Cloudtasks-Taskname', $task->getName());
[,,,,,,,$taskName] = explode('/', $task->getName());
request()->headers->set('X-Cloudtasks-Taskname', $taskName);
});

dispatch($job);
Expand Down