From 8fd8b1c0b9c87063142133e658112de0a64bf59e Mon Sep 17 00:00:00 2001 From: Auke Terpstra Date: Mon, 7 Apr 2025 22:54:04 +0200 Subject: [PATCH 1/4] Can configure the Cloud Task dispatch_deadline option. Default is 10 minutes, but some tasks take longer than that. --- README.md | 2 ++ src/CloudTasksQueue.php | 5 +++++ tests/QueueTest.php | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/README.md b/README.md index 0114d28..058c27f 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ Add a new queue connection to `config/queue.php` 'backoff' => 0, 'after_commit' => false, + // enable this if you want to set a non-default Google Cloud Tasks dispatch timeout + //'dispatch_deadline' => 1800, // in seconds ], ``` diff --git a/src/CloudTasksQueue.php b/src/CloudTasksQueue.php index 54952a9..40902c1 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -6,6 +6,7 @@ use Closure; use Exception; +use Google\Protobuf\Duration; use Illuminate\Support\Str; use function Safe\json_decode; @@ -282,6 +283,10 @@ public function addPayloadToTask(array $payload, Task $task, $job): Task $token->setServiceAccountEmail($this->config['service_account_email'] ?? ''); $httpRequest->setOidcToken($token); $task->setHttpRequest($httpRequest); + + if (! empty($this->config['dispatch_deadline'])) { + $task->setDispatchDeadline((new Duration())->setSeconds($this->config['dispatch_deadline'])); + } } return $task; diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 10a56f8..5a2e808 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -566,4 +566,51 @@ public function it_can_dispatch_closures(): void // Assert Event::assertDispatched(fn (JobOutput $event) => $event->output === 'ClosureJob:success'); } + + #[Test] + public function task_has_no_dispatch_deadline_by_default(): void + { + // Arrange + CloudTasksApi::fake(); + + // Act + $this->dispatch(new SimpleJob()); + + // Assert + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + return $task->getDispatchDeadline() === null; + }); + } + + #[Test] + public function task_has_no_dispatch_deadline_if_config_is_empty(): void + { + // Arrange + CloudTasksApi::fake(); + $this->setConfigValue('dispatch_deadline', null); + + // Act + $this->dispatch(new SimpleJob()); + + // Assert + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + return $task->getDispatchDeadline() === null; + }); + } + + #[Test] + public function task_has_configured_dispatch_deadline(): void + { + // Arrange + CloudTasksApi::fake(); + $this->setConfigValue('dispatch_deadline', 1800); + + // Act + $this->dispatch(new SimpleJob()); + + // Assert + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + return $task->getDispatchDeadline()->getSeconds() === 1800; + }); + } } From 257c4e98cfd5b07df8fd14f6459962ad188d0fc1 Mon Sep 17 00:00:00 2001 From: Marick van Tuil Date: Wed, 9 Apr 2025 19:50:40 +0200 Subject: [PATCH 2/4] Fix test --- tests/ConfigHandlerTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/ConfigHandlerTest.php b/tests/ConfigHandlerTest.php index 193e663..c4ee1da 100644 --- a/tests/ConfigHandlerTest.php +++ b/tests/ConfigHandlerTest.php @@ -4,6 +4,7 @@ namespace Tests; +use PHPUnit\Framework\Attributes\DataProvider; use Tests\Support\SimpleJob; use Google\Cloud\Tasks\V2\Task; use PHPUnit\Framework\Attributes\Test; @@ -11,9 +12,7 @@ class ConfigHandlerTest extends TestCase { - /** - * @dataProvider handlerDataProvider - */ + #[DataProvider('handlerDataProvider')] public function test_it_allows_a_handler_url_to_contain_path(string $handler, string $expectedHandler): void { CloudTasksApi::fake(); From 43a6df81ed621017c9fbd3df3e783732f3306d56 Mon Sep 17 00:00:00 2001 From: marickvantuil <647007+marickvantuil@users.noreply.github.com> Date: Wed, 9 Apr 2025 17:51:05 +0000 Subject: [PATCH 3/4] Apply code style rules --- tests/ConfigHandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ConfigHandlerTest.php b/tests/ConfigHandlerTest.php index c4ee1da..9a3f9ca 100644 --- a/tests/ConfigHandlerTest.php +++ b/tests/ConfigHandlerTest.php @@ -4,10 +4,10 @@ namespace Tests; -use PHPUnit\Framework\Attributes\DataProvider; use Tests\Support\SimpleJob; use Google\Cloud\Tasks\V2\Task; use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\DataProvider; use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi; class ConfigHandlerTest extends TestCase From fd7617932cb3b7b6a3b8f312120a19b43a1cf781 Mon Sep 17 00:00:00 2001 From: marickvantuil <647007+marickvantuil@users.noreply.github.com> Date: Sat, 12 Apr 2025 10:41:30 +0000 Subject: [PATCH 4/4] Apply code style rules --- src/CloudTasksQueue.php | 4 ++-- tests/QueueTest.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/CloudTasksQueue.php b/src/CloudTasksQueue.php index 40902c1..6c01dbb 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -6,8 +6,8 @@ use Closure; use Exception; -use Google\Protobuf\Duration; use Illuminate\Support\Str; +use Google\Protobuf\Duration; use function Safe\json_decode; use function Safe\json_encode; @@ -285,7 +285,7 @@ public function addPayloadToTask(array $payload, Task $task, $job): Task $task->setHttpRequest($httpRequest); if (! empty($this->config['dispatch_deadline'])) { - $task->setDispatchDeadline((new Duration())->setSeconds($this->config['dispatch_deadline'])); + $task->setDispatchDeadline((new Duration)->setSeconds($this->config['dispatch_deadline'])); } } diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 5a2e808..1e9110e 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -574,7 +574,7 @@ public function task_has_no_dispatch_deadline_by_default(): void CloudTasksApi::fake(); // Act - $this->dispatch(new SimpleJob()); + $this->dispatch(new SimpleJob); // Assert CloudTasksApi::assertTaskCreated(function (Task $task): bool { @@ -590,7 +590,7 @@ public function task_has_no_dispatch_deadline_if_config_is_empty(): void $this->setConfigValue('dispatch_deadline', null); // Act - $this->dispatch(new SimpleJob()); + $this->dispatch(new SimpleJob); // Assert CloudTasksApi::assertTaskCreated(function (Task $task): bool { @@ -606,7 +606,7 @@ public function task_has_configured_dispatch_deadline(): void $this->setConfigValue('dispatch_deadline', 1800); // Act - $this->dispatch(new SimpleJob()); + $this->dispatch(new SimpleJob); // Assert CloudTasksApi::assertTaskCreated(function (Task $task): bool {