Skip to content

Commit 864d2e5

Browse files
[10.x] Added JobPopping and JobPopped events (#46220)
* Added JobPopping and JobPopped events * pr-46220 styleci * formatting * Update Worker.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 3b7f6d0 commit 864d2e5

File tree

4 files changed

+114
-5
lines changed

4 files changed

+114
-5
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Events;
4+
5+
class JobPopped
6+
{
7+
/**
8+
* The connection name.
9+
*
10+
* @var string
11+
*/
12+
public $connectionName;
13+
14+
/**
15+
* The job instance.
16+
*
17+
* @var \Illuminate\Contracts\Queue\Job|null
18+
*/
19+
public $job;
20+
21+
/**
22+
* Create a new event instance.
23+
*
24+
* @param string $connectionName
25+
* @param \Illuminate\Contracts\Queue\Job|null $job
26+
* @return void
27+
*/
28+
public function __construct($connectionName, $job)
29+
{
30+
$this->connectionName = $connectionName;
31+
$this->job = $job;
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Events;
4+
5+
class JobPopping
6+
{
7+
/**
8+
* The connection name.
9+
*
10+
* @var string
11+
*/
12+
public $connectionName;
13+
14+
/**
15+
* Create a new event instance.
16+
*
17+
* @param string $connectionName
18+
* @return void
19+
*/
20+
public function __construct($connectionName)
21+
{
22+
$this->connectionName = $connectionName;
23+
}
24+
}

src/Illuminate/Queue/Worker.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Illuminate\Contracts\Queue\Factory as QueueManager;
99
use Illuminate\Database\DetectsLostConnections;
1010
use Illuminate\Queue\Events\JobExceptionOccurred;
11+
use Illuminate\Queue\Events\JobPopped;
12+
use Illuminate\Queue\Events\JobPopping;
1113
use Illuminate\Queue\Events\JobProcessed;
1214
use Illuminate\Queue\Events\JobProcessing;
1315
use Illuminate\Queue\Events\JobReleasedAfterException;
@@ -342,13 +344,20 @@ protected function getNextJob($connection, $queue)
342344
return $connection->pop($queue);
343345
};
344346

347+
$this->raiseBeforeJobPopEvent($connection->getConnectionName());
348+
345349
try {
346350
if (isset(static::$popCallbacks[$this->name])) {
347-
return (static::$popCallbacks[$this->name])($popJobCallback, $queue);
351+
return tap(
352+
(static::$popCallbacks[$this->name])($popJobCallback, $queue),
353+
fn ($job) => $this->raiseAfterJobPopEvent($connection->getConnectionName(), $job)
354+
);
348355
}
349356

350357
foreach (explode(',', $queue) as $queue) {
351358
if (! is_null($job = $popJobCallback($queue))) {
359+
$this->raiseAfterJobPopEvent($connection->getConnectionName(), $job);
360+
352361
return $job;
353362
}
354363
}
@@ -601,6 +610,31 @@ protected function calculateBackoff($job, WorkerOptions $options)
601610
return (int) ($backoff[$job->attempts() - 1] ?? last($backoff));
602611
}
603612

613+
/**
614+
* Raise the before job has been popped.
615+
*
616+
* @param string $connectionName
617+
* @return void
618+
*/
619+
protected function raiseBeforeJobPopEvent($connectionName)
620+
{
621+
$this->events->dispatch(new JobPopping($connectionName));
622+
}
623+
624+
/**
625+
* Raise the after job has been popped.
626+
*
627+
* @param string $connectionName
628+
* @param \Illuminate\Contracts\Queue\Job|null $job
629+
* @return void
630+
*/
631+
protected function raiseAfterJobPopEvent($connectionName, $job)
632+
{
633+
$this->events->dispatch(new JobPopped(
634+
$connectionName, $job
635+
));
636+
}
637+
604638
/**
605639
* Raise the before queue job event.
606640
*

tests/Queue/QueueWorkerTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Illuminate\Contracts\Events\Dispatcher;
99
use Illuminate\Contracts\Queue\Job as QueueJobContract;
1010
use Illuminate\Queue\Events\JobExceptionOccurred;
11+
use Illuminate\Queue\Events\JobPopped;
12+
use Illuminate\Queue\Events\JobPopping;
1113
use Illuminate\Queue\Events\JobProcessed;
1214
use Illuminate\Queue\Events\JobProcessing;
1315
use Illuminate\Queue\MaxAttemptsExceededException;
@@ -50,6 +52,8 @@ public function testJobCanBeFired()
5052
$worker = $this->getWorker('default', ['queue' => [$job = new WorkerFakeJob]]);
5153
$worker->runNextJob('default', 'queue', new WorkerOptions);
5254
$this->assertTrue($job->fired);
55+
$this->events->shouldHaveReceived('dispatch')->with(m::type(JobPopping::class))->once();
56+
$this->events->shouldHaveReceived('dispatch')->with(m::type(JobPopped::class))->once();
5357
$this->events->shouldHaveReceived('dispatch')->with(m::type(JobProcessing::class))->once();
5458
$this->events->shouldHaveReceived('dispatch')->with(m::type(JobProcessed::class))->once();
5559
}
@@ -118,7 +122,7 @@ public function testJobCanBeFiredBasedOnPriority()
118122
public function testExceptionIsReportedIfConnectionThrowsExceptionOnJobPop()
119123
{
120124
$worker = new InsomniacWorker(
121-
new WorkerFakeManager('default', new BrokenQueueConnection($e = new RuntimeException)),
125+
new WorkerFakeManager('default', new BrokenQueueConnection('default', $e = new RuntimeException)),
122126
$this->events,
123127
$this->exceptionHandler,
124128
function () {
@@ -377,7 +381,7 @@ private function getWorker($connectionName = 'default', $jobs = [], ?callable $i
377381
private function workerDependencies($connectionName = 'default', $jobs = [], ?callable $isInMaintenanceMode = null)
378382
{
379383
return [
380-
new WorkerFakeManager($connectionName, new WorkerFakeConnection($jobs)),
384+
new WorkerFakeManager($connectionName, new WorkerFakeConnection($connectionName, $jobs)),
381385
$this->events,
382386
$this->exceptionHandler,
383387
$isInMaintenanceMode ?? function () {
@@ -444,32 +448,46 @@ public function connection($name = null)
444448

445449
class WorkerFakeConnection
446450
{
451+
public $connectionName;
447452
public $jobs = [];
448453

449-
public function __construct($jobs)
454+
public function __construct($connectionName, $jobs)
450455
{
456+
$this->connectionName = $connectionName;
451457
$this->jobs = $jobs;
452458
}
453459

454460
public function pop($queue)
455461
{
456462
return array_shift($this->jobs[$queue]);
457463
}
464+
465+
public function getConnectionName()
466+
{
467+
return $this->connectionName;
468+
}
458469
}
459470

460471
class BrokenQueueConnection
461472
{
473+
public $connectionName;
462474
public $exception;
463475

464-
public function __construct($exception)
476+
public function __construct($connectionName, $exception)
465477
{
478+
$this->connectionName = $connectionName;
466479
$this->exception = $exception;
467480
}
468481

469482
public function pop($queue)
470483
{
471484
throw $this->exception;
472485
}
486+
487+
public function getConnectionName()
488+
{
489+
return $this->connectionName;
490+
}
473491
}
474492

475493
class WorkerFakeJob implements QueueJobContract

0 commit comments

Comments
 (0)