Jobs setup

This commit is contained in:
2023-06-12 21:14:07 -04:00
parent 03c1dac2f2
commit 88f91c4bd5
60 changed files with 965 additions and 495 deletions

View File

@ -1,19 +1,20 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Common\Exception\Request\MissingArgument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Attachments as Service;
use ProVM\Emails\Model\Attachment;
use Psr\Log\LoggerInterface;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service;
use ProVM\Emails\Model\Attachment;
use ProVM\Common\Exception\Request\MissingArgument;
use function Safe\json_decode;
class Attachments
{
use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service): ResponseInterface
{
$attachments = array_map(function(Attachment $attachment) {
return $attachment->toArray();
@ -24,35 +25,7 @@ class Attachments
];
return $this->withJson($response, $output);
}
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Jobs $jobsService): ResponseInterface
{
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'message UIDs');
}
$output = [
'messages' => $json->messages,
'total' => count($json->messages),
'saved' => [
'attachments' => [],
'total' => 0
]
];
foreach ($json->messages as $message_id) {
if (!$jobsService->isPending($message_id)) {
continue;
}
if ($service->grab($message_id)) {
$job = $jobsService->find($message_id);
$jobsService->execute($job->getId());
$output['saved']['attachments'] []= $job->toArray();
$output['saved']['total'] ++;
}
}
return $this->withJson($response, $output);
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, LoggerInterface $logger, int $attachment_id): ResponseInterface
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service, LoggerInterface $logger, int $attachment_id): ResponseInterface
{
$attachment = $service->getRepository()->fetchById($attachment_id);
@ -61,4 +34,25 @@ class Attachments
$response->getBody()->write($service->getFile($attachment_id));
return $response;
}
}
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service, Service\Messages $messagesService): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'Messages UIDs');
}
$output = [
'messages' => $json->messages,
'attachments' => [],
'total' => 0
];
foreach ($json->messages as $message_uid) {
$message = $messagesService->getLocalMessage($message_uid);
$attachments = $service->grab($message->getId());
$output['attachments'] = array_merge($output['attachments'], $attachments);
$output['total'] += count($attachments);
}
return $this->withJson($response, $output);
}
}

View File

@ -12,7 +12,8 @@ class Base
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
return $this->withJson($response, [
'version' => '1.0.0'
'version' => '1.0.0',
'app' => 'emails'
]);
}
}
}

View File

