Files
oficial/cli/src/Command/Queue.php

66 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Incoviba\Command;
2025-05-13 15:49:41 -04:00
use DateTimeImmutable;
use Symfony\Component\Console;
use Incoviba\Common\Alias\Command;
#[Console\Attribute\AsCommand(
name: 'queue',
description: 'Run queue'
)]
class Queue extends Command
{
public function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
{
$this->logger->debug("Running {$this->getName()}");
$io = new Console\Style\SymfonyStyle($input, $output);
2025-05-13 15:40:32 -04:00
$now = new DateTimeImmutable();
$io->title("[{$now->format('Y-m-d H:i:s e')}] Running Queue...");
$jobs = $this->getJobs($output);
if (is_int($jobs)) {
return $jobs;
}
return $this->runJobs($output, $jobs);
}
protected function getJobs(Console\Output\OutputInterface $output): int|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'];
}
protected function runJobs(Console\Output\OutputInterface $output, array $jobs): int
{
$errors = 0;
foreach ($jobs as $job) {
if ($this->runJob($output, $job) === Console\Command\Command::FAILURE) {
$errors ++;
}
}
return $errors === 0 ? Console\Command\Command::SUCCESS : Console\Command\Command::FAILURE;
}
protected function runJob(Console\Output\OutputInterface $output, int $job_id): int
{
$uri = "/api/queue/run/{$job_id}";
$output->writeln("GET {$uri}");
$response = $this->client->get($uri);
$output->writeln("Response Code: {$response->getStatusCode()}");
return ((int) floor($response->getStatusCode() / 100) === 2) ? Console\Command\Command::SUCCESS : Console\Command\Command::FAILURE;
}
}