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

[6.x] Optimize next available job in database queue #34990

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 11 additions & 6 deletions src/Illuminate/Queue/DatabaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,18 @@ public function pop($queue = null)
protected function getNextAvailableJob($queue)
{
$job = $this->database->table($this->table)
->lock($this->getLockForPopping())
Copy link
Contributor

@paras-malhotra paras-malhotra Oct 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to retain the lock like this to avoid deadlocks. See #31287

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although an optimization related to the database queue, this lock change appears to be a separate ongoing discussion unrelated to the changes I am recommending.

->where('queue', $this->getQueue($queue))
->where(function ($query) {
$this->isAvailable($query);
$this->isReservedButExpired($query);
->lockForUpdate()
->where('id', function ($query) use ($queue) {
$query->select('id')
->from($this->table)
->where('queue', $this->getQueue($queue))
->where(function ($query) {
$this->isAvailable($query);
$this->isReservedButExpired($query);
})
->orderBy('id', 'asc')
->limit(1);
})
->orderBy('id', 'asc')
->first();

return $job ? new DatabaseJobRecord((object) $job) : null;
Expand Down