@ -1,7 +1,6 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Emails\Model\Job;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Exception\Request\MissingArgument;
@ -13,23 +12,25 @@ class Jobs
{
use Json;
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Messages $messagesService): ResponseInterface
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$jobs = $service->getRepository()->fetchAll();
return $this->withJson($response, compact('jobs'));
}
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'messages ids');
if (!isset($json->jobs)) {
throw new MissingArgument('jobs', 'array', 'job commands with arguments');
}
$output = [
'messages' => $json->messages,
'total' => count($json->messages),
'jobs' => $json->jobs,
'total' => count($json->jobs),
'scheduled' => 0
];
foreach ($json->messages as $message_id) {
if ($service->schedule($message_id)) {
$message = $messagesService->getRepository()->fetchById($message_id);
$message->doesHaveScheduledDownloads();
$messagesService->getRepository()->save($message);
foreach ($json->jobs as $job) {
if ($service->queue($job->command, $job->arguments)) {
$output['scheduled'] ++;
}
}
@ -37,12 +38,47 @@ class Jobs
}
public function pending(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$pending = array_map(function(Job $job) {
return $job->toArray();
}, $service->getPending());
$pending = $service->getPending();
$output = [
'total' => count($pending),
'pending' => $pending
'jobs' => $pending
];
return $this->withJson($response, $output);
}
public function pendingCommands(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$body = $response->getBody();
$json = json_decode($body->getContents());
if (!isset($json->commands)) {
throw new MissingArgument('commands', 'array', 'job commands');
}
$output = [
'commands' => $json->commands,
'total' => count($json->commands),
'pending' => []
];
foreach ($json->commands as $command) {
$pending = $service->getPendingByCommand($command);
if (count($pending) === 0) {
continue;
}
$output['pending'][$command] = $pending;
}
return $this->withJson($response, $output);
}
public function finish(ServerRequestInterface $request, ResponseInterface $response, Service $service, $job_id): ResponseInterface
{
$output = [
'job_id' => $job_id,
'status' => $service->finish($job_id)
];
return $this->withJson($response, $output);
}
public function failed(ServerRequestInterface $request, ResponseInterface $response, Service $service, $job_id): ResponseInterface
{
$output = [
'job_id' => $job_id,
'status' => $service->failed($job_id)
];
return $this->withJson($response, $output);
}

View File

@ -1,11 +1,11 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Common\Exception\Request\MissingArgument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Exception\Request\MissingArgument;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Messages as Service;
use ProVM\Common\Service;
use ProVM\Emails\Model\Message;
use function Safe\json_decode;
@ -13,7 +13,7 @@ class Messages
{
use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $mailbox_id): ResponseInterface
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, int $mailbox_id): ResponseInterface
{
$mailbox = $service->getMailboxes()->get($mailbox_id);
$messages = array_map(function(Message $message) {
@ -37,7 +37,7 @@ class Messages
];
return $this->withJson($response, $output);
}
public function valid(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachments, int $mailbox_id): ResponseInterface
public function valid(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, Service\Attachments $attachments, int $mailbox_id): ResponseInterface
{
$mailbox = $service->getMailboxes()->get($mailbox_id);
$messages = array_values(array_filter(array_map(function(Message $message) use ($service, $attachments) {
@ -63,32 +63,48 @@ class Messages
];
return $this->withJson($response, $output);
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $message_id): ResponseInterface
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, int $message_id): ResponseInterface
{
$message = $service->getRepository()->fetchById($message_id);
return $this->withJson($response, ['message' => $message->toArray()]);
}
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachmentsService): ResponseInterface
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, \ProVM\Common\Service\Attachments $attachmentsService, $mailbox_id): ResponseInterface
{
$output = [
'mailbox_id' => $mailbox_id,
'messages' => [],
'count' => 0
];
$mailbox = $service->getMailboxes()->get($mailbox_id);
$messages = $service->grab($mailbox->getName());
foreach ($messages as $message_id) {
$message = $service->getLocalMessage($message_id);
if ($message->hasValidAttachments()) {
$attachmentsService->create($message->getId());
}
}
$output['messages'] = $messages;
$output['count'] = count($messages);
return $this->withJson($response, $output);
}
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, Service\Jobs $jobsService): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
if (!isset($json->mailboxes)) {
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
$json = json_decode($body);
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'messages IDs');
}
$output = [
'mailboxes' => $json->mailboxes,
'messages' => [],
'message_count' => 0
'messages' => $json->messages,
'scheduled' => 0
];
foreach ($json->mailboxes as $mailbox_name) {
$messages = $service->grab($mailbox_name);
foreach ($messages as $message) {
if ($message->hasValidAttachments()) {
$attachmentsService->create($message);
}
foreach ($json->messages as $message_id) {
if ($jobsService->queue('attachments:grab', [$message_id])) {
$message = $service->getRepository()->fetchById($message_id);
$message->doesHaveScheduledDownloads();
$service->getRepository()->save($message);
$output['scheduled'] ++;
}
$output['messages'] = array_merge($output['messages'], $messages);
$output['message_count'] += count($messages);
}
return $this->withJson($response, $output);
}

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Common\Exception\Request\Auth;
use Exception;
use Throwable;
class Forbidden extends Exception
{
public function __construct(?Throwable $previous = null)
{
$message = 'Forbidden';
$code = 413;
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Common\Exception\Request\Auth;
use Exception;
use Throwable;
class Unauthorized extends Exception
{
public function __construct(?Throwable $previous = null)
{
$message = 'Unauthorized';
$code = 401;
parent::__construct($message, $code, $previous);
}
}

View File

@ -6,19 +6,19 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use ProVM\Common\Exception\Request\Auth\Unauthorized;
use ProVM\Common\Service\Auth as Service;
class Auth
{
public function __construct(ResponseFactoryInterface $factory, LoggerInterface $logger, string $api_key)
public function __construct(ResponseFactoryInterface $factory, LoggerInterface $logger, protected Service $service)
{
$this->setResponseFactory($factory);
$this->setLogger($logger);
$this->setAPIKey($api_key);
}
protected ResponseFactoryInterface $factory;
protected LoggerInterface $logger;
protected string $api_key;
public function getResponseFactory(): ResponseFactoryInterface
{
@ -28,10 +28,6 @@ class Auth
{
return $this->logger;
}
public function getAPIKey(): string
{
return $this->api_key;
}
public function setResponseFactory(ResponseFactoryInterface $factory): Auth
{
@ -43,30 +39,23 @@ class Auth
$this->logger = $logger;
return $this;
}
public function setAPIKey(string $key): Auth
{
$this->api_key = $key;
return $this;
}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($request->getMethod() === 'OPTIONS') {
return $handler->handle($request);
}
$auths = $request->getHeader('Authorization');
foreach ($auths as $auth) {
if (str_contains($auth, 'Bearer')) {
$key = str_replace('Bearer ', '', $auth);
if (sha1($this->getAPIKey()) === $key) {
return $handler->handle($request);
}
try {
if ($this->service->validate($request)) {
return $handler->handle($request);
}
} catch (Unauthorized $e) {
$response = $this->getResponseFactory()->createResponse($e->getCode());
$response->getBody()->write(json_encode(['error' => $e->getCode(), 'message' => $e->getMessage()]));
}
$this->getLogger()->debug(sha1($this->getAPIKey()));
$response = $this->getResponseFactory()->createResponse(401);
$response->getBody()->write(\Safe\json_encode(['error' => 401, 'message' => 'Incorrect token']));
$response = $this->getResponseFactory()->createResponse(413);
$response->getBody()->write(\Safe\json_encode(['error' => 413, 'message' => 'Incorrect token']));
return $response
->withHeader('Content-Type', 'application/json');
}
}
}

View File

@ -1,24 +0,0 @@
<?php
namespace ProVM\Common\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Service;
class Messages
{
public function __construct(protected Service\Messages $service, protected LoggerInterface $logger) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$this->service->checkSchedule();
} catch (BlankResult $e) {
$this->logger->notice($e);
}
return $handler->handle($request);
}
}

View File

@ -117,7 +117,7 @@ class Attachments extends Base
continue;
}
$name = $file->getBasename(".{$file->getExtension()}");
list($subject, $date, $filename) = explode(' - ', $name);
list($date, $subject, $filename) = explode(' - ', $name);
try {
$message = $this->getMessages()->find($subject, $date)[0];
$filename = "{$filename}.{$file->getExtension()}";

View File

@ -0,0 +1,33 @@
<?php
namespace ProVM\Common\Service;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Exception\Request\Auth\Unauthorized;
class Auth
{
public function __construct(protected string $api_key) {}
public function validate(ServerRequestInterface $request): bool
{
$key = $this->getHeaderKey($request);
if (sha1($this->api_key) === $key) {
return true;
}
return false;
}
protected function getHeaderKey(ServerRequestInterface $request): string
{
if (!$request->hasHeader('Authorization')) {
throw new Unauthorized();
}
$auths = $request->getHeader('Authorization');
foreach ($auths as $auth) {
if (str_contains($auth, 'Bearer')) {
return str_replace('Bearer ', '', $auth);
}
}
throw new Unauthorized();
}
}

View File

@ -1,40 +1,49 @@
<?php
namespace ProVM\Common\Service;
use Illuminate\Support\Facades\Date;
use PDOException;
use ProVM\Common\Exception\Database\BlankResult;
use Safe\DateTimeImmutable;
use ProVM\Emails\Repository\Job;
use ProVM\Emails\Repository;
use ProVM\Emails\Model;
class Jobs extends Base
{
public function __construct(Job $repository)
public function __construct(Repository\Job $repository, protected Repository\State\Job $stateRepository)
{
$this->setRepository($repository);
}
protected Job $repository;
protected Repository\Job $repository;
public function getRepository(): Job
public function getRepository(): Repository\Job
{
return $this->repository;
}
public function setRepository(Job $repository): Jobs
public function setRepository(Repository\Job $repository): Jobs
{
$this->repository = $repository;
return $this;
}
public function schedule(int $message_id): bool
public function queue(string $command, ?array $arguments = null): bool
{
$data = [
'message_id' => $message_id,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s')
'command' => $command,
'arguments' => implode(' ', $arguments)
];
try {
$job = $this->getRepository()->create($data);
$this->getRepository()->save($job);
$data = [
'job_id' => $job->getId(),
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
'status' => Model\State\Job::Pending
];
$state = $this->stateRepository->create($data);
$this->stateRepository->save($state);
return true;
} catch (PDOException $e) {
return false;
@ -44,28 +53,42 @@ class Jobs extends Base
{
return $this->getRepository()->fetchAllPending();
}
public function isPending(int $message_id): bool
public function getPendingByCommand(string $command): array
{
try {
$this->getRepository()->fetchPendingByMessage($message_id);
return true;
return $this->getRepository()->fetchAllPendingByCommand($command);
} catch (BlankResult $e) {
return false;
return [];
}
}
public function find(int $message_id): \ProVM\Emails\Model\Job
{
return $this->getRepository()->fetchPendingByMessage($message_id);
}
public function execute(int $job_id): bool
public function finish(int $job_id): bool
{
$data = [
'job_id' => $job_id,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
'status' => Model\State\Job::Executed
];
try {
$job = $this->getRepository()->fetchById($job_id);
$job->wasExecuted();
$this->getRepository()->save($job);
$state = $this->stateRepository->create($data);
$this->stateRepository->save($state);
return true;
} catch (PDOException $e) {
return false;
}
}
}
public function failed(int $job_id): bool
{
$data = [
'job_id' => $job_id,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
'status' => Model\State\Job::Failure
];
try {
$state = $this->stateRepository->create($data);
$this->stateRepository->save($state);
return true;
} catch (PDOException $e) {
return false;
}
}
}

View File

@ -154,7 +154,6 @@ class Messages extends Base
$message->doesHaveValidAttachments();
}
}
error_log(json_encode(compact('message')).PHP_EOL,3,'/logs/debug');
$this->getRepository()->save($message);
return true;
} catch (PDOException $e) {
@ -196,20 +195,7 @@ class Messages extends Base
$registered = $this->getMailboxes()->getRegistered();
foreach ($registered as $mailbox) {
if (!$this->getMailboxes()->isUpdated($mailbox)) {
$this->logger->info("Updating messages from {$mailbox->getName()}");
$this->grab($mailbox->getName());
}
}
}
public function checkSchedule(): void
{
$messages = $this->getRepository()->fetchAll();
foreach ($messages as $message) {
if ($message->hasAttachments() and $message->hasValidAttachments() and !$message->hasDownloadedAttachments() and !$message->hasScheduledDownloads()) {
if ($this->getJobsService()->schedule($message->getId())) {
$message->doesHaveDownloadedAttachments();
$this->getRepository()->save($message);
}
$this->getJobsService()->queue('messages:grab', [$mailbox->getId()]);
}
}
}

