Skip to content

Fix short task name passed instead of fully qualified #144

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 2 commits into from
Apr 7, 2024
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
15 changes: 14 additions & 1 deletion src/IncomingTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'];
Expand Down
2 changes: 1 addition & 1 deletion src/TaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/IncomingTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/DispatchedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down