Skip to content
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

[11.x] Introduce OnQueue and OnConnection attributes for jobs #54229

Open
wants to merge 6 commits into
base: 11.x
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/Illuminate/Foundation/Bus/Attributes/OnConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Illuminate\Foundation\Bus\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class OnConnection
{
/**
* @param string|\UnitEnum $connection
* @return void
*/
public function __construct(public $connection)
{
}
}
18 changes: 18 additions & 0 deletions src/Illuminate/Foundation/Bus/Attributes/OnQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Illuminate\Foundation\Bus\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class OnQueue
{
/**
* @param string|\UnitEnum $queue
* @return void
*/
public function __construct(public $queue)
{

}
}
35 changes: 35 additions & 0 deletions src/Illuminate/Foundation/Bus/PendingDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Foundation\Bus\Attributes\OnConnection;
use Illuminate\Foundation\Bus\Attributes\OnQueue;
use Illuminate\Foundation\Queue\InteractsWithUniqueJobs;

class PendingDispatch
Expand Down Expand Up @@ -36,6 +38,39 @@ class PendingDispatch
public function __construct($job)
{
$this->job = $job;

$this->setQueueAndConnectionFromAttributesIfNotSet();
}

/**
* Set the job's queue and connection values based on the job's OnQueue/OnConnection attributes.
*
* @return void
* @throws \ReflectionException
*/
protected function setQueueAndConnectionFromAttributesIfNotSet(): void
{
$hasQueueSet = isset($this->job->queue);
$hasConnectionSet = isset($this->job->connection);

if ($hasQueueSet && $hasConnectionSet) {
return;
}

$reflectionClass = new \ReflectionClass($this->job);
if (!$hasQueueSet) {
$onQueue = $reflectionClass->getAttributes(OnQueue::class);
if ($onQueue !== []) {
$this->onQueue($onQueue[0]->newInstance()->queue);
}
}

if (!$hasConnectionSet) {
$onConnection = $reflectionClass->getAttributes(OnConnection::class);
if ($onConnection !== []) {
$this->onConnection($onConnection[0]->newInstance()->connection);
}
}
}

/**
Expand Down
151 changes: 151 additions & 0 deletions tests/Integration/Bus/BusPendingDispatchWithAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace Illuminate\Tests\Integration\Bus;


use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Attributes\OnConnection;
use Illuminate\Foundation\Bus\Attributes\OnQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Queue;
use Orchestra\Testbench\TestCase;

class BusPendingDispatchWithAttributesTest extends TestCase
{
public function testDispatchWhereQueueAndConnectionAreFromAttributes(): void
{
Queue::fake();
FakeJobWithOnQueueAndOnConnection::dispatch(123);

Queue::assertPushed(function (FakeJobWithOnQueueAndOnConnection $job) {
return $job->connection === "connection_from_attribute"
&& $job->queue === "queue-from-attribute"
&& $job->value === 123;
});
}

public function testDispatchWhereQueueIsFromAttribute(): void
{
Queue::fake();

FakeJobWithOnQueueFromAttribute::dispatch(1234)->onConnection("not-from-attribute");

Queue::assertPushed(function (FakeJobWithOnQueueFromAttribute $job) {
return $job->connection === "not-from-attribute"
&& $job->queue === "queue-from-attribute"
&& $job->value === 1234;
});
}

public function testDispatchWhereConnectionIsFromAttribute(): void
{
Queue::fake();

FakeJobWithOnConnection::dispatch(999);

Queue::assertPushed(function (FakeJobWithOnConnection $job) {
return $job->connection === "connection_from_attribute"
&& !isset($job->queue)
&& $job->value === 999;
});
}

public function testOverridingQueueAndConnectionDoesNotUseAttributeValues(): void
{
Queue::fake();

FakeJobWithOnQueueAndOnConnection::dispatch('abc')
->onQueue('setViaMethod')
->onConnection('setViaMethodToo');

Queue::assertPushed(function (FakeJobWithOnQueueAndOnConnection $job) {
return $job->queue === "setViaMethod"
&& $job->connection === "setViaMethodToo"
&& $job->value === 'abc';
});
}

public function testAllowsEnumsInAttributes(): void
{
Queue::fake();

FakeJobWithAttributesUsingEnums::dispatch(1234);

Queue::assertPushed(
fn (FakeJobWithAttributesUsingEnums $job) => $job->queue === 'my-value'
&& $job->connection == 'other-value'
&& $job->value === 1234
);
}

public function testWorksWithDispatchFunction(): void
{
Queue::fake();

dispatch(new FakeJobWithAttributesUsingEnums('laravel'))->onConnection('zzz');

Queue::assertPushed(
fn (FakeJobWithAttributesUsingEnums $job) => $job->queue === 'my-value'
&& $job->connection == 'zzz'
&& $job->value === 'laravel'
);
}
}

#[OnQueue('queue-from-attribute')]
#[OnConnection('connection_from_attribute')]
class FakeJobWithOnQueueAndOnConnection implements ShouldQueue
{
use Dispatchable;
use Queueable;
use InteractsWithQueue;

public function __construct(public $value)
{
}
}

#[OnQueue('queue-from-attribute')]
class FakeJobWithOnQueueFromAttribute implements ShouldQueue
{
use Dispatchable;
use Queueable;
use InteractsWithQueue;

public function __construct(public $value)
{
}
}

#[OnConnection('connection_from_attribute')]
class FakeJobWithOnConnection implements ShouldQueue
{
use Dispatchable;
use Queueable;
use InteractsWithQueue;

public function __construct(public $value)
{
}
}

#[OnQueue(PendingDispatchWithAttributesEnum::MyValue)]
#[OnConnection(PendingDispatchWithAttributesEnum::OtherValue)]
class FakeJobWithAttributesUsingEnums implements ShouldQueue
{
use Dispatchable;
use Queueable;
use InteractsWithQueue;

public function __construct(public $value)
{
}
}

enum PendingDispatchWithAttributesEnum: string
{
case MyValue = 'my-value';
case OtherValue = 'other-value';
}