View File

@ -7,7 +7,7 @@ services:
- ${API_PATH:-.}:/app/api
- ${API_PATH:-.}/nginx.conf:/etc/nginx/conf.d/api.conf
ports:
- "${API_PORT:-8080}:8080"
- "${API_PORT:-8080}:81"
api:
profiles:
- api

View File

@ -1,15 +1,15 @@
server {
listen 0.0.0.0:8080;
listen 81;
root /app/api/public;
index index.php index.html index.htm;
access_log /var/logs/nginx/access.log;
error_log /var/logs/nginx/error.log;
access_log /var/logs/nginx/api.access.log;
error_log /var/logs/nginx/api.error.log;
location / {
try_files $uri $uri/ /index.php?$query_string;
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
location ~ \.php {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
@ -26,11 +26,12 @@ server {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_pass api:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}

View File

@ -2,9 +2,8 @@
use ProVM\Common\Controller\Attachments;
$app->group('/attachments', function($app) {
$app->put('/grab', [Attachments::class, 'grab']);
$app->get('/pending', [Attachments::class, 'pending']);
$app->post('/decrypt', [Attachments::class, 'decrypt']);
$app->put('/grab[/]', [Attachments::class, 'grab']);
$app->get('/pending[/]', [Attachments::class, 'pending']);
$app->get('[/]', Attachments::class);
});
$app->group('/attachment/{attachment_id}', function($app) {

View File

@ -4,9 +4,9 @@ use ProVM\Common\Controller\Jobs;
$app->group('/messages', function($app) {
$app->put('/grab', [Messages::class, 'grab']);
$app->put('/schedule', [Jobs::class, 'schedule']);
$app->get('/pending', [Jobs::class, 'pending']);
$app->put('/schedule', [Messages::class, 'schedule']);
//$app->get('/pending', [Jobs::class, 'pending']);
});
$app->group('/message/{message_id}', function($app) {
$app->get('[/]', [Messages::class, 'get']);
});
});

View File

@ -0,0 +1,15 @@
<?php
use ProVM\Common\Controller\Jobs;
$app->group('/jobs', function($app) {
$app->group('/pending', function($app) {
$app->put('/command[/]', [Jobs::class, 'pendingCommands']);
$app->get('[/]', [Jobs::class, 'pending']);
});
$app->post('/schedule[/]', [Jobs::class, 'schedule']);
$app->get('[/]', Jobs::class);
});
$app->group('/job/{job_id}', function($app) {
$app->get('/finish[/]', [Jobs::class, 'finish']);
$app->get('/failed[/]', [Jobs::class, 'failed']);
});

View File

@ -1,7 +1,10 @@
<?php
use DI\ContainerBuilder;
use DI\Bridge\Slim\Bridge;
require_once 'composer.php';
$builder = new \DI\ContainerBuilder();
$builder = new ContainerBuilder();
$folders = [
'settings',
@ -24,7 +27,7 @@ foreach ($folders as $f) {
$builder->addDefinitions($file->getRealPath());
}
}
$app = \DI\Bridge\Slim\Bridge::create($builder->build());
$app = Bridge::create($builder->build());
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,

View File

@ -1,5 +1,4 @@
<?php
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Attachments::class));
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Messages::class));
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Mailboxes::class));
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Install::class));

