Skip to content
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
48 changes: 0 additions & 48 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,13 @@ parameters:
- vendor/codeigniter4/framework/system/Test/bootstrap.php
excludePaths:
ignoreErrors:
-
message: '#Cannot use \+\+ on array\|bool\|float\|int\|object\|string\|null.#'
paths:
- src/Commands/QueueWork.php
-
message: '#Variable \$config on left side of \?\?\= always exists and is not nullable.#'
paths:
- src/Config/Services.php
-
message: '#Call to an undefined method CodeIgniter\\Queue\\Handlers\\BaseHandler::push\(\).#'
paths:
- src/Handlers/BaseHandler.php
-
message: '#Call to deprecated function random_string\(\):#'
paths:
- src/Handlers/RedisHandler.php
- src/Handlers/PredisHandler.php
-
message: '#Cannot access property \$timestamp on array\|bool\|float\|int\|object\|string.#'
paths:
- tests/_support/Database/Seeds/TestRedisQueueSeeder.php
-
message: '#Access to an undefined property CodeIgniter\\I18n\\Time::\$timestamp.#'
paths:
- src/Handlers/BaseHandler.php
- src/Handlers/DatabaseHandler.php
- src/Handlers/RedisHandler.php
- src/Handlers/PredisHandler.php
- src/Models/QueueJobModel.php
- tests/RedisHandlerTest.php
- tests/PredisHandlerTest.php
-
message: '#Call to an undefined method CodeIgniter\\Queue\\Models\\QueueJobFailedModel::affectedRows\(\).#'
paths:
- src/Handlers/BaseHandler.php
-
message: '#Call to an undefined method CodeIgniter\\Queue\\Models\\QueueJobFailedModel::truncate\(\).#'
paths:
- src/Handlers/BaseHandler.php
-
message: '#Parameter \#3 \$tries of method CodeIgniter\\Queue\\Commands\\QueueWork::handleWork\(\) expects int\|null, string\|true\|null given.#'
paths:
- src/Commands/QueueWork.php
-
message: '#Parameter \#4 \$retryAfter of method CodeIgniter\\Queue\\Commands\\QueueWork::handleWork\(\) expects int\|null, string\|true\|null given.#'
paths:
- src/Commands/QueueWork.php
-
message: '#Expression on left side of \?\? is not nullable.#'
paths:
- src/Commands/QueueWork.php
-
message: '#Variable \$job might not be defined.#'
paths:
- src/Commands/QueueWork.php
universalObjectCratesClasses:
- CodeIgniter\Entity
- CodeIgniter\Entity\Entity
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/QueueWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ public function run(array $params)
$memory = $params['memory'] ?? CLI::getOption('memory') ?? 128;
$priority = $params['priority'] ?? CLI::getOption('priority') ?? $config->getQueuePriorities($queue) ?? 'default';
$tries = $params['tries'] ?? CLI::getOption('tries');
$tries = ($tries !== null) ? (int) $tries : $tries;
$retryAfter = $params['retry-after'] ?? CLI::getOption('retry-after');
$retryAfter = ($retryAfter !== null) ? (int) $retryAfter : $retryAfter;
$countJobs = 0;

