Skip to content

Can configure the Cloud Task dispatch_deadline option. Default is 10 … #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2025
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;
});
}
}