Skip to content

Commit 841471c

Browse files
committed
Potentially improve task insert performance by using hashed random id as task name
1 parent b377efa commit 841471c

File tree

2 files changed

+5
-22
lines changed

2 files changed

+5
-22
lines changed

src/CloudTasksQueue.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
use function Safe\json_decode;
2222
use function Safe\json_encode;
23-
use function Safe\preg_replace;
2423

2524
class CloudTasksQueue extends LaravelQueue implements QueueContract
2625
{
@@ -113,7 +112,7 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
113112

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

116-
$task = tap(new Task())->setName($this->taskName($queue, $payload));
115+
$task = tap(new Task())->setName($this->taskName($queue));
117116

118117
$payload = $this->enrichPayloadWithInternalData(
119118
payload: $payload,
@@ -144,29 +143,16 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
144143
return $payload['uuid'];
145144
}
146145

147-
private function taskName(string $queueName, array $payload): string
146+
private function taskName(string $queueName): string
148147
{
149-
$displayName = $this->sanitizeTaskName($payload['displayName']);
150-
151148
return CloudTasksClient::taskName(
152149
$this->config['project'],
153150
$this->config['location'],
154151
$queueName,
155-
$displayName.'-'.bin2hex(random_bytes(8)),
152+
bin2hex(random_bytes(16)),
156153
);
157154
}
158155

159-
private function sanitizeTaskName(string $taskName): string
160-
{
161-
// Remove all characters that are not -, letters, numbers, or whitespace
162-
$sanitizedName = preg_replace('![^-\pL\pN\s]+!u', '-', $taskName);
163-
164-
// Replace all separator characters and whitespace by a -
165-
$sanitizedName = preg_replace('![-\s]+!u', '-', $sanitizedName);
166-
167-
return trim($sanitizedName, '-');
168-
}
169-
170156
private function enrichPayloadWithInternalData(
171157
array $payload,
172158
string $queueName,

tests/QueueTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public function test_ignoring_jobs_with_deleted_models()
436436
}
437437

438438
#[Test]
439-
public function it_adds_a_task_name_based_on_the_display_name()
439+
public function it_adds_a_pre_defined_task_name()
440440
{
441441
// Arrange
442442
CloudTasksApi::fake();
@@ -446,10 +446,7 @@ public function it_adds_a_task_name_based_on_the_display_name()
446446

447447
// Assert
448448
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
449-
return str_starts_with(
450-
$task->getName(),
451-
'projects/my-test-project/locations/europe-west6/queues/barbequeue/tasks/Tests-Support-SimpleJob'
452-
);
449+
return str($task->getName())->test('/projects\/.+\/locations\/.+\/queues\/.+\/tasks\/[a-z0-9]{16}/');
453450
});
454451
}
455452

0 commit comments

Comments
 (0)