From 90d88686500fabc8f94a84b0bd696b8400225b7e Mon Sep 17 00:00:00 2001 From: Marick van Tuil Date: Sun, 7 Apr 2024 13:48:43 +0200 Subject: [PATCH 1/2] Fix short task name passed instead of fully qualified --- src/IncomingTask.php | 15 ++++++++++++++- src/TaskHandler.php | 2 +- tests/IncomingTaskTest.php | 2 +- tests/Support/DispatchedJob.php | 3 ++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/IncomingTask.php b/src/IncomingTask.php index f433c45..5d4a664 100644 --- a/src/IncomingTask.php +++ b/src/IncomingTask.php @@ -5,6 +5,7 @@ namespace Stackkit\LaravelGoogleCloudTasksQueue; use Error; +use Google\Cloud\Tasks\V2\Client\CloudTasksClient; use Illuminate\Contracts\Encryption\Encrypter; use Safe\Exceptions\JsonException; @@ -51,13 +52,25 @@ public function queue(): string return config('queue.connections.'.$this->connection().'.queue'); } - public function taskName(): string + public function shortTaskName(): string { return request()->header('X-CloudTasks-TaskName') ?? request()->header('X-AppEngine-TaskName') ?? throw new Error('Unable to extract taskname from header'); } + public function fullyQualifiedTaskName(): string + { + $config = config('queue.connections.'.$this->connection()); + + return CloudTasksClient::taskName( + project: $config['project'], + location: $config['location'], + queue: $this->queue(), + task: $this->shortTaskName(), + ); + } + public function command(): array { $command = $this->task['data']['command']; diff --git a/src/TaskHandler.php b/src/TaskHandler.php index 68391e6..2cab73e 100644 --- a/src/TaskHandler.php +++ b/src/TaskHandler.php @@ -28,7 +28,7 @@ public function handle(?string $task = null): void abort(422, 'Invalid task payload'); } - if (! CloudTasksApi::exists($task->taskName())) { + if (! CloudTasksApi::exists($task->fullyQualifiedTaskName())) { abort(404); } diff --git a/tests/IncomingTaskTest.php b/tests/IncomingTaskTest.php index c5002e9..2a3d595 100644 --- a/tests/IncomingTaskTest.php +++ b/tests/IncomingTaskTest.php @@ -41,7 +41,7 @@ public function it_reads_the_incoming_task(string $job, string $taskType) // Assert Event::assertDispatched(function (TaskIncoming $event) use ($job) { - return $event->task->taskName() === 'projects/my-test-project/locations/europe-west6/queues/barbequeue/tasks/01HSR4V9QE2F4T0K8RBAYQ88KE-'.class_basename($job) + return $event->task->shortTaskName() === '01HSR4V9QE2F4T0K8RBAYQ88KE-'.class_basename($job) && $event->task->connection() === 'my-cloudtasks-connection' && $event->task->queue() === 'barbequeue'; }); diff --git a/tests/Support/DispatchedJob.php b/tests/Support/DispatchedJob.php index a6fcf7e..781ef17 100644 --- a/tests/Support/DispatchedJob.php +++ b/tests/Support/DispatchedJob.php @@ -31,11 +31,12 @@ public function run(): void default => throw new Error('Task does not have a request.'), }; + $this->testCase->call( method: 'POST', uri: route('cloud-tasks.handle-task'), server: [ - $header => $this->task->getName(), + $header => (string) str($this->task->getName())->after('/tasks/'), ], content: $this->payload, ); From 1d65da7a2cfa455c82d1edd8773dcdc38e75b07c Mon Sep 17 00:00:00 2001 From: Marick van Tuil Date: Sun, 7 Apr 2024 13:50:28 +0200 Subject: [PATCH 2/2] Pint --- tests/IncomingTaskTest.php | 2 +- tests/Support/DispatchedJob.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/IncomingTaskTest.php b/tests/IncomingTaskTest.php index 2a3d595..16b49b8 100644 --- a/tests/IncomingTaskTest.php +++ b/tests/IncomingTaskTest.php @@ -41,7 +41,7 @@ public function it_reads_the_incoming_task(string $job, string $taskType) // Assert Event::assertDispatched(function (TaskIncoming $event) use ($job) { - return $event->task->shortTaskName() === '01HSR4V9QE2F4T0K8RBAYQ88KE-'.class_basename($job) + return $event->task->fullyQualifiedTaskName() === 'projects/my-test-project/locations/europe-west6/queues/barbequeue/tasks/01HSR4V9QE2F4T0K8RBAYQ88KE-'.class_basename($job) && $event->task->connection() === 'my-cloudtasks-connection' && $event->task->queue() === 'barbequeue'; }); diff --git a/tests/Support/DispatchedJob.php b/tests/Support/DispatchedJob.php index 781ef17..2ee248a 100644 --- a/tests/Support/DispatchedJob.php +++ b/tests/Support/DispatchedJob.php @@ -31,7 +31,6 @@ public function run(): void default => throw new Error('Task does not have a request.'), }; - $this->testCase->call( method: 'POST', uri: route('cloud-tasks.handle-task'),