Skip to content

Commit 815c3ab

Browse files
committed
default queue by class
1 parent 6fe51dd commit 815c3ab

File tree

16 files changed

+396
-12
lines changed

16 files changed

+396
-12
lines changed

src/Illuminate/Bus/Dispatcher.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
use Illuminate\Contracts\Queue\ShouldQueue;
1010
use Illuminate\Foundation\Bus\PendingChain;
1111
use Illuminate\Pipeline\Pipeline;
12+
use Illuminate\Queue\Concerns\HasDefaultQueues;
1213
use Illuminate\Queue\InteractsWithQueue;
1314
use Illuminate\Queue\Jobs\SyncJob;
1415
use Illuminate\Support\Collection;
1516
use RuntimeException;
1617

1718
class Dispatcher implements QueueingDispatcher
1819
{
20+
use HasDefaultQueues;
21+
1922
/**
2023
* The container implementation.
2124
*
@@ -239,11 +242,13 @@ public function dispatchToQueue($command)
239242
*/
240243
protected function pushCommandToQueue($queue, $command)
241244
{
245+
$queueName = $command->queue ?? $this->getDefaultQueue($command);
246+
242247
if (isset($command->delay)) {
243-
return $queue->later($command->delay, $command, queue: $command->queue ?? null);
248+
return $queue->later($command->delay, $command, queue: $queueName);
244249
}
245250

246-
return $queue->push($command, queue: $command->queue ?? null);
251+
return $queue->push($command, queue: $queueName);
247252
}
248253

249254
/**
@@ -313,4 +318,9 @@ public function withoutDispatchingAfterResponses()
313318

314319
return $this;
315320
}
321+
322+
protected function getQueueDefaults()
323+
{
324+
return $this->container['queue.defaults'];
325+
}
316326
}

src/Illuminate/Foundation/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,7 @@ public function registerCoreContainerAliases()
16621662
'queue' => [\Illuminate\Queue\QueueManager::class, \Illuminate\Contracts\Queue\Factory::class, \Illuminate\Contracts\Queue\Monitor::class],
16631663
'queue.connection' => [\Illuminate\Contracts\Queue\Queue::class],
16641664
'queue.failer' => [\Illuminate\Queue\Failed\FailedJobProviderInterface::class],
1665+
'queue.defaults' => [\Illuminate\Queue\QueueDefaults::class],
16651666
'redirect' => [\Illuminate\Routing\Redirector::class],
16661667
'redis' => [\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class],
16671668
'redis.connection' => [\Illuminate\Redis\Connections\Connection::class, \Illuminate\Contracts\Redis\Connection::class],

src/Illuminate/Mail/Mailable.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ public function queue(Queue $queue)
230230

231231
$connection = property_exists($this, 'connection') ? $this->connection : null;
232232

233-
$queueName = property_exists($this, 'queue') ? $this->queue : null;
233+
$queueName = property_exists($this, 'queue')
234+
? $this->queue
235+
: $queue->getDefaultQueue($this);
234236

235237
return $queue->connection($connection)->pushOn(
236238
$queueName ?: null, $this->newQueuedJob()
@@ -248,7 +250,9 @@ public function later($delay, Queue $queue)
248250
{
249251
$connection = property_exists($this, 'connection') ? $this->connection : null;
250252

251-
$queueName = property_exists($this, 'queue') ? $this->queue : null;
253+
$queueName = property_exists($this, 'queue')
254+
? $this->queue
255+
: $queue->getDefaultQueue($this);
252256

253257
return $queue->connection($connection)->laterOn(
254258
$queueName ?: null, $delay, $this->newQueuedJob()

src/Illuminate/Notifications/ChannelManager.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
use Illuminate\Contracts\Events\Dispatcher;
77
use Illuminate\Contracts\Notifications\Dispatcher as DispatcherContract;
88
use Illuminate\Contracts\Notifications\Factory as FactoryContract;
9+
use Illuminate\Queue\Concerns\HasDefaultQueues;
910
use Illuminate\Support\Manager;
1011
use InvalidArgumentException;
1112

1213
class ChannelManager extends Manager implements DispatcherContract, FactoryContract
1314
{
15+
use HasDefaultQueues;
16+
1417
/**
1518
* The default channel used to deliver messages.
1619
*
@@ -159,4 +162,14 @@ public function locale($locale)
159162

160163
return $this;
161164
}
165+
166+
/**
167+
* Get the queue defaults instance.
168+
*
169+
* @return \Illuminate\Queue\QueueDefaults
170+
*/
171+
protected function getQueueDefaults()
172+
{
173+
return $this->container['queue.defaults'];
174+
}
162175
}

