Jobs setup
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
15
api/common/Exception/Request/Auth/Forbidden.php
Normal file
15
api/common/Exception/Request/Auth/Forbidden.php
Normal 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);
|
||||
}
|
||||
}
|
15
api/common/Exception/Request/Auth/Unauthorized.php
Normal file
15
api/common/Exception/Request/Auth/Unauthorized.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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()}";
|
||||
|
33
api/common/Service/Auth.php
Normal file
33
api/common/Service/Auth.php
Normal 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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user