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');
|
||||
|
41
cli/common/Command/Jobs/Execute.php
Normal file
41
cli/common/Command/Jobs/Execute.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace ProVM\Command\Jobs;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use ProVM\Service\Jobs;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'jobs:execute',
|
||||
description: 'Execute job by job_id',
|
||||
hidden: false
|
||||
)]
|
||||
class Execute extends Command
|
||||
{
|
||||
public function __construct(protected Jobs $service, string $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->addArgument('job_id', InputArgument::REQUIRED, 'Job ID to be executed');
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$job_id = $input->getArgument('job_id');
|
||||
$job = $this->service->get($job_id);
|
||||
if ($this->service->run($job)) {
|
||||
$io->success('Success');
|
||||
} else {
|
||||
$io->error('Failed');
|
||||
}
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
@ -5,9 +5,8 @@ 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 ProVM\Service\Communicator;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use function Safe\json_decode;
|
||||
use ProVM\Service\Mailboxes;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'mailboxes:check',
|
||||
@ -16,33 +15,11 @@ use function Safe\json_decode;
|
||||
)]
|
||||
class Check extends Command
|
||||
{
|
||||
public function __construct(protected Communicator $communicator, protected int $min_check_days, string $name = null)
|
||||
public function __construct(protected Mailboxes $service, string $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
protected function getMailboxes(): array
|
||||
{
|
||||
$response = $this->communicator->get('/mailboxes/registered');
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return [];
|
||||
}
|
||||
return json_decode($body)->mailboxes;
|
||||
}
|
||||
protected function checkMailbox($mailbox): bool
|
||||
{
|
||||
if ((new \DateTimeImmutable())->diff(new \DateTimeImmutable($mailbox->last_checked->date->date))->days < $this->min_check_days) {
|
||||
return true;
|
||||
}
|
||||
$response = $this->communicator->get("/mailbox/{$mailbox->id}/check");
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return true;
|
||||
}
|
||||
return json_decode($body)->status;
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$section1 = $output->section();
|
||||
@ -50,7 +27,7 @@ class Check extends Command
|
||||
$io1 = new SymfonyStyle($input, $section1);
|
||||
$io2 = new SymfonyStyle($input, $section2);
|
||||
$io1->title('Checking for New Messages');
|
||||
$mailboxes = $this->getMailboxes();
|
||||
$mailboxes = $this->service->getAll();
|
||||
$notice = 'Found ' . count($mailboxes) . ' mailboxes';
|
||||
$io1->text($notice);
|
||||
if (count($mailboxes) > 0) {
|
||||
@ -59,7 +36,7 @@ class Check extends Command
|
||||
foreach ($mailboxes as $mailbox) {
|
||||
$section2->clear();
|
||||
$io2->text("Checking {$mailbox->name}");
|
||||
if ($this->checkMailbox($mailbox)) {
|
||||
if ($this->service->check($mailbox)) {
|
||||
$io2->success("Found new emails in {$mailbox->name}");
|
||||
} else {
|
||||
$io2->info("No new emails in {$mailbox->name}");
|
||||
|
@ -7,19 +7,17 @@ use Symfony\Component\Console\Input\InputArgument;
|
||||
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\Mailboxes;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'messages:grab',
|
||||
description: 'Run grab messages job for registered mailboxes',
|
||||
description: 'Run grab messages job for mailbox',
|
||||
hidden: false
|
||||
)]
|
||||
class Grab extends Command
|
||||
{
|
||||
public function __construct(Communicator $communicator, string $name = null)
|
||||
public function __construct(protected Mailboxes $service, string $name = null)
|
||||
{
|
||||
$this->setCommunicator($communicator);
|
||||
parent::__construct($name);
|
||||
}
|
||||
protected function configure()
|
||||
@ -27,27 +25,6 @@ class Grab extends Command
|
||||
$this->addArgument('mailbox_id', InputArgument::REQUIRED, 'Mailbox ID to grab emails');
|
||||
}
|
||||
|
||||
protected Communicator $communicator;
|
||||
public function getCommunicator(): Communicator
|
||||
{
|
||||
return $this->communicator;
|
||||
}
|
||||
public function setCommunicator(Communicator $communicator): Grab
|
||||
{
|
||||
$this->communicator = $communicator;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function grabMessages(int $mailbox_id): int
|
||||
{
|
||||
$response = $this->getCommunicator()->get("/mailbox/{$mailbox_id}/grab");
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return 0;
|
||||
}
|
||||
return json_decode($body)->messages->count;
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
@ -55,7 +32,7 @@ class Grab extends Command
|
||||
$mailbox_id = $input->getArgument('mailbox_id');
|
||||
$io->title("Grabbing Messages for Mailbox ID {$mailbox_id}");
|
||||
$io->section('Grabbing Messages');
|
||||
$count = $this->grabMessages($mailbox_id);
|
||||
$count = $this->service->grabMessages($mailbox_id);
|
||||
$io->info("Found {$count} messages");
|
||||
$io->success('Done.');
|
||||
|
||||
|
Reference in New Issue
Block a user