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; + }); + } }