Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
```

Expand Down
5 changes: 5 additions & 0 deletions src/CloudTasksQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use Exception;
use Google\Protobuf\Duration;
use Illuminate\Support\Str;

use function Safe\json_decode;
Expand Down Expand Up @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}