8000 [6.x] Use SKIP LOCKED for mysql 8.1 and pgsql 9.5 queue workers by themsaid · Pull Request #31287 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[6.x] Use SKIP LOCKED for mysql 8.1 and pgsql 9.5 queue workers #31287

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 1 commit into from
Jan 29, 2020
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
use SKIP LOCKED for mysql 8.1 and pgsql 9.5 queue workers
  • Loading branch information
themsaid committed Jan 29, 2020
commit de9c7f39639c3f5981bc6c15e690f58dfadd47f7
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
< 8000 /td> */
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', '>=') ||
Copy link
Member

Choose a reason for hiding this comment

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

Kinda feels like a hack we need to look at the engine here. Feels like this should be the responsibility of the database component.

Copy link
Contributor

Choose a reason for hiding this comment

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

it is a hack :)

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

return 'FOR UPDATE';

Choose a reason for hiding this comment

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

'FOR UPDATE' does not seem to work in MSSQL while not in a cursor.

}

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