Files
emails/api/common/Service/Jobs.php

95 lines
2.7 KiB
PHP
Raw Normal View History

2022-11-28 22:56:21 -03:00
<?php
namespace ProVM\Common\Service;
2023-06-12 21:14:07 -04:00
use Illuminate\Support\Facades\Date;
2022-11-28 22:56:21 -03:00
use PDOException;
use ProVM\Common\Exception\Database\BlankResult;
use Safe\DateTimeImmutable;
2023-06-12 21:14:07 -04:00
use ProVM\Emails\Repository;
use ProVM\Emails\Model;
2022-11-28 22:56:21 -03:00
class Jobs extends Base
{
2023-06-12 21:14:07 -04:00
public function __construct(Repository\Job $repository, protected Repository\State\Job $stateRepository)
2022-11-28 22:56:21 -03:00
{
$this->setRepository($repository);
}
2023-06-12 21:14:07 -04:00
protected Repository\Job $repository;
2022-11-28 22:56:21 -03:00
2023-06-12 21:14:07 -04:00
public function getRepository(): Repository\Job
2022-11-28 22:56:21 -03:00
{
return $this->repository;
}
2023-06-12 21:14:07 -04:00
public function setRepository(Repository\Job $repository): Jobs
2022-11-28 22:56:21 -03:00
{
$this->repository = $repository;
return $this;
}
2023-06-12 21:14:07 -04:00
public function queue(string $command, ?array $arguments = null): bool
2022-11-28 22:56:21 -03:00
{
$data = [
2023-06-12 21:14:07 -04:00
'command' => $command,
'arguments' => implode(' ', $arguments)
2022-11-28 22:56:21 -03:00
];
try {
$job = $this->getRepository()->create($data);
$this->getRepository()->save($job);
2023-06-12 21:14:07 -04:00
$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);
2022-11-28 22:56:21 -03:00
return true;
} catch (PDOException $e) {
return false;
}
}
public function getPending(): array
{
return $this->getRepository()->fetchAllPending();
}
2023-06-12 21:14:07 -04:00
public function getPendingByCommand(string $command): array
2022-11-28 22:56:21 -03:00
{
try {
2023-06-12 21:14:07 -04:00
return $this->getRepository()->fetchAllPendingByCommand($command);
2022-11-28 22:56:21 -03:00
} catch (BlankResult $e) {
2023-06-12 21:14:07 -04:00
return [];
2022-11-28 22:56:21 -03:00
}
}
2023-06-12 21:14:07 -04:00
public function finish(int $job_id): bool
2022-11-28 22:56:21 -03:00
{
2023-06-12 21:14:07 -04:00
$data = [
'job_id' => $job_id,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
'status' => Model\State\Job::Executed
];
try {
$state = $this->stateRepository->create($data);
$this->stateRepository->save($state);
return true;
} catch (PDOException $e) {
return false;
}
2022-11-28 22:56:21 -03:00
}
2023-06-12 21:14:07 -04:00
public function failed(int $job_id): bool
2022-11-28 22:56:21 -03:00
{
2023-06-12 21:14:07 -04:00
$data = [
'job_id' => $job_id,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
'status' => Model\State\Job::Failure
];
2022-11-28 22:56:21 -03:00
try {
2023-06-12 21:14:07 -04:00
$state = $this->stateRepository->create($data);
$this->stateRepository->save($state);
2022-11-28 22:56:21 -03:00
return true;
} catch (PDOException $e) {
return false;
}
}
2023-06-12 21:14:07 -04:00
}