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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user