Queue command with direct redis access so it's faster
This commit is contained in:
@ -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
|
||||
{
|
||||
|
Reference in New Issue
Block a user