Cleanup of cli

This commit is contained in:
2023-06-09 00:54:34 -04:00
parent 9307ba330c
commit 03c1dac2f2
36 changed files with 614 additions and 272 deletions

View File

@ -3,7 +3,7 @@ FROM php:8-cli
ENV PATH ${PATH}:/app/bin
RUN apt-get update \
&& apt-get install -y cron git libzip-dev unzip \
&& apt-get install -y cron git libzip-dev unzip qpdf \
&& rm -r /var/lib/apt/lists/* \
&& docker-php-ext-install zip

View File

@ -1,68 +0,0 @@
<?php
namespace ProVM\Common\Command;
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\Common\Service\Communicator;
use function Safe\json_decode;
#[AsCommand(
name: 'attachments:decrypt',
description: 'Decrypt attachments pending',
hidden: false
)]
class DecryptPdf extends Command
{
public function __construct(Communicator $communicator, string $name = null)
{
$this->setCommunicator($communicator);
parent::__construct($name);
}
protected Communicator $communicator;
public function getCommunicator(): Communicator
{
return $this->communicator;
}
public function setCommunicator(Communicator $communicator): DecryptPdf
{
$this->communicator = $communicator;
return $this;
}
protected function getAttachments(): array
{
$response = $this->getCommunicator()->get('/attachments/pending');
return json_decode($response->getBody()->getContents())->attachments;
}
protected function decrypt(string $attachment): bool
{
$response = $this->getCommunicator()->put('/attachments/decrypt', ['attachments' => [$attachment]]);
return json_decode($response->getBody()->getContents())->status;
}
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->title('Decrypt Attachments');
$io->section('Grabbing Attachments');
$attachments = $this->getAttachments();
$io->text('Found ' . count($attachments) . ' attachments.');
$io->section('Decrypting Attachments');
foreach ($attachments as $attachment) {
$status = $this->decrypt($attachment);
if ($status) {
$io->success("{$attachment} decrypted correctly.");
} else {
$io->error("Problem decrypting {$attachment}.");
}
}
$io->success('Done.');
return Command::SUCCESS;
}
}

View File

@ -1,66 +0,0 @@
<?php
namespace ProVM\Common\Command;
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\Common\Service\Communicator;
use function Safe\json_decode;
#[AsCommand(
name: 'attachments:grab',
description: 'Grab attachments from pending messages',
aliases: ['attachments:get'],
hidden: false
)]
class GrabAttachments extends Command
{
public function __construct(Communicator $communicator, string $name = null)
{
$this->setCommunicator($communicator);
parent::__construct($name);
}
protected Communicator $service;
public function getCommunicator(): Communicator
{
return $this->service;
}
public function setCommunicator(Communicator $service): GrabAttachments
{
$this->service = $service;
return $this;
}
protected function getMessages(): array
{
$response = $this->getCommunicator()->get('/messages/pending');
return json_decode($response->getBody()->getContents())->messages;
}
protected function grabAttachments(int $message_uid): int
{
$response = $this->getCommunicator()->put('/attachments/grab', ['messages' => [$message_uid]]);
return json_decode($response->getBody()->getContents())->attachment_count;
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('Grab Attachments');
$io->section('Grabbing Messages');
$messages = $this->getMessages();
$io->text('Found ' . count($messages) . ' messages.');
$io->section('Grabbing Attachments');
foreach ($messages as $job) {
$message = $job->message;
$attachments = $this->grabAttachments($message->uid);
$io->text("Found {$attachments} attachments for message UID:{$message->uid}.");
}
$io->success('Done.');
return Command::SUCCESS;
}
}

View 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;
}
}

View File

@ -0,0 +1,76 @@
<?php
namespace ProVM\Command\Mailboxes;
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;
#[AsCommand(
name: 'mailboxes:check',
description: 'Check registered mailboxes for new emails',
hidden: false
)]
class Check extends Command
{
public function __construct(protected Communicator $communicator, protected int $min_check_days, 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();
$section2 = $output->section();
$io1 = new SymfonyStyle($input, $section1);
$io2 = new SymfonyStyle($input, $section2);
$io1->title('Checking for New Messages');
$mailboxes = $this->getMailboxes();
$notice = 'Found ' . count($mailboxes) . ' mailboxes';
$io1->text($notice);
if (count($mailboxes) > 0) {
$io1->section('Checking for new messages');
$io1->progressStart(count($mailboxes));
foreach ($mailboxes as $mailbox) {
$section2->clear();
$io2->text("Checking {$mailbox->name}");
if ($this->checkMailbox($mailbox)) {
$io2->success("Found new emails in {$mailbox->name}");
} else {
$io2->info("No new emails in {$mailbox->name}");
}
$io1->progressAdvance();
}
$io1->progressFinish();
}
$section2->clear();
$io2->success('Done');
return Command::SUCCESS;
}
}

View File

@ -1,65 +1,62 @@
<?php
namespace ProVM\Common\Command;
namespace ProVM\Command\Messages;
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\Common\Service\Communicator;
use ProVM\Service\Communicator;
use function Safe\json_decode;
#[AsCommand(
name: 'messages:grab',
description: 'Run grab messages job for registered mailboxes',
aliases: ['messages'],
hidden: false
)]
class Messages extends Command
class Grab extends Command
{
public function __construct(Communicator $communicator, string $name = null)
{
$this->setCommunicator($communicator);
parent::__construct($name);
}
protected function configure()
{
$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): Messages
public function setCommunicator(Communicator $communicator): Grab
{
$this->communicator = $communicator;
return $this;
}
protected function getMailboxes(): array
protected function grabMessages(int $mailbox_id): int
{
$response = $this->getCommunicator()->get('/mailboxes/registered');
return json_decode($response->getBody()->getContents())->mailboxes;
}
protected function grabMessages(string $mailbox): int
{
$response = $this->getCommunicator()->put('/messages/grab', ['mailboxes' => [$mailbox]]);
$body = json_decode($response->getBody()->getContents());
return $body->message_count;
$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);
$io->title('Messages');
$io->section('Grabbing Registered Mailboxes');
$mailboxes = $this->getMailboxes();
$io->text('Found ' . count($mailboxes) . ' registered mailboxes.');
$mailbox_id = $input->getArgument('mailbox_id');
$io->title("Grabbing Messages for Mailbox ID {$mailbox_id}");
$io->section('Grabbing Messages');
foreach ($mailboxes as $mailbox) {
$message_count = $this->grabMessages($mailbox->name);
$io->text("Found {$message_count} messages in {$mailbox->name}.");
}
$count = $this->grabMessages($mailbox_id);
$io->info("Found {$count} messages");
$io->success('Done.');
return Command::SUCCESS;

View File

@ -1,5 +1,5 @@
<?php
namespace ProVM\Common\Middleware;
namespace ProVM\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

View File

@ -1,9 +1,11 @@
<?php
namespace ProVM\Common\Service;
namespace ProVM\Service;
use HttpResponseException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Safe\Exceptions\JsonException;
use function Safe\json_encode;
class Communicator
@ -26,6 +28,9 @@ class Communicator
return $this;
}
/**
* @throws HttpResponseException
*/
protected function handleResponse(ResponseInterface $response): ResponseInterface
{
if ($response->getStatusCode() < 200 or $response->getStatusCode() >= 300) {
@ -33,6 +38,11 @@ class Communicator
}
return $response;
}
/**
* @throws HttpResponseException
* @throws JsonException
*/
protected function request(string $method, string $uri, ?array $body = null): ResponseInterface
{
$options = [];
@ -45,20 +55,39 @@ class Communicator
return $this->handleResponse($this->getClient()->request($method, $uri, $options));
}
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function get(string $uri): ResponseInterface
{
return $this->request('get', $uri);
}
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function post(string $uri, array $data): ResponseInterface
{
return $this->request('post', $uri, $data);
}
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function put(string $uri, array $data): ResponseInterface
{
return $this->request('put', $uri, $data);
}
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function delete(string $uri, array $data): ResponseInterface
{
return $this->request('delete', $uri, $data);
}
}
}

