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..6c01dbb 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -7,6 +7,7 @@ use Closure; use Exception; use Illuminate\Support\Str; +use Google\Protobuf\Duration; use function Safe\json_decode; use function Safe\json_encode; @@ -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/ConfigHandlerTest.php b/tests/ConfigHandlerTest.php index 193e663..9a3f9ca 100644 --- a/tests/ConfigHandlerTest.php +++ b/tests/ConfigHandlerTest.php @@ -7,13 +7,12 @@ 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 { - /** - * @dataProvider handlerDataProvider - */ + #[DataProvider('handlerDataProvider')] public function test_it_allows_a_handler_url_to_contain_path(string $handler, string $expectedHandler): void { CloudTasksApi::fake(); diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 10a56f8..1e9110e 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; + }); + } }