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

[8.x]: Prevent redis crash when large number of jobs are scheduled fo… #43488

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/Illuminate/Queue/Connectors/RedisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function connect(array $config)
$config['connection'] ?? $this->connection,
$config['retry_after'] ?? 60,
$config['block_for'] ?? null,
$config['after_commit'] ?? null
$config['after_commit'] ?? null,
$config['migration_batch_size'] ?? -1
);
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/LuaScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function migrateExpiredJobs()
{
return <<<'LUA'
-- Get all of the jobs with an expired "score"...
local val = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[1])
local val = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[1], 'limit', 0, ARGV[2])

-- If we have values in the array, we will remove them from the first queue
-- and add them onto the destination queue in chunks of 100, which moves
Expand Down
16 changes: 14 additions & 2 deletions src/Illuminate/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class RedisQueue extends Queue implements QueueContract, ClearableQueue
*/
protected $blockFor = null;

/**
* The batch size to use when migrating delayed / expired jobs onto the primary queue.
*
* Negative values are infinite.
*
* @var int
*/
protected $migrationBatchSize = -1;

/**
* Create a new Redis queue instance.
*
Expand All @@ -54,21 +63,24 @@ class RedisQueue extends Queue implements QueueContract, ClearableQueue
* @param int $retryAfter
* @param int|null $blockFor
* @param bool $dispatchAfterCommit
* @param int $migrationBatchSize
* @return void
*/
public function __construct(Redis $redis,
$default = 'default',
$connection = null,
$retryAfter = 60,
$blockFor = null,
$dispatchAfterCommit = false)
$dispatchAfterCommit = false,
$migrationBatchSize = -1)
{
$this->redis = $redis;
$this->default = $default;
$this->blockFor = $blockFor;
$this->connection = $connection;
$this->retryAfter = $retryAfter;
$this->dispatchAfterCommit = $dispatchAfterCommit;
$this->migrationBatchSize = $migrationBatchSize;
}

/**
Expand Down Expand Up @@ -244,7 +256,7 @@ protected function migrate($queue)
public function migrateExpiredJobs($from, $to)
{
return $this->getConnection()->eval(
LuaScripts::migrateExpiredJobs(), 3, $from, $to, $to.':notify', $this->currentTime()
LuaScripts::migrateExpiredJobs(), 3, $from, $to, $to.':notify', $this->currentTime(), $this->migrationBatchSize
);
}

Expand Down