Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[12.x] feat: Add WorkerStarting event when worker daemon starts #55941

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

Merged
merged 7 commits into from
Jun 6, 2025
Merged
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
20 changes: 20 additions & 0 deletions 20 src/Illuminate/Queue/Events/WorkerStarting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Illuminate\Queue\Events;

class WorkerStarting
{
/**
* Create a new event instance.
*
* @param string $connectionName
* @param string $queue
* @param \Illuminate\Queue\WorkerOptions $options
*/
public function __construct(
public $connectionName,
public $queue,
public $workerOptions
) {
}
}
11 changes: 11 additions & 0 deletions 11 src/Illuminate/Queue/QueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ public function failing($callback)
$this->app['events']->listen(Events\JobFailed::class, $callback);
}

/**
* Register an event listener for the daemon queue starting.
*
* @param mixed $callback
* @return void
*/
public function starting($callback)
{
$this->app['events']->listen(Events\WorkerStarting::class, $callback);
}

/**
* Register an event listener for the daemon queue stopping.
*
Expand Down
24 changes: 20 additions & 4 deletions 24 src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Queue\Events\JobReleasedAfterException;
use Illuminate\Queue\Events\JobTimedOut;
use Illuminate\Queue\Events\Looping;
use Illuminate\Queue\Events\WorkerStarting;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Support\Carbon;
use Throwable;
Expand Down Expand Up @@ -139,6 +140,8 @@ public function daemon($connectionName, $queue, WorkerOptions $options)

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

$this->raiseWorkerStartingEvent($connectionName, $queue, $options);

while (true) {
// Before reserving any jobs, we will make sure this queue is not paused and
// if it is we will just pause this worker for a given amount of time and
Expand Down Expand Up @@ -624,7 +627,20 @@ protected function calculateBackoff($job, WorkerOptions $options)
}

/**
* Raise the before job has been popped.
* Raise an event indicating the worker is starting.
*
* @param string $connectionName
* @param string $queue
* @param \Illuminate\Queue\WorkerOptions $options
* @return void
*/
protected function raiseWorkerStartingEvent($connectionName, $queue, $options)
{
$this->events->dispatch(new WorkerStarting($connectionName, $queue, $options));
}

/**
* Raise an event indicating a job is being popped from the queue.
*
* @param string $connectionName
* @return void
Expand All @@ -635,7 +651,7 @@ protected function raiseBeforeJobPopEvent($connectionName)
}

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

/**
* Raise the before queue job event.
* Raise an event indicating a job is being processed.
*
* @param string $connectionName
* @param \Illuminate\Contracts\Queue\Job $job
Expand All @@ -663,7 +679,7 @@ protected function raiseBeforeJobEvent($connectionName, $job)
}

/**
* Raise the after queue job event.
* Raise an event indicating a job has been processed.
*
* @param string $connectionName
* @param \Illuminate\Contracts\Queue\Job $job
Expand Down
19 changes: 19 additions & 0 deletions 19 tests/Queue/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Queue\Events\JobPopping;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\WorkerStarting;
use Illuminate\Queue\MaxAttemptsExceededException;
use Illuminate\Queue\QueueManager;
use Illuminate\Queue\Worker;
Expand Down Expand Up @@ -386,6 +387,24 @@ public function testWorkerPicksJobUsingCustomCallbacks()
Worker::popUsing('myworker', null);
}

public function testWorkerStartingIsDispatched()
{
$workerOptions = new WorkerOptions();
$workerOptions->stopWhenEmpty = true;

$worker = $this->getWorker('default', ['queue' => [
$firstJob = new WorkerFakeJob(),
$secondJob = new WorkerFakeJob(),
]]);

$worker->daemon('default', 'queue', $workerOptions);

$this->assertTrue($firstJob->fired);
$this->assertTrue($secondJob->fired);

$this->events->shouldHaveReceived('dispatch')->with(m::type(WorkerStarting::class))->once();
}

/**
* Helpers...
*/
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.