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

Add skipLocked config for the database handler #48

Merged
merged 1 commit into from
Sep 19, 2024
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
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The configuration settings for `database` handler.

* `dbGroup` - The database group to use. Default value: `default`.
* `getShared` - Weather to use shared instance. Default value: `true`.
* `skipLocked` - Weather to use "skip locked" feature to maintain concurrency calls. Default to `true`.

### $redis

Expand Down
2 changes: 1 addition & 1 deletion docs/running-queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ This way, worker will consume jobs with the `low` priority and then with `high`.

### Running many instances of the same queue

As mentioned above, sometimes we may want to have multiple instances of the same command running at the same time. The queue is safe to use in that scenario with all databases except `SQLite3` since it doesn't guarantee that the job will be selected only by one process.
As mentioned above, sometimes we may want to have multiple instances of the same command running at the same time. The queue is safe to use in that scenario with all databases if you keep the `skipLocked` to `true` in the config file. Only for SQLite3 driver this setting is not relevant.

### Handling long-running process

Expand Down
3 changes: 3 additions & 0 deletions src/Config/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class Queue extends BaseConfig
public array $database = [
'dbGroup' => 'default',
'getShared' => true,
// use skip locked feature to maintain concurrency calls
// this is not relevant for the SQLite3 database driver
'skipLocked' => true,
];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Models/QueueJobModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getFromQueue(string $name, array $priority): ?QueueJob
*/
private function skipLocked(string $sql): string
{
if ($this->db->DBDriver === 'SQLite3') {
if ($this->db->DBDriver === 'SQLite3' || config('Queue')->database['skipLocked'] === false) {
return $sql;
}

Expand Down
64 changes: 64 additions & 0 deletions tests/Models/QueueJobModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter Queue.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Models;

use CodeIgniter\Queue\Models\QueueJobModel;
use CodeIgniter\Test\ReflectionHelper;
use Tests\Support\TestCase;

/**
* @internal
*/
final class QueueJobModelTest extends TestCase
{
use ReflectionHelper;

public function testQueueJobModel(): void
{
$model = model(QueueJobModel::class);
$this->assertInstanceOf(QueueJobModel::class, $model);
}

public function testSkipLocked(): void
{
$model = model(QueueJobModel::class);
$method = $this->getPrivateMethodInvoker($model, 'skipLocked');

$sql = 'SELECT * FROM queue_jobs WHERE queue = "test" AND status = 0 AND available_at < 123456 LIMIT 1';
$result = $method($sql);

if ($model->db->DBDriver === 'SQLite3') {
$this->assertSame($sql, $result);
} elseif ($model->db->DBDriver === 'SQLSRV') {
$expected = 'SELECT * FROM queue_jobs WITH (ROWLOCK,UPDLOCK,READPAST) WHERE queue = "test" AND status = 0 AND available_at < 123456 LIMIT 1';
$this->assertSame($expected, $result);
} else {
$expected = 'SELECT * FROM queue_jobs WHERE queue = "test" AND status = 0 AND available_at < 123456 LIMIT 1 FOR UPDATE SKIP LOCKED';
$this->assertSame($expected, $result);
}
}

public function testSkipLockedFalse(): void
{
config('Queue')->database['skipLocked'] = false;

$model = model(QueueJobModel::class);
$method = $this->getPrivateMethodInvoker($model, 'skipLocked');

$sql = 'SELECT * FROM queue_jobs WHERE queue = "test" AND status = 0 AND available_at < 123456 LIMIT 1';
$result = $method($sql);

$this->assertSame($sql, $result);
}
}
5 changes: 3 additions & 2 deletions tests/_support/Config/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class Queue extends BaseQueue
* Database handler config.
*/
public array $database = [
'dbGroup' => 'default',
'getShared' => true,
'dbGroup' => 'default',
'getShared' => true,
'skipLocked' => true,
];

/**
Expand Down
Loading