Queue command with direct redis access so it's faster

This commit is contained in:
Juan Pablo Vial
2025-05-16 13:56:32 -04:00
parent f47f86dd2b
commit 8ba54fd3ad
6 changed files with 103 additions and 18 deletions

26
cli/src/Service/Job.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace Incoviba\Service;
use Exception;
use Predis\Connection\ConnectionException;
use Psr\Log\LoggerInterface;
class Job
{
public function __construct(protected LoggerInterface $logger, protected Redis $redisService)
{
$this->redisKey = 'jobs';
}
protected string $redisKey;
public function getPending(): array
{
try {
$jobs = $this->redisService->get($this->redisKey);
return json_decode($jobs, true);
} catch (ConnectionException|Exception $exception) {
$this->logger->warning($exception);
return [];
}
}
}

41
cli/src/Service/Redis.php Normal file
View File

@ -0,0 +1,41 @@
<?php
namespace Incoviba\Service;
use Exception;
use Predis\ClientInterface;
use Predis\Connection\ConnectionException;
class Redis
{
public function __construct(protected ClientInterface $client) {}
/**
* @param string $name
* @return string|null
* @throws Exception|ConnectionException
*/
public function get(string $name): ?string
{
if (!$this->client->exists($name)) {
throw new Exception($name);
}
return $this->client->get($name);
}
/**
* @param string $name
* @param mixed $value
* @param int $expirationTTL
* @return void
* @throws ConnectionException
*/
public function set(string $name, mixed $value, int $expirationTTL = 60 * 60 * 24): void
{
$resolution = 'EX';
if ($expirationTTL === -1) {
$resolution = null;
$expirationTTL = null;
}
$this->client->set($name, $value, $resolution, $expirationTTL);
}
}