Varios cambios Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: http://git.provm.cl/Incoviba/oficial/pulls/25
82 lines
2.7 KiB
PHP
82 lines
2.7 KiB
PHP
<?php
|
|
namespace Incoviba\Command;
|
|
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
use Psr\Http\Client\ClientInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Console;
|
|
use Incoviba\Service\Job;
|
|
use Incoviba\Common\Alias\Command;
|
|
use Throwable;
|
|
|
|
#[Console\Attribute\AsCommand(
|
|
name: 'queue',
|
|
description: 'Run queue'
|
|
)]
|
|
class Queue extends Command
|
|
{
|
|
public function __construct(ClientInterface $client, LoggerInterface $logger,
|
|
protected Job $jobService,
|
|
protected DateTimeZone $timezone,
|
|
protected string $baseCommand = '/code/bin/incoviba',
|
|
protected int $batchSize = 10,
|
|
?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()}");
|
|
$this->sections = [
|
|
'top' => $output->section(),
|
|
'bottom' => $output->section(),
|
|
];
|
|
$io = new Console\Style\SymfonyStyle($input, $this->sections['top']);
|
|
$now = new DateTimeImmutable('now', $this->timezone);
|
|
|
|
if ($this->jobService->getPending() === 0) {
|
|
$io->success("[{$now->format('Y-m-d H:i:s e')}] Queue is empty");
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$io->title("[{$now->format('Y-m-d H:i:s e')}] Running Queue...");
|
|
$results = [];
|
|
for ($i = 0; $i < $this->batchSize; $i++) {
|
|
if ($this->jobService->getPending() === 0) {
|
|
break;
|
|
}
|
|
$results []= $this->runJob();
|
|
}
|
|
return count(array_filter($results, fn ($result) => $result === self::FAILURE)) === 0 ? self::SUCCESS : self::FAILURE;
|
|
}
|
|
|
|
protected array $sections;
|
|
protected array $outputs = [];
|
|
protected function runJob(): int
|
|
{
|
|
$baseCommand = "{$this->baseCommand} jobs:run";
|
|
$command = "{$baseCommand}";
|
|
try {
|
|
exec($command, $output, $resultCode);
|
|
$this->outputs []= $output;
|
|
} catch (Throwable $exception) {
|
|
$this->logger->error("Failed to run command", [
|
|
'command' => $command,
|
|
'exception' => $exception
|
|
]);
|
|
return self::FAILURE;
|
|
}
|
|
if ($resultCode !== 0) {
|
|
$this->logger->error("Failed to run command", [
|
|
'command' => $command,
|
|
'result_code' => $resultCode
|
|
]);
|
|
return self::FAILURE;
|
|
} else {
|
|
return self::SUCCESS;
|
|
}
|
|
}
|
|
}
|