Skip to content

Explicitly specify job queue #147

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 1 commit into from
Jun 24, 2024
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
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Display name of a closure job looks like Closure (QueueTest.php:150). Added this line to keep the ability to assert displayName in CloudTasksApi::assertTaskCreated().


// 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'
Comment on lines -159 to +165
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This failed initially after the changes, because $command['queue'] is now always set.

&& $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();
}
Comment on lines -36 to +42
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran composer pint, thus this change.


#[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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an optional parameter that allows to set queue for a closure job.

{
$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
Loading