Skip to content

Commit d6471a3

Browse files
Merge pull request #147 from Plytas/master
Explicitly specify job queue
2 parents 7dd1012 + d8c6357 commit d6471a3

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

src/CloudTasksQueue.php

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public function size($queue = null): int
9898
*/
9999
public function push($job, $data = '', $queue = null)
100100
{
101+
if (! ($job instanceof Closure)) {
102+
$job->queue = $queue ?? $job->queue ?? $this->config['queue'];
103+
}
104+
101105
return $this->enqueueUsing(
102106
$job,
103107
$this->createPayload($job, $queue, $data),

tests/QueueTest.php

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

77
use Google\Cloud\Tasks\V2\HttpMethod;
88
use Google\Cloud\Tasks\V2\Task;
9+
use Illuminate\Queue\CallQueuedClosure;
910
use Illuminate\Queue\Events\JobProcessed;
1011
use Illuminate\Queue\Events\JobProcessing;
1112
use Illuminate\Queue\Events\JobQueued;
@@ -146,17 +147,22 @@ public function it_posts_the_task_the_correct_queue()
146147
// Arrange
147148
CloudTasksApi::fake();
148149

150+
$closure = fn () => 'closure job';
151+
$closureDisplayName = CallQueuedClosure::create($closure)->displayName();
152+
149153
// Act
150154
$this->dispatch((new SimpleJob()));
151155
$this->dispatch((new FailingJob())->onQueue('my-special-queue'));
156+
$this->dispatch($closure);
157+
$this->dispatch($closure, 'my-special-queue');
152158

153159
// Assert
154160
CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool {
155161
$decoded = json_decode($task->getHttpRequest()->getBody(), true);
156162
$command = IncomingTask::fromJson($task->getHttpRequest()->getBody())->command();
157163

158164
return $decoded['displayName'] === SimpleJob::class
159-
&& ($command['queue'] ?? null) === null
165+
&& $command['queue'] === 'barbequeue'
160166
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue';
161167
});
162168

@@ -168,6 +174,24 @@ public function it_posts_the_task_the_correct_queue()
168174
&& $command['queue'] === 'my-special-queue'
169175
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue';
170176
});
177+
178+
CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName) use ($closureDisplayName): bool {
179+
$decoded = json_decode($task->getHttpRequest()->getBody(), true);
180+
$command = IncomingTask::fromJson($task->getHttpRequest()->getBody())->command();
181+
182+
return $decoded['displayName'] === $closureDisplayName
183+
&& $command['queue'] === 'barbequeue'
184+
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue';
185+
});
186+
187+
CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName) use ($closureDisplayName): bool {
188+
$decoded = json_decode($task->getHttpRequest()->getBody(), true);
189+
$command = IncomingTask::fromJson($task->getHttpRequest()->getBody())->command();
190+
191+
return $decoded['displayName'] === $closureDisplayName
192+
&& $command['queue'] === 'my-special-queue'
193+
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue';
194+
});
171195
}
172196

173197
#[Test]

tests/TaskHandlerTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ protected function setUp(): void
3333
CloudTasksApi::fake();
3434
}
3535

36-
#[Override]
37-
protected function tearDown(): void
38-
{
39-
parent::tearDown();
36+
#[Override]
37+
protected function tearDown(): void
38+
{
39+
parent::tearDown();
4040

41-
CloudTasksQueue::forgetWorkerOptionsCallback();
42-
}
41+
CloudTasksQueue::forgetWorkerOptionsCallback();
42+
}
4343

4444
#[Test]
4545
public function it_can_run_a_task()

tests/TestCase.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Tests;
66

77
use Google\Cloud\Tasks\V2\Client\CloudTasksClient;
8+
use Illuminate\Foundation\Bus\PendingClosureDispatch;
9+
use Illuminate\Foundation\Bus\PendingDispatch;
810
use Illuminate\Foundation\Testing\DatabaseTransactions;
911
use Illuminate\Support\Facades\Event;
1012
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksServiceProvider;
@@ -82,7 +84,7 @@ protected function setConfigValue($key, $value)
8284
$this->app['config']->set('queue.connections.my-cloudtasks-connection.'.$key, $value);
8385
}
8486

85-
public function dispatch($job): DispatchedJob
87+
public function dispatch($job, ?string $onQueue = null): DispatchedJob
8688
{
8789
$payload = null;
8890
$task = null;
@@ -93,7 +95,11 @@ public function dispatch($job): DispatchedJob
9395
$task = $event->task;
9496
});
9597

96-
dispatch($job);
98+
tap(dispatch($job), function (PendingClosureDispatch|PendingDispatch $pendingDispatch) use ($onQueue) {
99+
if ($onQueue !== null) {
100+
$pendingDispatch->onQueue($onQueue);
101+
}
102+
});
97103

98104
return new DispatchedJob($payload, $task, $this);
99105
}

0 commit comments

Comments
 (0)