View File

@ -1,5 +1,5 @@
<?php
namespace ProVM\Common\Wrapper;
namespace ProVM\Wrapper;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Application as Base;
@ -22,4 +22,4 @@ class Application extends Base
$this->container = $container;
return $this;
}
}
}

View File

@ -15,7 +15,7 @@
},
"autoload": {
"psr-4": {
"ProVM\\Common\\": "common/"
"ProVM\\": "common/"
}
},
"authors": [

View File

@ -1,3 +1,10 @@
# minutes hour day_of_month month day_of_week command
0 2 * * 2-6 /app/bin/emails messages:grab >> /logs/messages.log
0 3 * * 2-6 /app/bin/emails attachments:grab >> /logs/attachments.log
#0 2 * * 2-6 /app/bin/emails messages:grab >> /logs/messages.log
#0 3 * * 2-6 /app/bin/emails attachments:grab >> /logs/attachments.log
# Pending jobs every minute
1 * * * * /app/bin/emails jobs:pending >> /logs/jobs.log
# Check mailboxes for new emails every weekday
0 0 * * 2-6 /app/bin/emails mailboxes:check >> /logs/mailboxes.log
# Check attachments every weekday
0 1 * * 2-6 /app/bin/emails attachments:check >> /logs/attachments.log

View File

@ -12,4 +12,5 @@ services:
- .key.env
volumes:
- ${CLI_PATH:-.}/:/app
- ./logs/cli:/logs
- ${LOGS_PATH}/cli:/logs
- ${ATT_PATH}:/attachments

View File

@ -7,9 +7,8 @@ $app = require_once implode(DIRECTORY_SEPARATOR, [
Monolog\ErrorHandler::register($app->getContainer()->get(Psr\Log\LoggerInterface::class));
try {
$app->run();
} catch (Error | Exception $e) {
$logger = $app->getContainer()->get(Psr\Log\LoggerInterface::class);
$logger->debug(Safe\json_encode(compact('_SERVER')));
$logger->error($e);
throw $e;
} catch (Error $e) {
$app->getContainer()->get(Psr\Log\LoggerInterface::class)->error($e);
} catch (Exception $e) {
$app->getContainer()->get(Psr\Log\LoggerInterface::class)->warning($e);
}

View File

@ -0,0 +1,2 @@
<?php
$app->add($app->getContainer()->get(ProVM\Command\Mailboxes\Check::class));

View File

@ -1,2 +1,2 @@
<?php
$app->add($app->getContainer()->get(\ProVM\Common\Command\Messages::class));
$app->add($app->getContainer()->get(ProVM\Command\Messages\Grab::class));

View File

@ -1,3 +1,4 @@
<?php
$app->add($app->getContainer()->get(\ProVM\Common\Command\GrabAttachments::class));
$app->add($app->getContainer()->get(\ProVM\Common\Command\DecryptPdf::class));
$app->add($app->getContainer()->get(ProVM\Command\Attachments\Check::class));
$app->add($app->getContainer()->get(ProVM\Command\Attachments\Grab::class));
$app->add($app->getContainer()->get(ProVM\Command\Attachments\Decrypt::class));

View File

@ -0,0 +1,2 @@
<?php
$app->add($app->getContainer()->get(ProVM\Command\Jobs\Check::class));

View File

@ -1,7 +1,7 @@
<?php
require_once 'composer.php';
$builder = new \DI\ContainerBuilder();
$builder = new DI\ContainerBuilder();
$folders = [
'settings',
@ -24,7 +24,7 @@ foreach ($folders as $f) {
}
}
$app = new \ProVM\Common\Wrapper\Application($builder->build());
$app = new ProVM\Wrapper\Application($builder->build());
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,

View File

@ -1,2 +1,2 @@
<?php
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Logging::class));
//$app->add($app->getContainer()->get(ProVM\Common\Middleware\Logging::class));

View File

@ -1,5 +1,8 @@
<?php
return [
'api_uri' => $_ENV['API_URI'],
'api_key' => sha1($_ENV['API_KEY'])
'api_key' => sha1($_ENV['API_KEY']),
'passwords' => function() {
return explode($_ENV['PASSWORDS_SEPARATOR'] ?? ',', $_ENV['PASSWORDS'] ?? '');
},
];

View File

@ -2,12 +2,12 @@
use Psr\Container\ContainerInterface;
return [
\Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) {
return new \GuzzleHttp\Client([
Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) {
return new GuzzleHttp\Client([
'base_uri' => $container->get('api_uri'),
'headers' => [
'Authorization' => "Bearer {$container->get('api_key')}"
]
]);
}
];
];

View File

@ -2,7 +2,7 @@
use Psr\Container\ContainerInterface;
return [
ProVM\Common\Middleware\Logging::class => function(ContainerInterface $container) {
return new ProVM\Common\Middleware\Logging($container->get('request_logger'));
ProVM\Middleware\Logging::class => function(ContainerInterface $container) {
return new ProVM\Middleware\Logging($container->get('request_logger'));
}
];

View File

@ -0,0 +1,20 @@
<?php
use Psr\Container\ContainerInterface;
return [
ProVM\Command\Attachments\DecryptPdf::class => function(ContainerInterface $container) {
return new ProVM\Command\Attachments\DecryptPdf(
$container->get(ProVM\Service\Communicator::class),
$container->get(Psr\Log\LoggerInterface::class),
'qpdf',
$container->get('passwords')
);
},
ProVM\Command\Mailboxes\Check::class => function(ContainerInterface $container) {
return new ProVM\Command\Mailboxes\Check(
$container->get(ProVM\Service\Communicator::class),
1
);
}
];

View File

@ -2,28 +2,50 @@
use Psr\Container\ContainerInterface;
return [
Monolog\Handler\RotatingFileHandler::class => function(ContainerInterface $container) {
$handler = new Monolog\Handler\RotatingFileHandler($container->get('log_file'));
$handler->setFormatter($container->get(Monolog\Formatter\LineFormatter::class));
return $handler;
'log_processors' => function(ContainerInterface $container) {
return [
$container->get(Monolog\Processor\PsrLogMessageProcessor::class),
$container->get(Monolog\Processor\IntrospectionProcessor::class),
$container->get(Monolog\Processor\MemoryPeakUsageProcessor::class),
];
},
'request_log_handler' => function(ContainerInterface $container) {
return (new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log'])))
->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true));
},
'request_logger' => function(ContainerInterface $container) {
$logger = new Monolog\Logger('request_logger');
$handler = new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log']));
$handler->setFormatter($container->get(Monolog\Formatter\SyslogFormatter::class));
$dedupHandler = new Monolog\Handler\DeduplicationHandler($handler, null, Monolog\Level::Info);
$logger->pushHandler($dedupHandler);
$logger->pushProcessor($container->get(Monolog\Processor\PsrLogMessageProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\IntrospectionProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\MemoryUsageProcessor::class));
return $logger;
return new Monolog\Logger(
'request_logger',
[$container->get('request_log_handler')],
$container->get('log_processors')
);
},
'file_log_handler' => function(ContainerInterface $container) {
return new Monolog\Handler\FilterHandler(
(new Monolog\Handler\RotatingFileHandler($container->get('log_file')))
->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
Monolog\Level::Error
);
},
'debug_log_handler' => function(ContainerInterface $container) {
return new Monolog\Handler\FilterHandler(
(new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'debug.log'])))
->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
Monolog\Level::Debug,
Monolog\Level::Warning
);
},
Psr\Log\LoggerInterface::class => function(ContainerInterface $container) {
$logger = new Monolog\Logger('file_logger');
$logger->pushHandler($container->get(Monolog\Handler\RotatingFileHandler::class));
$logger->pushProcessor($container->get(Monolog\Processor\PsrLogMessageProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\IntrospectionProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\MemoryUsageProcessor::class));
return $logger;
return $container->get('file_logger');
},
'file_logger' => function(ContainerInterface $container) {
return new Monolog\Logger(
'file',
[
$container->get('file_log_handler'),
$container->get('debug_log_handler')
],
$container->get('log_processors')
);
},
];