Jobs setup
This commit is contained in:
@ -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