if (array_key_exists('stop-when-empty', $params) || CLI::getOption('stop-when-empty')) {
Expand Down Expand Up @@ -190,7 +192,7 @@ private function handleWork(QueueJob $work, QueueConfig $config, ?int $tries, ?i

CLI::write('The processing of this job was successful', 'green');
} catch (Throwable $err) {
if (isset($job) && ++$work->attempts < $tries ?? $job->getTries()) {
if (isset($job) && ++$work->attempts < ($tries ?? $job->getTries())) {
// Schedule for later
service('queue')->later($work, $retryAfter ?? $job->getRetryAfter());
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/Config/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use CodeIgniter\Queue\Handlers\DatabaseHandler;
use CodeIgniter\Queue\Handlers\PredisHandler;
use CodeIgniter\Queue\Handlers\RedisHandler;
use CodeIgniter\Queue\Interfaces\JobInterface;
use CodeIgniter\Queue\Interfaces\QueueInterface;

class Queue extends BaseConfig
{
Expand All @@ -17,6 +19,8 @@ class Queue extends BaseConfig

/**
* Available handlers.
*
* @var array<string, class-string<QueueInterface>>
*/
public array $handlers = [
'database' => DatabaseHandler::class,
Expand Down Expand Up @@ -81,6 +85,8 @@ class Queue extends BaseConfig

/**
* Your jobs handlers.
*
* @var array<string, class-string<JobInterface>>
*/
public array $jobHandlers = [];

Expand All @@ -95,6 +101,8 @@ public function __construct()

/**
* Resolve job class name.
*
* @return class-string<JobInterface>
*/
public function resolveJobClass(string $name): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function queue(?QueueConfig $config = null, $getShared = true): Qu
return static::getSharedInstance('queue', $config);
}

/** @var QueueConfig $config */
/** @var QueueConfig|null $config */
$config ??= config('Queue');

return (new Queue($config))->init();
Expand Down
11 changes: 11 additions & 0 deletions src/Entities/QueueJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
namespace CodeIgniter\Queue\Entities;

use CodeIgniter\Entity\Entity;
use CodeIgniter\I18n\Time;

/**
* @property int $attempts
* @property Time $available_at
* @property Time $created_at
* @property int $id
* @property array $payload
* @property string $priority
* @property string $queue
* @property int $status
*/
class QueueJob extends Entity
{
protected $dates = ['available_at', 'created_at'];
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function pop(string $queue, array $priorities): ?QueueJob
$queueJob->status = Status::RESERVED->value;
$queueJob->syncOriginal();

$this->predis->hset("queues:{$queue}::reserved", $queueJob->id, json_encode($queueJob));
$this->predis->hset("queues:{$queue}::reserved", (string) $queueJob->id, json_encode($queueJob));

return $queueJob;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function pop(string $queue, array $priorities): ?QueueJob
$queueJob->status = Status::RESERVED->value;
$queueJob->syncOriginal();

$this->redis->hSet("queues:{$queue}::reserved", $queueJob->id, json_encode($queueJob));
$this->redis->hSet("queues:{$queue}::reserved", (string) $queueJob->id, json_encode($queueJob));

return $queueJob;
}
Expand All @@ -115,7 +115,7 @@ public function later(QueueJob $queueJob, int $seconds): bool
$queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp;

if ($result = (int) $this->redis->zAdd("queues:{$queueJob->queue}:{$queueJob->priority}", $queueJob->available_at->timestamp, json_encode($queueJob))) {
$this->redis->hDel("queues:{$queueJob->queue}::reserved", $queueJob->id);
$this->redis->hDel("queues:{$queueJob->queue}::reserved", (string) $queueJob->id);
}

return $result > 0;
Expand All @@ -130,7 +130,7 @@ public function failed(QueueJob $queueJob, Throwable $err, bool $keepJob): bool
$this->logFailed($queueJob, $err);
}

return (bool) $this->redis->hDel("queues:{$queueJob->queue}::reserved", $queueJob->id);
return (bool) $this->redis->hDel("queues:{$queueJob->queue}::reserved", (string) $queueJob->id);
}

/**
Expand All @@ -145,7 +145,7 @@ public function done(QueueJob $queueJob, bool $keepJob): bool
$this->redis->lPush("queues:{$queueJob->queue}::done", json_encode($queueJob));
}

return (bool) $this->redis->hDel("queues:{$queueJob->queue}::reserved", $queueJob->id);
return (bool) $this->redis->hDel("queues:{$queueJob->queue}::reserved", (string) $queueJob->id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Database/Seeds/TestRedisQueueSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function run(): void
'attempts' => 0,
'available_at' => 1_697_269_864,
]);
$redis->hSet("queues:{$jobQueue->queue}::reserved", $jobQueue->id, json_encode($jobQueue));
$redis->hSet("queues:{$jobQueue->queue}::reserved", (string) $jobQueue->id, json_encode($jobQueue));

$jobQueue = new QueueJob([
'id' => '1234567890654321',
Expand Down