Jobs setup
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user