Skip to content

Commit

Permalink
Merge pull request #49 from stackkit/feature/dispatch-deadline
Browse files Browse the repository at this point in the history
Add support for dispatch deadline
  • Loading branch information
marickvantuil authored Apr 9, 2022
2 parents 6eb9bf9 + 23a1f97 commit 9b8368c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
```

Expand Down
5 changes: 5 additions & 0 deletions src/CloudTasksQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 9b8368c

Please sign in to comment.