54 lines
1.8 KiB
PHP
54 lines
1.8 KiB
PHP
<?php
|
|
namespace Contabilidad\Common\Command;
|
|
|
|
use Psr\Http\Client\ClientInterface;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class Queue extends Command {
|
|
protected $client;
|
|
public function __construct(ClientInterface $client = null, string $name = null) {
|
|
parent::__construct($name);
|
|
$this->setClient($client);
|
|
}
|
|
|
|
public function setClient(ClientInterface $client) {
|
|
$this->client = $client;
|
|
return $this;
|
|
}
|
|
public function getClient(): ClientInterface {
|
|
return $this->client;
|
|
}
|
|
|
|
public function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$response = $this->getClient()->get('/queues/pending');
|
|
if ($response->getStatusCode() !== 200) {
|
|
return Command::FAILURE;
|
|
}
|
|
$data = json_decode($response->getBody()->getContents());
|
|
$output = [
|
|
'input' => $data,
|
|
'processed' => []
|
|
];
|
|
foreach ($data->pending as $queue) {
|
|
$log = "Running {$queue->command} from queue. Created in {$queue->created}.";
|
|
error_log($log);
|
|
$cmd = '/usr/local/bin/php /app/bin/console ' . $queue->cmd;
|
|
exec($cmd, $result, $code);
|
|
if ($code != Command::SUCCESS) {
|
|
error_log(var_export($queue, true));
|
|
error_log(var_export($result, true));
|
|
continue;
|
|
}
|
|
$output['processed'] []= $queue->id;
|
|
}
|
|
$response = $this->getClient()->post('/queues/processed', ['json' => $output]);
|
|
if ($response->getStatusCode() !== 200) {
|
|
return Command::FAILURE;
|
|
}
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|