diff --git a/CHANGELOG.md b/CHANGELOG.md index 12630ea..6e200b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## 3.1.0 - 2022-04-09 + +**Added** + +- Added support for `dispatchDeadline`. See README how to configure. + ## 3.0.0 - 2022-04-03 **Added** diff --git a/README.md b/README.md index 8b4b053..1d31d44 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ Please check the table below for supported Laravel and PHP versions: 'handler' => env('STACKKIT_CLOUD_TASKS_HANDLER', ''), 'queue' => env('STACKKIT_CLOUD_TASKS_QUEUE', 'default'), 'service_account_email' => env('STACKKIT_CLOUD_TASKS_SERVICE_EMAIL', ''), + // Optional: The deadline for requests sent to the worker. If the worker does not + // respond by this deadline then the request is cancelled and the attempt is + // marked as a DEADLINE_EXCEEDED failure. + 'dispatch_deadline' => null, ], ``` diff --git a/src/CloudTasksQueue.php b/src/CloudTasksQueue.php index 883efd9..9a35298 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -7,6 +7,7 @@ use Google\Cloud\Tasks\V2\HttpRequest; use Google\Cloud\Tasks\V2\OidcToken; use Google\Cloud\Tasks\V2\Task; +use Google\Protobuf\Duration; use Google\Protobuf\Timestamp; use Illuminate\Contracts\Queue\Queue as QueueContract; use Illuminate\Queue\Queue as LaravelQueue; @@ -112,6 +113,10 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0) $task = $this->createTask(); $task->setHttpRequest($httpRequest); + if (!empty($this->config['dispatch_deadline'])) { + $task->setDispatchDeadline(new Duration(['seconds' => $this->config['dispatch_deadline']])); + } + $token = new OidcToken; $token->setServiceAccountEmail($this->config['service_account_email']); $httpRequest->setOidcToken($token); diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 88f9fcc..68c1e10 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -85,6 +85,25 @@ public function it_will_set_the_scheduled_time_when_dispatching_later() }); } + /** + * @test + */ + public function test_dispatch_deadline_config() + { + // Arrange + CloudTasksApi::fake(); + $this->setConfigValue('dispatch_deadline', 30); + + // Act + $this->dispatch(new SimpleJob()); + + // Assert + CloudTasksApi::assertTaskCreated(function (Task $task) { + return $task->hasDispatchDeadline() + && $task->getDispatchDeadline()->getSeconds() === 30; + }); + } + /** * @test */