Skip to content

Commit fb054cb

Browse files
Merge pull request #178 from aaajeetee/master
Can configure the Cloud Task dispatch_deadline option. Default is 10 …
2 parents e20299b + f4b7fd2 commit fb054cb

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Add a new queue connection to `config/queue.php`
4343

4444
'backoff' => 0,
4545
'after_commit' => false,
46+
// enable this if you want to set a non-default Google Cloud Tasks dispatch timeout
47+
//'dispatch_deadline' => 1800, // in seconds
4648
],
4749
```
4850

src/CloudTasksQueue.php

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Closure;
88
use Exception;
9+
use Google\Protobuf\Duration;
910
use Illuminate\Support\Str;
1011

1112
use function Safe\json_decode;
@@ -282,6 +283,10 @@ public function addPayloadToTask(array $payload, Task $task, $job): Task
282283
$token->setServiceAccountEmail($this->config['service_account_email'] ?? '');
283284
$httpRequest->setOidcToken($token);
284285
$task->setHttpRequest($httpRequest);
286+
287+
if (! empty($this->config['dispatch_deadline'])) {
288+
$task->setDispatchDeadline((new Duration())->setSeconds($this->config['dispatch_deadline']));
289+
}
285290
}
286291

287292
return $task;

tests/QueueTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,51 @@ public function it_can_dispatch_closures(): void
566566
// Assert
567567
Event::assertDispatched(fn (JobOutput $event) => $event->output === 'ClosureJob:success');
568568
}
569+
570+
#[Test]
571+
public function task_has_no_dispatch_deadline_by_default(): void
572+
{
573+
// Arrange
574+
CloudTasksApi::fake();
575+
576+
// Act
577+
$this->dispatch(new SimpleJob());
578+
579+
// Assert
580+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
581+
return $task->getDispatchDeadline() === null;
582+
});
583+
}
584+
585+
#[Test]
586+
public function task_has_no_dispatch_deadline_if_config_is_empty(): void
587+
{
588+
// Arrange
589+
CloudTasksApi::fake();
590+
$this->setConfigValue('dispatch_deadline', null);
591+
592+
// Act
593+
$this->dispatch(new SimpleJob());
594+
595+
// Assert
596+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
597+
return $task->getDispatchDeadline() === null;
598+
});
599+
}
600+
601+
#[Test]
602+
public function task_has_configured_dispatch_deadline(): void
603+
{
604+
// Arrange
605+
CloudTasksApi::fake();
606+
$this->setConfigValue('dispatch_deadline', 1800);
607+
608+
// Act
609+
$this->dispatch(new SimpleJob());
610+
611+
// Assert
612+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
613+
return $task->getDispatchDeadline()->getSeconds() === 1800;
614+
});
615+
}
569616
}

0 commit comments

Comments
 (0)