Skip to content

Commit

Permalink
Explicitly specify job queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Plytas committed May 30, 2024
1 parent 7dd1012 commit d8c6357
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/CloudTasksQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public function size($queue = null): int
*/
public function push($job, $data = '', $queue = null)
{
if (! ($job instanceof Closure)) {
$job->queue = $queue ?? $job->queue ?? $this->config['queue'];
}

return $this->enqueueUsing(
$job,
$this->createPayload($job, $queue, $data),
Expand Down
26 changes: 25 additions & 1 deletion tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Google\Cloud\Tasks\V2\HttpMethod;
use Google\Cloud\Tasks\V2\Task;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\JobQueued;
Expand Down Expand Up @@ -146,17 +147,22 @@ public function it_posts_the_task_the_correct_queue()
// Arrange
CloudTasksApi::fake();

$closure = fn () => 'closure job';
$closureDisplayName = CallQueuedClosure::create($closure)->displayName();

// Act
$this->dispatch((new SimpleJob()));
$this->dispatch((new FailingJob())->onQueue('my-special-queue'));
$this->dispatch($closure);
$this->dispatch($closure, 'my-special-queue');

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

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

Expand All @@ -168,6 +174,24 @@ public function it_posts_the_task_the_correct_queue()
&& $command['queue'] === 'my-special-queue'
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue';
});

CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName) use ($closureDisplayName): bool {
$decoded = json_decode($task->getHttpRequest()->getBody(), true);
$command = IncomingTask::fromJson($task->getHttpRequest()->getBody())->command();

return $decoded['displayName'] === $closureDisplayName
&& $command['queue'] === 'barbequeue'
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue';
});

CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName) use ($closureDisplayName): bool {
$decoded = json_decode($task->getHttpRequest()->getBody(), true);
$command = IncomingTask::fromJson($task->getHttpRequest()->getBody())->command();

return $decoded['displayName'] === $closureDisplayName
&& $command['queue'] === 'my-special-queue'
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue';
});
}

#[Test]
Expand Down
12 changes: 6 additions & 6 deletions tests/TaskHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ protected function setUp(): void
CloudTasksApi::fake();
}

#[Override]
protected function tearDown(): void
{
parent::tearDown();
#[Override]
protected function tearDown(): void
{
parent::tearDown();

CloudTasksQueue::forgetWorkerOptionsCallback();
}
CloudTasksQueue::forgetWorkerOptionsCallback();
}

#[Test]
public function it_can_run_a_task()
Expand Down
10 changes: 8 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Tests;

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

public function dispatch($job): DispatchedJob
public function dispatch($job, ?string $onQueue = null): DispatchedJob
{
$payload = null;
$task = null;
Expand All @@ -93,7 +95,11 @@ public function dispatch($job): DispatchedJob
$task = $event->task;
});

dispatch($job);
tap(dispatch($job), function (PendingClosureDispatch|PendingDispatch $pendingDispatch) use ($onQueue) {
if ($onQueue !== null) {
$pendingDispatch->onQueue($onQueue);
}
});

return new DispatchedJob($payload, $task, $this);
}
Expand Down

0 comments on commit d8c6357

Please sign in to comment.