Jobs setup
This commit is contained in:
@ -1,14 +1,12 @@
|
||||
<?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;
|
||||
use ProVM\Service\Jobs;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'jobs:check',
|
||||
@ -17,35 +15,11 @@ use function Safe\json_decode;
|
||||
)]
|
||||
class Check extends Command
|
||||
{
|
||||
public function __construct(protected Communicator $communicator, protected LoggerInterface $logger, string $name = null)
|
||||
public function __construct(protected Jobs $service, 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();
|
||||
@ -53,17 +27,16 @@ class Check extends Command
|
||||
$io1 = new SymfonyStyle($input, $section1);
|
||||
$io2 = new SymfonyStyle($input, $section2);
|
||||
$io1->title('Checking Pending Jobs');
|
||||
$pending_jobs = $this->getPendingJobs();
|
||||
$pending_jobs = $this->service->getPending();
|
||||
$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)) {
|
||||
if ($this->service->run($job)) {
|
||||
$io2->success('Success');
|
||||
} else {
|
||||
$io2->error('Failure');
|
||||
|
Reference in New Issue
Block a user