Cleanup of cli
This commit is contained in:
79
cli/common/Command/Jobs/Check.php
Normal file
79
cli/common/Command/Jobs/Check.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
namespace ProVM\Command\Jobs;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use ProVM\Service\Communicator;
|
||||
use function Safe\json_decode;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'jobs:check',
|
||||
description: 'Check for pending jobs',
|
||||
hidden: false
|
||||
)]
|
||||
class Check extends Command
|
||||
{
|
||||
public function __construct(protected Communicator $communicator, protected LoggerInterface $logger, string $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
protected function getPendingJobs(): array
|
||||
{
|
||||
$this->logger->notice('Grabbing pending jobs.');
|
||||
$response = $this->communicator->get('/jobs/pending');
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return [];
|
||||
}
|
||||
return json_decode($body)->jobs;
|
||||
}
|
||||
protected function runJob($job): bool
|
||||
{
|
||||
$base_command = '/app/bin/emails';
|
||||
$cmd = [$base_command, $job->command];
|
||||
if ($job->arguments !== '') {
|
||||
$cmd []= $job->arguments;
|
||||
}
|
||||
$cmd = implode(' ', $cmd);
|
||||
$this->logger->notice("Running '{$cmd}'");
|
||||
$response = shell_exec($cmd);
|
||||
$this->logger->info("Result: {$response}");
|
||||
return $response !== false;
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$section1 = $output->section();
|
||||
$section2 = $output->section();
|
||||
$io1 = new SymfonyStyle($input, $section1);
|
||||
$io2 = new SymfonyStyle($input, $section2);
|
||||
$io1->title('Checking Pending Jobs');
|
||||
$pending_jobs = $this->getPendingJobs();
|
||||
$notice = 'Found ' . count($pending_jobs) . ' jobs';
|
||||
$io1->text($notice);
|
||||
$this->logger->info($notice);
|
||||
if (count($pending_jobs) > 0) {
|
||||
$io1->section('Running Jobs');
|
||||
$io1->progressStart(count($pending_jobs));
|
||||
foreach ($pending_jobs as $job) {
|
||||
$section2->clear();
|
||||
$io2->text("Running {$job->command}");
|
||||
if ($this->runJob($job)) {
|
||||
$io2->success('Success');
|
||||
} else {
|
||||
$io2->error('Failure');
|
||||
}
|
||||
$io1->progressAdvance();
|
||||
}
|
||||
}
|
||||
$section2->clear();
|
||||
$io2->success('Done');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user