diff --git a/phpstan.neon.dist b/phpstan.neon.dist index fba7c30..924b3e7 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -15,8 +15,6 @@ parameters: paths: - src/Handlers/RedisHandler.php - src/Handlers/PredisHandler.php - - - message: '#Access to an undefined property CodeIgniter\\I18n\\Time::\$timestamp.#' - message: '#Call to an undefined method CodeIgniter\\Queue\\Models\\QueueJobFailedModel::affectedRows\(\).#' paths: diff --git a/src/Commands/Generators/JobGenerator.php b/src/Commands/Generators/JobGenerator.php index 5e525d4..d26c163 100644 --- a/src/Commands/Generators/JobGenerator.php +++ b/src/Commands/Generators/JobGenerator.php @@ -45,7 +45,7 @@ class JobGenerator extends BaseCommand /** * The Command's Arguments * - * @var array + * @var array */ protected $arguments = [ 'name' => 'The job class name.', @@ -54,7 +54,7 @@ class JobGenerator extends BaseCommand /** * The Command's Options * - * @var array + * @var array */ protected $options = [ '--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".', diff --git a/src/Commands/QueueClear.php b/src/Commands/QueueClear.php index cb86539..bad5ee1 100644 --- a/src/Commands/QueueClear.php +++ b/src/Commands/QueueClear.php @@ -40,7 +40,7 @@ class QueueClear extends BaseCommand /** * The Command's Arguments * - * @var array + * @var array */ protected $arguments = [ 'queueName' => 'Name of the queue we will work with.', diff --git a/src/Commands/QueueFailed.php b/src/Commands/QueueFailed.php index 89c4749..af793e3 100644 --- a/src/Commands/QueueFailed.php +++ b/src/Commands/QueueFailed.php @@ -41,7 +41,7 @@ class QueueFailed extends BaseCommand /** * The Command's Options * - * @var array + * @var array */ protected $options = [ '-queue' => 'Queue name.', diff --git a/src/Commands/QueueFlush.php b/src/Commands/QueueFlush.php index 8bd8e06..fe9a321 100644 --- a/src/Commands/QueueFlush.php +++ b/src/Commands/QueueFlush.php @@ -40,7 +40,7 @@ class QueueFlush extends BaseCommand /** * The Command's Options * - * @var array + * @var array */ protected $options = [ '-hours' => 'Number of hours.', diff --git a/src/Commands/QueueForget.php b/src/Commands/QueueForget.php index b99088d..dc88695 100644 --- a/src/Commands/QueueForget.php +++ b/src/Commands/QueueForget.php @@ -40,7 +40,7 @@ class QueueForget extends BaseCommand /** * The Command's Arguments * - * @var array + * @var array */ protected $arguments = [ 'id' => 'ID of the failed job.', diff --git a/src/Commands/QueueRetry.php b/src/Commands/QueueRetry.php index f1af6b0..ee7759d 100644 --- a/src/Commands/QueueRetry.php +++ b/src/Commands/QueueRetry.php @@ -40,7 +40,7 @@ class QueueRetry extends BaseCommand /** * The Command's Arguments * - * @var array + * @var array */ protected $arguments = [ 'id' => 'ID of the failed job or "all" for all failed jobs.', @@ -49,7 +49,7 @@ class QueueRetry extends BaseCommand /** * The Command's Options * - * @var array + * @var array */ protected $options = [ '-queue' => 'Queue name.', diff --git a/src/Commands/QueueStop.php b/src/Commands/QueueStop.php index 27966a2..0c2e2ca 100644 --- a/src/Commands/QueueStop.php +++ b/src/Commands/QueueStop.php @@ -40,7 +40,7 @@ class QueueStop extends BaseCommand /** * The Command's Arguments * - * @var array + * @var array */ protected $arguments = [ 'queueName' => 'Name of the queue we will work with.', @@ -49,7 +49,7 @@ class QueueStop extends BaseCommand /** * The Command's Options * - * @var array + * @var array */ protected $options = [ ]; diff --git a/src/Commands/QueueWork.php b/src/Commands/QueueWork.php index 1b45902..4c02bd6 100644 --- a/src/Commands/QueueWork.php +++ b/src/Commands/QueueWork.php @@ -44,7 +44,7 @@ class QueueWork extends BaseCommand /** * The Command's Arguments * - * @var array + * @var array */ protected $arguments = [ 'queueName' => 'Name of the queue we will work with.', @@ -53,7 +53,7 @@ class QueueWork extends BaseCommand /** * The Command's Options * - * @var array + * @var array */ protected $options = [ '-sleep' => 'Wait time between the next check for available job when the queue is empty. Default value: 10 (seconds).', diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index 8afe7c5..152cb8b 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -39,7 +39,7 @@ public function push(string $queue, string $job, array $data): bool 'priority' => $this->priority, 'status' => Status::PENDING->value, 'attempts' => 0, - 'available_at' => Time::now()->timestamp, + 'available_at' => Time::now(), ]); $this->priority = null; @@ -75,7 +75,7 @@ public function pop(string $queue, array $priorities): ?QueueJob public function later(QueueJob $queueJob, int $seconds): bool { $queueJob->status = Status::PENDING->value; - $queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp; + $queueJob->available_at = Time::now()->addSeconds($seconds); return $this->jobModel->save($queueJob); } diff --git a/src/Handlers/PredisHandler.php b/src/Handlers/PredisHandler.php index c5a33af..f9790c3 100644 --- a/src/Handlers/PredisHandler.php +++ b/src/Handlers/PredisHandler.php @@ -45,7 +45,7 @@ public function push(string $queue, string $job, array $data): bool 'priority' => $this->priority, 'status' => Status::PENDING->value, 'attempts' => 0, - 'available_at' => Time::now()->timestamp, + 'available_at' => Time::now(), ]); $result = $this->predis->zadd("queues:{$queue}:{$this->priority}", [json_encode($queueJob) => Time::now()->timestamp]); @@ -100,7 +100,7 @@ public function pop(string $queue, array $priorities): ?QueueJob public function later(QueueJob $queueJob, int $seconds): bool { $queueJob->status = Status::PENDING->value; - $queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp; + $queueJob->available_at = Time::now()->addSeconds($seconds); $result = $this->predis->zadd( "queues:{$queueJob->queue}:{$queueJob->priority}", diff --git a/src/Handlers/RedisHandler.php b/src/Handlers/RedisHandler.php index d435523..048b7e3 100644 --- a/src/Handlers/RedisHandler.php +++ b/src/Handlers/RedisHandler.php @@ -62,7 +62,7 @@ public function push(string $queue, string $job, array $data): bool 'priority' => $this->priority, 'status' => Status::PENDING->value, 'attempts' => 0, - 'available_at' => Time::now()->timestamp, + 'available_at' => Time::now(), ]); $result = (int) $this->redis->zAdd("queues:{$queue}:{$this->priority}", Time::now()->timestamp, json_encode($queueJob)); @@ -83,7 +83,7 @@ public function pop(string $queue, array $priorities): ?QueueJob $now = Time::now()->timestamp; foreach ($priorities as $priority) { - if ($tasks = $this->redis->zRangeByScore("queues:{$queue}:{$priority}", '-inf', $now, ['limit' => [0, 1]])) { + if ($tasks = $this->redis->zRangeByScore("queues:{$queue}:{$priority}", '-inf', (string) $now, ['limit' => [0, 1]])) { if ($this->redis->zRem("queues:{$queue}:{$priority}", ...$tasks)) { break; } @@ -114,7 +114,7 @@ public function pop(string $queue, array $priorities): ?QueueJob public function later(QueueJob $queueJob, int $seconds): bool { $queueJob->status = Status::PENDING->value; - $queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp; + $queueJob->available_at = Time::now()->addSeconds($seconds); $result = (int) $this->redis->zAdd( "queues:{$queueJob->queue}:{$queueJob->priority}", diff --git a/tests/DatabaseHandlerTest.php b/tests/DatabaseHandlerTest.php index 62eff81..e2887d6 100644 --- a/tests/DatabaseHandlerTest.php +++ b/tests/DatabaseHandlerTest.php @@ -4,6 +4,7 @@ namespace Tests; +use CodeIgniter\I18n\Time; use CodeIgniter\Queue\Entities\QueueJob; use CodeIgniter\Queue\Enums\Status; use CodeIgniter\Queue\Exceptions\QueueException; @@ -70,13 +71,16 @@ public function testPriorityNameLengthException(): void */ public function testPush(): void { + Time::setTestNow('2023-12-29 14:15:16'); + $handler = new DatabaseHandler($this->config); $result = $handler->push('queue', 'success', ['key' => 'value']); $this->assertTrue($result); $this->seeInDatabase('queue_jobs', [ - 'queue' => 'queue', - 'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]), + 'queue' => 'queue', + 'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]), + 'available_at' => '1703859316', ]); } @@ -85,36 +89,43 @@ public function testPush(): void */ public function testPushWithPriority(): void { + Time::setTestNow('2023-12-29 14:15:16'); + $handler = new DatabaseHandler($this->config); $result = $handler->setPriority('high')->push('queue', 'success', ['key' => 'value']); $this->assertTrue($result); $this->seeInDatabase('queue_jobs', [ - 'queue' => 'queue', - 'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]), - 'priority' => 'high', + 'queue' => 'queue', + 'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]), + 'priority' => 'high', + 'available_at' => '1703859316', ]); } public function testPushAndPopWithPriority(): void { + Time::setTestNow('2023-12-29 14:15:16'); + $handler = new DatabaseHandler($this->config); $result = $handler->push('queue', 'success', ['key1' => 'value1']); $this->assertTrue($result); $this->seeInDatabase('queue_jobs', [ - 'queue' => 'queue', - 'payload' => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]), - 'priority' => 'low', + 'queue' => 'queue', + 'payload' => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]), + 'priority' => 'low', + 'available_at' => '1703859316', ]); $result = $handler->setPriority('high')->push('queue', 'success', ['key2' => 'value2']); $this->assertTrue($result); $this->seeInDatabase('queue_jobs', [ - 'queue' => 'queue', - 'payload' => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]), - 'priority' => 'high', + 'queue' => 'queue', + 'payload' => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]), + 'priority' => 'high', + 'available_at' => '1703859316', ]); $result = $handler->pop('queue', ['high', 'low']); @@ -207,6 +218,8 @@ public function testPopEmpty(): void */ public function testLater(): void { + Time::setTestNow('2023-12-29 14:15:16'); + $handler = new DatabaseHandler($this->config); $queueJob = $handler->pop('queue1', ['default']); @@ -219,8 +232,9 @@ public function testLater(): void $this->assertTrue($result); $this->seeInDatabase('queue_jobs', [ - 'id' => 2, - 'status' => Status::PENDING->value, + 'id' => 2, + 'status' => Status::PENDING->value, + 'available_at' => Time::now()->addSeconds(60)->timestamp, ]); } @@ -229,6 +243,8 @@ public function testLater(): void */ public function testFailedAndKeepJob(): void { + Time::setTestNow('2023-12-29 14:15:16'); + $handler = new DatabaseHandler($this->config); $queueJob = $handler->pop('queue1', ['default']); @@ -243,6 +259,7 @@ public function testFailedAndKeepJob(): void 'id' => 2, 'connection' => 'database', 'queue' => 'queue1', + 'failed_at' => '1703859316', ]); } diff --git a/tests/_support/CLITestCase.php b/tests/_support/CLITestCase.php index 9cc05dd..4ec1e68 100644 --- a/tests/_support/CLITestCase.php +++ b/tests/_support/CLITestCase.php @@ -5,9 +5,7 @@ namespace Tests\Support; use CodeIgniter\CLI\CLI; -use CodeIgniter\I18n\Time; use CodeIgniter\Test\ReflectionHelper; -use Exception; abstract class CLITestCase extends TestCase { @@ -15,17 +13,6 @@ abstract class CLITestCase extends TestCase private array $lines = []; - /** - * @throws Exception - */ - protected function tearDown(): void - { - parent::tearDown(); - - // Reset the current time. - Time::setTestNow(); - } - protected function parseOutput(string $output): string { $this->lines = []; diff --git a/tests/_support/TestCase.php b/tests/_support/TestCase.php index 3814462..efed0d2 100644 --- a/tests/_support/TestCase.php +++ b/tests/_support/TestCase.php @@ -4,8 +4,10 @@ namespace Tests\Support; +use CodeIgniter\I18n\Time; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\DatabaseTestTrait; +use Exception; abstract class TestCase extends CIUnitTestCase { @@ -19,4 +21,15 @@ protected function setUp(): void parent::setUp(); } + + /** + * @throws Exception + */ + protected function tearDown(): void + { + parent::tearDown(); + + // Reset the current time. + Time::setTestNow(); + } }