Skip to content
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

[11.x] Improve performance of Redis queue block_for when a worker has multiple queues to service #52826

Merged
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
23 changes: 21 additions & 2 deletions src/Illuminate/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ class RedisQueue extends Queue implements QueueContract, ClearableQueue
*/
protected $migrationBatchSize = -1;

/**
* Indicates if a secondary queue had a job available between checks of the primary queue.
*
* Only applicable when monitoring multiple named queues with a single instance.
*
* @var bool
*/
protected $secondaryQueueHadJob = false;

/**
* Create a new Redis queue instance.
*
Expand Down Expand Up @@ -221,13 +230,23 @@ protected function createPayloadArray($job, $queue, $data = '')
* @param string|null $queue
* @return \Illuminate\Contracts\Queue\Job|null
*/
public function pop($queue = null)
public function pop($queue = null, $index = 0)
{
$this->migrate($prefixed = $this->getQueue($queue));

[$job, $reserved] = $this->retrieveNextJob($prefixed);
$block = ! $this->secondaryQueueHadJob && $index == 0;

[$job, $reserved] = $this->retrieveNextJob($prefixed, $block);

if ($index == 0) {
$this->secondaryQueueHadJob = false;
}

if ($reserved) {
if ($index > 0) {
$this->secondaryQueueHadJob = true;
}

return new RedisJob(
$this->container, $this, $job,
$reserved, $this->connectionName, $queue ?: $this->default
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ public function runNextJob($connectionName, $queue, WorkerOptions $options)
*/
protected function getNextJob($connection, $queue)
{
$popJobCallback = function ($queue) use ($connection) {
return $connection->pop($queue);
$popJobCallback = function ($queue, $index = 0) use ($connection) {
return $connection->pop($queue, $index);
};

$this->raiseBeforeJobPopEvent($connection->getConnectionName());
Expand All @@ -360,8 +360,8 @@ protected function getNextJob($connection, $queue)
);
}

foreach (explode(',', $queue) as $queue) {
if (! is_null($job = $popJobCallback($queue))) {
foreach (explode(',', $queue) as $index => $queue) {
if (! is_null($job = $popJobCallback($queue, $index))) {
$this->raiseAfterJobPopEvent($connection->getConnectionName(), $job);

return $job;
Expand Down