From 8ba54fd3ad90f5308aa05e6c4908f08c0eaea49b Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Fri, 16 May 2025 13:56:32 -0400 Subject: [PATCH] Queue command with direct redis access so it's faster --- cli/composer.json | 1 + cli/setup/setups/logs.php | 2 +- cli/setup/setups/services.php | 19 ++++++++++++++++ cli/src/Command/Queue.php | 32 +++++++++++++-------------- cli/src/Service/Job.php | 26 ++++++++++++++++++++++ cli/src/Service/Redis.php | 41 +++++++++++++++++++++++++++++++++++ 6 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 cli/setup/setups/services.php create mode 100644 cli/src/Service/Job.php create mode 100644 cli/src/Service/Redis.php diff --git a/cli/composer.json b/cli/composer.json index 34b7e40..10f9450 100644 --- a/cli/composer.json +++ b/cli/composer.json @@ -6,6 +6,7 @@ "guzzlehttp/guzzle": "^7.8", "monolog/monolog": "^3.5", "php-di/php-di": "^7.0", + "predis/predis": "^3.0", "symfony/console": "^6.3" }, "require-dev": { diff --git a/cli/setup/setups/logs.php b/cli/setup/setups/logs.php index b698d1e..e62c9cd 100644 --- a/cli/setup/setups/logs.php +++ b/cli/setup/setups/logs.php @@ -4,7 +4,7 @@ use Psr\Container\ContainerInterface; return [ Psr\Log\LoggerInterface::class => function(ContainerInterface $container) { $minLogLevel = Monolog\Level::Debug; - if ($container->has('DEBUG') and !$container->get('DEBUG')) { + if ($container->has('DEBUG') and $container->get('DEBUG') === 'false') { $minLogLevel = Monolog\Level::Warning; } $handlers = []; diff --git a/cli/setup/setups/services.php b/cli/setup/setups/services.php new file mode 100644 index 0000000..dc19355 --- /dev/null +++ b/cli/setup/setups/services.php @@ -0,0 +1,19 @@ + function(ContainerInterface $container) { + $options = [ + 'scheme' => 'tcp', + 'host' => $container->get('REDIS_HOST'), + 'port' => $container->get('REDIS_PORT') + ]; + if ($container->has('REDIS_USER')) { + $options['username'] = $container->get('REDIS_USER'); + } + if ($container->has('REDIS_PASSWORD')) { + $options['password'] = $container->get('REDIS_PASSWORD'); + } + return new Predis\Client($options); + }, +]; diff --git a/cli/src/Command/Queue.php b/cli/src/Command/Queue.php index e23cb38..24f5619 100644 --- a/cli/src/Command/Queue.php +++ b/cli/src/Command/Queue.php @@ -2,7 +2,10 @@ namespace Incoviba\Command; use DateTimeImmutable; +use Psr\Http\Client\ClientInterface; +use Psr\Log\LoggerInterface; use Symfony\Component\Console; +use Incoviba\Service\Job; use Incoviba\Common\Alias\Command; #[Console\Attribute\AsCommand( @@ -11,6 +14,12 @@ use Incoviba\Common\Alias\Command; )] class Queue extends Command { + public function __construct(ClientInterface $client, LoggerInterface $logger, + protected Job $jobService, ?string $name = null) + { + parent::__construct($client, $logger, $name); + } + public function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int { $this->logger->debug("Running {$this->getName()}"); @@ -19,29 +28,18 @@ class Queue extends Command $io->title("[{$now->format('Y-m-d H:i:s e')}] Running Queue..."); $jobs = $this->getJobs($output); - if (is_int($jobs)) { - return $jobs; + if (count($jobs) === 0) { + return Console\Command\Command::SUCCESS; } return $this->runJobs($output, $jobs); } - protected function getJobs(Console\Output\OutputInterface $output): int|array + protected function getJobs(Console\Output\OutputInterface $output): array { - $uri = '/api/queue/jobs'; - $output->writeln("GET {$uri}"); - $response = $this->client->get($uri); - $output->writeln("Response Code: {$response->getStatusCode()}"); - if ($response->getStatusCode() !== 200) { - return Console\Command\Command::FAILURE; - } - - $contents = $response->getBody()->getContents(); - if (empty($contents)) { - return []; - } - - return json_decode($contents, true)['jobs']; + $this->logger->debug("Getting jobs"); + $jobs = $this->jobService->getPending(); + return array_column($jobs, 'id'); } protected function runJobs(Console\Output\OutputInterface $output, array $jobs): int { diff --git a/cli/src/Service/Job.php b/cli/src/Service/Job.php new file mode 100644 index 0000000..c017c1f --- /dev/null +++ b/cli/src/Service/Job.php @@ -0,0 +1,26 @@ +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 []; + } + } +} diff --git a/cli/src/Service/Redis.php b/cli/src/Service/Redis.php new file mode 100644 index 0000000..f091060 --- /dev/null +++ b/cli/src/Service/Redis.php @@ -0,0 +1,41 @@ +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); + } +}