Skip to content

Commit c08d48d

Browse files
Merge pull request #100 from i386/master
set a name for the cloud task based on display name and UUID
2 parents 9bb7f18 + 7bf7b31 commit c08d48d

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

src/CloudTasksApiFake.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public function getRetryConfig(string $queueName): RetryConfig
3030

3131
public function createTask(string $queueName, Task $task): Task
3232
{
33-
$task->setName(Str::uuid()->toString());
34-
3533
$this->createdTasks[] = compact('queueName', 'task');
3634

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

5755
public function assertTaskDeleted(string $taskName): void
5856
{
59-
$taskUuids = array_map(function ($fullTaskName) {
60-
return Arr::last(explode('/', $fullTaskName));
61-
}, $this->deletedTasks);
62-
6357
Assert::assertTrue(
64-
in_array($taskName, $taskUuids),
58+
in_array($taskName, $this->deletedTasks),
6559
'The task [' . $taskName . '] should have been deleted but it is not.'
6660
);
6761
}
6862

6963
public function assertTaskNotDeleted(string $taskName): void
7064
{
71-
$taskUuids = array_map(function ($fullTaskName) {
72-
return Arr::last(explode('/', $fullTaskName));
73-
}, $this->deletedTasks);
74-
7565
Assert::assertTrue(
76-
! in_array($taskName, $taskUuids),
66+
! in_array($taskName, $this->deletedTasks),
7767
'The task [' . $taskName . '] should not have been deleted but it was.'
7868
);
7969
}

src/CloudTasksQueue.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,22 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
139139
$httpRequest->setUrl($this->getHandler());
140140
$httpRequest->setHttpMethod(HttpMethod::POST);
141141

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

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

152-
$httpRequest->setBody($payload);
154+
$httpRequest->setBody(json_encode($payload));
153155

154156
$task = $this->createTask();
157+
$task->setName($this->taskName($queue, $payload));
155158
$task->setHttpRequest($httpRequest);
156159

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

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

177-
return $uuid;
180+
return $payload['uuid'];
178181
}
179182

180-
private function withUuid(string $payload): array
183+
private function withUuid(array $payload): array
181184
{
182-
/** @var array $decoded */
183-
$decoded = json_decode($payload, true);
184-
185-
if (!isset($decoded['uuid'])) {
186-
$decoded['uuid'] = (string) Str::uuid();
185+
if (!isset($payload['uuid'])) {
186+
$payload['uuid'] = (string) Str::uuid();
187187
}
188188

189-
return [
190-
json_encode($decoded),
191-
$decoded['uuid'],
192-
];
189+
return $payload;
193190
}
194191

195-
private function withAttempts(string $payload): string
192+
private function taskName(string $queueName, array $payload): string
196193
{
197-
/** @var array $decoded */
198-
$decoded = json_decode($payload, true);
194+
$displayName = str_replace("\\", '-', $payload['displayName']);
195+
196+
return CloudTasksClient::taskName(
197+
$this->config['project'],
198+
$this->config['location'],
199+
$queueName,
200+
$displayName . '-' . $payload['uuid']
201+
);
202+
}
199203

200-
if (!isset($decoded['internal']['attempts'])) {
201-
$decoded['internal']['attempts'] = 0;
204+
private function withAttempts(array $payload): array
205+
{
206+
if (!isset($payload['internal']['attempts'])) {
207+
$payload['internal']['attempts'] = 0;
202208
}
203209

204-
return json_encode($decoded);
210+
return $payload;
205211
}
206212

207213
/**

tests/QueueTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,23 @@ public function test_ignoring_jobs_with_deleted_models()
478478
Log::assertLogged('UserJob:John');
479479
CloudTasksApi::assertTaskNotDeleted($job->task->getName());
480480
}
481+
482+
/**
483+
* @test
484+
*/
485+
public function it_adds_a_task_name_based_on_the_display_name()
486+
{
487+
// Arrange
488+
CloudTasksApi::fake();
489+
490+
// Act
491+
$this->dispatch((new SimpleJob()));
492+
493+
// Assert
494+
CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool {
495+
$uuid = \Safe\json_decode($task->getHttpRequest()->getBody(), true)['uuid'];
496+
497+
return $task->getName() === 'projects/my-test-project/locations/europe-west6/queues/barbequeue/tasks/Tests-Support-SimpleJob-' . $uuid;
498+
});
499+
}
481500
}

tests/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ public function dispatch($job)
133133
$payloadAsArray = json_decode($payload, true);
134134
$task = $event->task;
135135

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

139140
dispatch($job);

0 commit comments

Comments
 (0)