src/Illuminate/Notifications/NotificationSender.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ protected function queueNotification($notifiables, $notification)
235235
$connection = $notification->viaConnections()[$channel] ?? $connection;
236236
}
237237

238-
$queue = $notification->queue;
238+
$queue = $notification->queue ?? $this->manager->getDefaultQueue($notification);
239239

240240
if (method_exists($notification, 'viaQueues')) {
241241
$queue = $notification->viaQueues()[$channel] ?? $queue;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Concerns;
4+
5+
trait HasDefaultQueues
6+
{
7+
/**
8+
* Set multiple default queues at once.
9+
*
10+
* @param class-string $class
11+
* @param string $queue
12+
* @return $this
13+
*/
14+
public function defaultQueue($class, $queue)
15+
{
16+
$this->getQueueDefaults()->set($class, $queue);
17+
18+
return $this;
19+
}
20+
21+
/**
22+
* Set the default queues for the given classes.
23+
*
24+
* @param array<class-string, string> $queues
25+
* @return $this
26+
*/
27+
public function defaultQueues($queues)
28+
{
29+
$this->getQueueDefaults()->setMany($queues);
30+
31+
return $this;
32+
}
33+
34+
/**
35+
* Get the default queue for a given queueable instance.
36+
*
37+
* @param object $queueable
38+
* @return string|null
39+
*/
40+
public function getDefaultQueue($queueable)
41+
{
42+
return $this->getQueueDefaults()->get($queueable);
43+
}
44+
45+
/**
46+
* Get the queue defaults instance.
47+
*
48+
* @return \Illuminate\Queue\QueueDefaults
49+
*/
50+
abstract protected function getQueueDefaults();
51+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Illuminate\Queue;
4+
5+
class QueueDefaults
6+
{
7+
/**
8+
* The mapping of class names to their default queues.
9+
*
10+
* @var array
11+
*/
12+
protected $defaults = [];
13+
14+
/**
15+
* Set the default queues for the given classes.
16+
*
17+
* @param class-string $class
18+
* @param string $queue
19+
* @return $this
20+
*/
21+
public function set($class, $queue)
22+
{
23+
$this->defaults[$class] = $queue;
24+
25+
return $this;
26+
}
27+
28+
/**
29+
* Set multiple default queues at once.
30+
*
31+
* @param array<class-string, string> $defaults
32+
* @return $this
33+
*/
34+
public function setMany(array $defaults)
35+
{
36+
$this->defaults = array_merge($this->defaults, $defaults);
37+
38+
return $this;
39+
}
40+
41+
/**
42+
* Get the default queue for a given queueable instance.
43+
*
44+
* @param object $queueable
45+
* @return string|null
46+
*/
47+
public function get($queueable)
48+
{
49+
if (empty($this->defaults)) {
50+
return null;
51+
}
52+
53+
$classes = array_merge(
54+
[get_class($queueable)],
55+
class_parents($queueable) ?: [],
56+
class_implements($queueable) ?: [],
57+
class_uses_recursive($queueable)
58+
);
59+
60+
foreach ($classes as $class) {
61+
if (isset($this->defaults[$class])) {
62+
return $this->defaults[$class];
63+
}
64+
}
65+
66+
return null;
67+
}
68+
69+
/**
70+
* Get all registered default queues.
71+
*
72+
* @return array
73+
*/
74+
public function all()
75+
{
76+
return $this->defaults;
77+
}
78+
}

src/Illuminate/Queue/QueueManager.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
use Closure;
66
use Illuminate\Contracts\Queue\Factory as FactoryContract;
77
use Illuminate\Contracts\Queue\Monitor as MonitorContract;
8+
use Illuminate\Queue\Concerns\HasDefaultQueues;
89
use InvalidArgumentException;
910

1011
/**
1112
* @mixin \Illuminate\Contracts\Queue\Queue
1213
*/
1314
class QueueManager implements FactoryContract, MonitorContract
1415
{
16+
use HasDefaultQueues;
17+
1518
/**
1619
* The application instance.
1720
*
@@ -120,6 +123,16 @@ public function stopping($callback)
120123
$this->app['events']->listen(Events\WorkerStopping::class, $callback);
121124
}
122125

126+
/**
127+
* Get the queue defaults instance.
128+
*
129+
* @return \Illuminate\Queue\QueueDefaults
130+
*/
131+
protected function getQueueDefaults()
132+
{
133+
return $this->app['queue.defaults'];
134+
}
135+
123136
/**
124137
* Determine if the driver is connected.
125138
*

src/Illuminate/Queue/QueueServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function register()
4141
$this->registerManager();
4242
$this->registerConnection();
4343
$this->registerWorker();
44+
$this->registerDefaults();
4445
$this->registerListener();
4546
$this->registerFailedJobServices();
4647
}
@@ -111,6 +112,18 @@ public function registerConnectors($manager)
111112
}
112113
}
113114

115+
/**
116+
* Register the default queues.
117+
*
118+
* @return void
119+
*/
120+
protected function registerDefaults()
121+
{
122+
$this->app->singleton('queue.defaults', function () {
123+
return new QueueDefaults;
124+
});
125+
}
126+
114127
/**
115128
* Register the Null queue connector.
116129
*
@@ -386,6 +399,7 @@ public function provides()
386399
return [
387400
'queue',
388401
'queue.connection',
402+
'queue.defaults',
389403
'queue.failer',
390404
'queue.listener',
391405
'queue.worker',

tests/Bus/BusDispatcherTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ protected function tearDown(): void
2323
public function testCommandsThatShouldQueueIsQueued()
2424
{
2525
$container = new Container;
26+
$container->instance('queue.defaults', $queueDefaults = m::mock());
27+
$queueDefaults->shouldReceive('get')->andReturn(null);
28+
Container::setInstance($container);
2629
$dispatcher = new Dispatcher($container, function () {
2730
$mock = m::mock(Queue::class);
2831
$mock->shouldReceive('push')->once();
@@ -36,6 +39,9 @@ public function testCommandsThatShouldQueueIsQueued()
3639
public function testCommandsThatShouldQueueIsQueuedUsingCustomHandler()
3740
{
3841
$container = new Container;
42+
$container->instance('queue.defaults', $queueDefaults = m::mock());
43+
$queueDefaults->shouldReceive('get')->andReturn(null);
44+
Container::setInstance($container);
3945
$dispatcher = new Dispatcher($container, function () {
4046
$mock = m::mock(Queue::class);
4147
$mock->shouldReceive('push')->once();
@@ -49,6 +55,9 @@ public function testCommandsThatShouldQueueIsQueuedUsingCustomHandler()
4955
public function testCommandsThatShouldQueueIsQueuedUsingCustomQueueAndDelay()
5056
{
5157
$container = new Container;
58+
$container->instance('queue.defaults', $queueDefaults = m::mock());
59+
$queueDefaults->shouldReceive('get')->andReturn(null);
60+
Container::setInstance($container);
5261
$dispatcher = new Dispatcher($container, function () {
5362
$mock = m::mock(Queue::class);
5463
$mock->shouldReceive('later')->once()->with(10, m::type(BusDispatcherTestSpecificQueueAndDelayCommand::class), '', 'foo');
@@ -59,6 +68,22 @@ public function testCommandsThatShouldQueueIsQueuedUsingCustomQueueAndDelay()
5968
$dispatcher->dispatch(new BusDispatcherTestSpecificQueueAndDelayCommand);
6069
}
6170

71+
public function testCommandsAreDispatchedWithDefaultQueue()
72+
{
73+
$container = new Container;
74+
$container->instance('queue.defaults', $queueDefaults = m::mock());
75+
$queueDefaults->shouldReceive('get')->andReturn('high-priority');
76+
77+
$mock = m::mock(Queue::class);
78+
$mock->shouldReceive('push')->once()->with(BusDispatcherQueueable::class, '', 'high-priority');
79+
80+
$dispatcher = new Dispatcher($container, function () use ($mock) {
81+
return $mock;
82+
});
83+
84+
$dispatcher->dispatch(new BusDispatcherQueueable);
85+
}
86+
6287
public function testDispatchNowShouldNeverQueue()
6388
{
6489
$container = new Container;
@@ -99,6 +124,9 @@ public function testOnConnectionOnJobWhenDispatching()
99124
],
100125
]);
101126
});
127+
$container->instance('queue.defaults', $queueDefaults = m::mock());
128+
$queueDefaults->shouldReceive('get')->andReturn(null);
129+
Container::setInstance($container);
102130

103131
$dispatcher = new Dispatcher($container, function () {
104132
$mock = m::mock(Queue::class);
@@ -147,6 +175,11 @@ class BusDispatcherTestSpecificQueueAndDelayCommand implements ShouldQueue
147175
public $delay = 10;
148176
}
149177

178+
class BusDispatcherQueueable implements ShouldQueue
179+
{
180+
use Queueable;
181+
}
182+
150183
class StandAloneCommand
151184
{
152185
//

0 commit comments

Comments
 (0)