View File

@ -33,5 +33,8 @@ return [
$container->get(ProVM\Common\Factory\Model::class),
$container->get('model_list')
);
},
ProVM\Common\Service\Auth::class => function(ContainerInterface $container) {
return new ProVM\Common\Service\Auth($container->get('api_key'));
}
];

View File

@ -8,9 +8,11 @@ return [
'Mailbox' => ProVM\Emails\Repository\Mailbox::class,
'Message' => ProVM\Emails\Repository\Message::class,
'Attachment' => ProVM\Emails\Repository\Attachment::class,
'Job' => ProVM\Emails\Repository\Job::class,
"State\\Mailbox" => ProVM\Emails\Repository\State\Mailbox::class,
"State\\Message" => ProVM\Emails\Repository\State\Message::class,
"State\\Attachment" => ProVM\Emails\Repository\State\Attachment::class
"State\\Attachment" => ProVM\Emails\Repository\State\Attachment::class,
"State\\Job" => ProVM\Emails\Repository\State\Job::class,
]);
}
];

View File

@ -6,7 +6,7 @@ return [
return new ProVM\Common\Middleware\Auth(
$container->get(Nyholm\Psr7\Factory\Psr17Factory::class),
$container->get(Psr\Log\LoggerInterface::class),
$container->get('api_key')
$container->get(ProVM\Common\Service\Auth::class)
);
}
];

