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..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->taskName() === 'projects/my-test-project/locations/europe-west6/queues/barbequeue/tasks/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 a6fcf7e..2ee248a 100644 --- a/tests/Support/DispatchedJob.php +++ b/tests/Support/DispatchedJob.php @@ -35,7 +35,7 @@ public function run(): void method: 'POST', uri: route('cloud-tasks.handle-task'), server: [ - $header => $this->task->getName(), + $header => (string) str($this->task->getName())->after('/tasks/'), ], content: $this->payload, );