Skip to content

Commit 4bed165

Browse files
[12.x] feat: Add WorkerStarting event when worker daemon starts (#55941)
* Set up and apply worker starting event as the daemon starts Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> * Add starting to the Monitor and QueueManager Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> * Add a test Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> * Remove unused status variable Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> * Make sure the worker stops Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> * Remove starting from Monitor interface Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> * formatting --------- Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent f97e6d9 commit 4bed165

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Events;
4+
5+
class WorkerStarting
6+
{
7+
/**
8+
* Create a new event instance.
9+
*
10+
* @param string $connectionName
11+
* @param string $queue
12+
* @param \Illuminate\Queue\WorkerOptions $options
13+
*/
14+
public function __construct(
15+
public $connectionName,
16+
public $queue,
17+
public $workerOptions
18+
) {
19+
}
20+
}

src/Illuminate/Queue/QueueManager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ public function failing($callback)
9898
$this->app['events']->listen(Events\JobFailed::class, $callback);
9999
}
100100

101+
/**
102+
* Register an event listener for the daemon queue starting.
103+
*
104+
* @param mixed $callback
105+
* @return void
106+
*/
107+
public function starting($callback)
108+
{
109+
$this->app['events']->listen(Events\WorkerStarting::class, $callback);
110+
}
111+
101112
/**
102113
* Register an event listener for the daemon queue stopping.
103114
*

src/Illuminate/Queue/Worker.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Queue\Events\JobReleasedAfterException;
1717
use Illuminate\Queue\Events\JobTimedOut;
1818
use Illuminate\Queue\Events\Looping;
19+
use Illuminate\Queue\Events\WorkerStarting;
1920
use Illuminate\Queue\Events\WorkerStopping;
2021
use Illuminate\Support\Carbon;
2122
use Throwable;
@@ -139,6 +140,8 @@ public function daemon($connectionName, $queue, WorkerOptions $options)
139140

140141
[$startTime, $jobsProcessed] = [hrtime(true) / 1e9, 0];
141142

143+
$this->raiseWorkerStartingEvent($connectionName, $queue, $options);
144+
142145
while (true) {
143146
// Before reserving any jobs, we will make sure this queue is not paused and
144147
// if it is we will just pause this worker for a given amount of time and
@@ -624,7 +627,20 @@ protected function calculateBackoff($job, WorkerOptions $options)
624627
}
625628

626629
/**
627-
* Raise the before job has been popped.
630+
* Raise an event indicating the worker is starting.
631+
*
632+
* @param string $connectionName
633+
* @param string $queue
634+
* @param \Illuminate\Queue\WorkerOptions $options
635+
* @return void
636+
*/
637+
protected function raiseWorkerStartingEvent($connectionName, $queue, $options)
638+
{
639+
$this->events->dispatch(new WorkerStarting($connectionName, $queue, $options));
640+
}
641+
642+
/**
643+
* Raise an event indicating a job is being popped from the queue.
628644
*
629645
* @param string $connectionName
630646
* @return void
@@ -635,7 +651,7 @@ protected function raiseBeforeJobPopEvent($connectionName)
635651
}
636652

637653
/**
638-
* Raise the after job has been popped.
654+
* Raise an event indicating a job has been popped from the queue.
639655
*
640656
* @param string $connectionName
641657
* @param \Illuminate\Contracts\Queue\Job|null $job
@@ -649,7 +665,7 @@ protected function raiseAfterJobPopEvent($connectionName, $job)
649665
}
650666

651667
/**
652-
* Raise the before queue job event.
668+
* Raise an event indicating a job is being processed.
653669
*
654670
* @param string $connectionName
655671
* @param \Illuminate\Contracts\Queue\Job $job
@@ -663,7 +679,7 @@ protected function raiseBeforeJobEvent($connectionName, $job)
663679
}
664680

665681
/**
666-
* Raise the after queue job event.
682+
* Raise an event indicating a job has been processed.
667683
*
668684
* @param string $connectionName
669685
* @param \Illuminate\Contracts\Queue\Job $job

tests/Queue/QueueWorkerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Queue\Events\JobPopping;
1313
use Illuminate\Queue\Events\JobProcessed;
1414
use Illuminate\Queue\Events\JobProcessing;
15+
use Illuminate\Queue\Events\WorkerStarting;
1516
use Illuminate\Queue\MaxAttemptsExceededException;
1617
use Illuminate\Queue\QueueManager;
1718
use Illuminate\Queue\Worker;
@@ -386,6 +387,24 @@ public function testWorkerPicksJobUsingCustomCallbacks()
386387
Worker::popUsing('myworker', null);
387388
}
388389

390+
public function testWorkerStartingIsDispatched()
391+
{
392+
$workerOptions = new WorkerOptions();
393+
$workerOptions->stopWhenEmpty = true;
394+
395+
$worker = $this->getWorker('default', ['queue' => [
396+
$firstJob = new WorkerFakeJob(),
397+
$secondJob = new WorkerFakeJob(),
398+
]]);
399+
400+
$worker->daemon('default', 'queue', $workerOptions);
401+
402+
$this->assertTrue($firstJob->fired);
403+
$this->assertTrue($secondJob->fired);
404+
405+
$this->events->shouldHaveReceived('dispatch')->with(m::type(WorkerStarting::class))->once();
406+
}
407+
389408
/**
390409
* Helpers...
391410
*/

0 commit comments

Comments
 (0)