View File

@ -11,8 +11,7 @@ return [
];
},
'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));
return (new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log'])));
},
'request_logger' => function(ContainerInterface $container) {
return new Monolog\Logger(

View File

@ -95,9 +95,9 @@ class Attachment implements Model
public function getFullFilename(): string
{
return implode(' - ', [
$this->getMessage()->getSubject(),
$this->getMessage()->getDateTime()->format('Y-m-d His'),
$this->getFilename()
$this->getMessage()->getSubject(),
$this->getFilename(),
]);
}

View File

@ -25,24 +25,20 @@ class Job implements Model
{
return $this->arguments ?? '';
}
public function isExecuted(): bool
{
return $this->lastState()->getStatus() === State\Job::Executed;
}
public function setId(int $id): Job
{
$this->id = $id;
return $this;
}
public function setCommand(string $message): Job
public function setCommand(string $command): Job
{
$this->message = $message;
$this->command = $command;
return $this;
}
public function setArguments(string $dateTime): Job
public function setArguments(string $arguments): Job
{
$this->dateTime = $dateTime;
$this->arguments = $arguments;
return $this;
}
@ -82,10 +78,14 @@ class Job implements Model
return $this;
}
public function isExecuted(): bool
{
return $this->lastState()->getStatus() === State\Job::Executed;
}
public function lastState(): State\Job
{
if (count($this->getStates()) === 0) {
throw new Stateless($this);
throw new Stateless($this->getId());
}
return $this->getStates()[array_key_last($this->getStates())];
}
@ -94,9 +94,9 @@ class Job implements Model
{
return [
'id' => $this->getId(),
'message' => $this->getMessage()->toArray(),
'date_time' => $this->getDateTime()->format('Y-m-d H:i:s'),
'executed' => $this->isExecuted()
'command' => $this->getCommand(),
'arguments' => $this->getArguments(),
'states' => $this->getStates()
];
}
public function jsonSerialize(): mixed

View File

@ -57,7 +57,7 @@ class Job implements Model
{
return [
'id' => $this->getId(),
'job' => $this->getJob(),
'job_id' => $this->getJob()->getId(),
'date' => $this->getDateTime(),
'status' => $this->getStatus()
];

View File

@ -15,7 +15,7 @@ class Job extends Repository
{
parent::__construct($connection, $logger);
$this->setFactory($factory)
->setTable('attachments_jobs');
->setTable('jobs');
}
protected Factory\Model $factory;
@ -98,19 +98,22 @@ CREATE TABLE {$this->getTable()} (
public function fetchAllPending(): array
{
$query = "SELECT a.*
FROM {$this->getTable()} a
JOIN `jobs_states` b ON b.job_id = a.id
FROM `{$this->getTable()}` a
JOIN (SELECT s1.* FROM `jobs_states` s1 JOIN (SELECT MAX(id) AS id, job_id FROM `jobs_states` GROUP BY job_id) s2 ON s2.id = s1.id) b ON b.`job_id` = a.`id`
WHERE b.`status` = ?";
return $this->fetchMany($query);
return $this->fetchMany($query, [Emails\Model\State\Job::Pending]);
}
public function fetchByCommandAndArguments(string $command, string $arguments): \ProVM\Emails\Model\Job
public function fetchByCommandAndArguments(string $command, string $arguments): Emails\Model\Job
{
$query = "SELECT * FROM {$this->getTable()} WHERE `command` = ? AND `arguments` = ?";
return $this->fetchOne($query, [$command, $arguments]);
}
public function fetchPendingByMessage(int $message_id): \ProVM\Emails\Model\Job
public function fetchAllPendingByCommand(string $command): array
{
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `executed` = 0";
return $this->fetchOne($query, [$message_id]);
$query = "SELECT a.*
FROM `{$this->getTable()}` a
JOIN (SELECT s1.* FROM `jobs_states` s1 JOIN (SELECT MAX(id) AS id, job_id FROM `jobs_states` GROUP BY job_id) s2 ON s2.id = s1.id) b ON b.`job_id` = a.`id`
WHERE a.`command` = ? AND b.`status` = ?";
return $this->fetchMany($query, [$command, Emails\Model\State\Job::Pending]);
}
}

View File

@ -57,7 +57,7 @@ CREATE TABLE {$this->getTable()} (
$query = "SELECT * FROM `{$this->getTable()}` WHERE `job_id` = ?";
return $this->fetchMany($query, [$job_id]);
}
public function fetchByJobAndStatus(int $job_id, int $status): Emails\Model\Job
public function fetchByJobAndStatus(int $job_id, int $status): Emails\Model\State\Job
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `job_id` = ? AND `status` = ?";
return $this->fetchOne($query, [$job_id, $status]);