Skip to content

Commit

Permalink
use SKIP LOCKED for mysql 8.1 and pgsql 9.5 queue workers (#31287)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored Jan 29, 2020
1 parent ea3d291 commit 1f2175d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ class SQLiteGrammar extends Grammar
'&', '|', '<<', '>>',
];

/**
* Compile the lock into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param bool|string $value
* @return string
*/
protected function compileLock(Builder $query, $value)
{
return '';
}

/**
* Wrap a union subquery in parentheses.
*
Expand Down
21 changes: 20 additions & 1 deletion src/Illuminate/Queue/DatabaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Queue\Jobs\DatabaseJob;
use Illuminate\Queue\Jobs\DatabaseJobRecord;
use Illuminate\Support\Carbon;
use PDO;

class DatabaseQueue extends Queue implements QueueContract
{
Expand Down Expand Up @@ -211,7 +212,7 @@ public function pop($queue = null)
protected function getNextAvailableJob($queue)
{
$job = $this->database->table($this->table)
->lockForUpdate()
->lock($this->getLockForPopping())
->where('queue', $this->getQueue($queue))
->where(function ($query) {
$this->isAvailable($query);
Expand All @@ -223,6 +224,24 @@ protected function getNextAvailableJob($queue)
return $job ? new DatabaseJobRecord((object) $job) : null;
}

/**
* Get the lock required for popping the next job.
*
* @return string
*/
protected function getLockForPopping()
{
$databaseEngine = $this->database->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
$databaseVersion = $this->database->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);

if ($databaseEngine == 'mysql' && version_compare($databaseVersion, '8.0.1', '>=') ||
$databaseEngine == 'pgsql' && version_compare($databaseVersion, '9.5', '>=')) {
return 'FOR UPDATE SKIP LOCKED';
}

return 'FOR UPDATE';
}

/**
* Modify the query to check for available jobs.
*
Expand Down

0 comments on commit 1f2175d

Please sign in to comment.