Skip to content

Commit 5a7f2b4

Browse files
authored
Allow Listeners to dynamically specify delay with withDelay (#48026)
1 parent d522b86 commit 5a7f2b4

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

src/Illuminate/Events/Dispatcher.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -579,16 +579,20 @@ protected function queueHandler($class, $method, $arguments)
579579
[$listener, $job] = $this->createListenerAndJob($class, $method, $arguments);
580580

581581
$connection = $this->resolveQueue()->connection(method_exists($listener, 'viaConnection')
582-
? (isset($arguments[0]) ? $listener->viaConnection($arguments[0]) : $listener->viaConnection())
583-
: $listener->connection ?? null);
582+
? (isset($arguments[0]) ? $listener->viaConnection($arguments[0]) : $listener->viaConnection())
583+
: $listener->connection ?? null);
584584

585585
$queue = method_exists($listener, 'viaQueue')
586-
? (isset($arguments[0]) ? $listener->viaQueue($arguments[0]) : $listener->viaQueue())
587-
: $listener->queue ?? null;
586+
? (isset($arguments[0]) ? $listener->viaQueue($arguments[0]) : $listener->viaQueue())
587+
: $listener->queue ?? null;
588588

589-
isset($listener->delay)
590-
? $connection->laterOn($queue, $listener->delay, $job)
591-
: $connection->pushOn($queue, $job);
589+
$delay = method_exists($listener, 'withDelay')
590+
? (isset($arguments[0]) ? $listener->withDelay($arguments[0]) : $listener->withDelay())
591+
: $listener->delay ?? null;
592+
593+
is_null($delay)
594+
? $connection->pushOn($queue, $job)
595+
: $connection->laterOn($queue, $delay, $job);
592596
}
593597

594598
/**

tests/Events/QueuedEventsTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ public function testQueueIsSetByGetConnection()
8585
$d->dispatch('some.event', ['foo', 'bar']);
8686
}
8787

88+
public function testDelayIsSetByWithDelay()
89+
{
90+
$d = new Dispatcher;
91+
$queue = m::mock(Queue::class);
92+
93+
$queue->shouldReceive('connection')->once()->with(null)->andReturnSelf();
94+
95+
$queue->shouldReceive('laterOn')->once()->with(null, 20, m::type(CallQueuedListener::class));
96+
97+
$d->setQueueResolver(function () use ($queue) {
98+
return $queue;
99+
});
100+
101+
$d->listen('some.event', TestDispatcherGetDelay::class.'@handle');
102+
$d->dispatch('some.event', ['foo', 'bar']);
103+
}
104+
88105
public function testQueueIsSetByGetQueueDynamically()
89106
{
90107
$d = new Dispatcher;
@@ -127,6 +144,23 @@ public function testQueueIsSetByGetConnectionDynamically()
127144
]);
128145
}
129146

147+
public function testDelayIsSetByWithDelayDynamically()
148+
{
149+
$d = new Dispatcher;
150+
$queue = m::mock(Queue::class);
151+
152+
$queue->shouldReceive('connection')->once()->with(null)->andReturnSelf();
153+
154+
$queue->shouldReceive('laterOn')->once()->with(null, 60, m::type(CallQueuedListener::class));
155+
156+
$d->setQueueResolver(function () use ($queue) {
157+
return $queue;
158+
});
159+
160+
$d->listen('some.event', TestDispatcherGetDelayDynamically::class.'@handle');
161+
$d->dispatch('some.event', [['useHighDelay' => true], 'bar']);
162+
}
163+
130164
public function testQueuePropagateRetryUntilAndMaxExceptions()
131165
{
132166
$d = new Dispatcher;
@@ -219,6 +253,21 @@ public function viaConnection()
219253
}
220254
}
221255

256+
class TestDispatcherGetDelay implements ShouldQueue
257+
{
258+
public $delay = 10;
259+
260+
public function handle()
261+
{
262+
//
263+
}
264+
265+
public function withDelay()
266+
{
267+
return 20;
268+
}
269+
}
270+
222271
class TestDispatcherOptions implements ShouldQueue
223272
{
224273
public $maxExceptions = 1;
@@ -299,3 +348,22 @@ public function viaQueue($event)
299348
return 'p99';
300349
}
301350
}
351+
352+
class TestDispatcherGetDelayDynamically implements ShouldQueue
353+
{
354+
public $delay = 10;
355+
356+
public function handle()
357+
{
358+
//
359+
}
360+
361+
public function withDelay($event)
362+
{
363+
if ($event['useHighDelay']) {
364+
return 60;
365+
}
366+
367+
return 20;
368+
}
369+
}

0 commit comments

Comments
 (0)