Command queue

This commit is contained in:
2022-03-25 15:03:49 -03:00
parent e3737aba27
commit dbad283e14
7 changed files with 222 additions and 1 deletions

View File

@ -0,0 +1,53 @@
<?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;
}
$input = json_decode($response->getBody()->getContents());
$output = [
'input' => $input,
'processed' => []
];
foreach ($input->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;
}
}

View File

@ -21,4 +21,4 @@
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 2 1 * * /usr/local/bin/php /app/bin/console consolidar
0 2 * * * /usr/local/bin/php /app/bin/console queue

View File

@ -0,0 +1,4 @@
<?php
use Contabilidad\Common\Command\Queue;
$app->add(new Queue($app->getContainer()->get(\Psr\Http\Client\ClientInterface::class), 'queue'));