Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3494a12

Browse files
Merge pull request #146 from Plytas/master
Add ability to customize `WorkerOptions`
2 parents e692e30 + 1e3dacb commit 3494a12

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

‎src/CloudTasksQueue.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Google\Protobuf\Timestamp;
1616
use Illuminate\Contracts\Queue\Queue as QueueContract;
1717
use Illuminate\Queue\Queue as LaravelQueue;
18+
use Illuminate\Queue\WorkerOptions;
1819
use Illuminate\Support\Str;
1920
use Stackkit\LaravelGoogleCloudTasksQueue\Events\TaskCreated;
2021

@@ -27,6 +28,9 @@ class CloudTasksQueue extends LaravelQueue implements QueueContract
2728

2829
private static ?Closure $taskHeadersCallback = null;
2930

31+
/** @var (Closure(IncomingTask): WorkerOptions)|null */
32+
private static ?Closure $workerOptionsCallback = null;
33+
3034
public function __construct(public array $config, public CloudTasksClient $client, public $dispatchAfterCommit = false)
3135
{
3236
//
@@ -52,6 +56,27 @@ public static function forgetTaskHeadersCallback(): void
5256
self::$taskHeadersCallback = null;
5357
}
5458

59+
/**
60+
* @param Closure(IncomingTask): WorkerOptions $callback
61+
*/
62+
public static function configureWorkerOptionsUsing(Closure $callback): void
63+
{
64+
static::$workerOptionsCallback = $callback;
65+
}
66+
67+
/**
68+
* @return (Closure(IncomingTask): WorkerOptions)|null
69+
*/
70+
public static function getWorkerOptionsCallback(): ?Closure
71+
{
72+
return self::$workerOptionsCallback;
73+
}
74+
75+
public static function forgetWorkerOptionsCallback(): void
76+
{
77+
self::$workerOptionsCallback = null;
78+
}
79+
5580
/**
5681
* Get the size of the queue.
5782
*

‎src/TaskHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private function run(IncomingTask $task): void
6161
tap(app('cloud-tasks.worker'), fn (Worker $worker) => $worker->process(
6262
connectionName: $job->getConnectionName(),
6363
job: $job,
64-
options: $this->getWorkerOptions()
64+
options: CloudTasksQueue::getWorkerOptionsCallback() ? (CloudTasksQueue::getWorkerOptionsCallback())($task) : $this->getWorkerOptions()
6565
));
6666
}
6767

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Support;
6+
7+
class FailingJobWithNoMaxTries extends FailingJob
8+
{
9+
public $tries = null;
10+
}

‎tests/TaskHandlerTest.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
namespace Tests;
66

77
use Illuminate\Queue\Events\JobReleasedAfterException;
8+
use Illuminate\Queue\WorkerOptions;
89
use Illuminate\Support\Facades\Event;
10+
use Override;
911
use PHPUnit\Framework\Attributes\Test;
1012
use PHPUnit\Framework\Attributes\TestWith;
1113
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
14+
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksQueue;
15+
use Stackkit\LaravelGoogleCloudTasksQueue\IncomingTask;
1216
use Tests\Support\EncryptedJob;
1317
use Tests\Support\FailingJob;
1418
use Tests\Support\FailingJobWithMaxTries;
1519
use Tests\Support\FailingJobWithMaxTriesAndRetryUntil;
20+
use Tests\Support\FailingJobWithNoMaxTries;
1621
use Tests\Support\FailingJobWithRetryUntil;
1722
use Tests\Support\FailingJobWithUnlimitedTries;
1823
use Tests\Support\JobOutput;
@@ -28,6 +33,14 @@ protected function setUp(): void
2833
CloudTasksApi::fake();
2934
}
3035

36+
#[Override]
37+
protected function tearDown(): void
38+
{
39+
parent::tearDown();
40+
41+
CloudTasksQueue::forgetWorkerOptionsCallback();
42+
}
43+
3144
#[Test]
3245
public function it_can_run_a_task()
3346
{
@@ -77,6 +90,38 @@ public function after_max_attempts_it_will_log_to_failed_table()
7790
$this->assertDatabaseCount('failed_jobs', 1);
7891
}
7992

93+
#[Test]
94+
public function uses_worker_options_callback_and_after_max_attempts_it_will_log_to_failed_table()
95+
{
96+
// Arrange
97+
CloudTasksQueue::configureWorkerOptionsUsing(function (IncomingTask $task) {
98+
$queueTries = [
99+
'high' => 5,
100+
'low' => 1,
101+
];
102+
103+
return new WorkerOptions(maxTries: $queueTries[$task->queue()] ?? 1);
104+
});
105+
106+
$job = $this->dispatch(tap(new FailingJobWithNoMaxTries(), fn ($job) => $job->queue = 'high'));
107+
108+
// Act & Assert
109+
$this->assertDatabaseCount('failed_jobs', 0);
110+
111+
$releasedJob = $job->runAndGetReleasedJob();
112+
$this->assertDatabaseCount('failed_jobs', 0);
113+
114+
$releasedJob = $releasedJob->runAndGetReleasedJob();
115+
$this->assertDatabaseCount('failed_jobs', 0);
116+
$releasedJob = $releasedJob->runAndGetReleasedJob();
117+
$this->assertDatabaseCount('failed_jobs', 0);
118+
$releasedJob = $releasedJob->runAndGetReleasedJob();
119+
$this->assertDatabaseCount('failed_jobs', 0);
120+
121+
$releasedJob->run();
122+
$this->assertDatabaseCount('failed_jobs', 1);
123+
}
124+
80125
#[Test]
81126
public function after_max_attempts_it_will_no_longer_execute_the_task()
82127
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /