refactoring

This commit is contained in:
Yuri Kuznetsov
2021-06-15 11:51:34 +03:00
parent 612fbf8f38
commit dc5ac7b3a3
4 changed files with 55 additions and 34 deletions
+3 -1
View File
@@ -31,6 +31,8 @@ namespace Espo\Core\Job;
use Espo\Entities\ScheduledJob;
use DateTimeImmutable;
/**
* Can create multiple jobs for different targets according scheduling.
*/
@@ -39,5 +41,5 @@ interface JobPreperable extends Job
/**
* Create multiple job records for a scheduled job.
*/
public function prepare(ScheduledJob $scheduledJob, string $executeTime): void;
public function prepare(ScheduledJob $scheduledJob, DateTimeImmutable $executeTime): void;
}
@@ -44,6 +44,7 @@ use Cron\CronExpression;
use Throwable;
use Exception;
use DateTimeImmutable;
/**
* Creates jobs from scheduled jobs according scheduling.
@@ -151,7 +152,10 @@ class ScheduleProcessor
if ($this->jobFactory->isPreparable($jobName)) {
$jobObj = $this->jobFactory->create($jobName);
$jobObj->prepare($scheduledJob, $executeTime);
$executeAtDt = DateTimeImmutable
::createFromFormat(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT, $executeTime);
$jobObj->prepare($scheduledJob, $executeAtDt);
return;
}
+24 -16
View File
@@ -37,11 +37,13 @@ use Espo\Core\{
Job\Data,
ServiceFactory,
ORM\EntityManager,
Utils\DateTime,
};
use Espo\Entities\ScheduledJob;
use Throwable;
use DateTimeImmutable;
class CheckEmailAccounts implements JobPreperable
{
@@ -68,24 +70,27 @@ class CheckEmailAccounts implements JobPreperable
$entity = $this->entityManager->getEntity('EmailAccount', $targetId);
if (!$entity) {
throw new Error("Job CheckEmailAccounts '".$targetId."': EmailAccount does not exist.", -1);
throw new Error("Job CheckEmailAccounts '{$targetId}': EmailAccount does not exist.", -1);
}
if ($entity->get('status') !== 'Active') {
throw new Error("Job CheckEmailAccounts '".$targetId."': EmailAccount is not active.", -1);
throw new Error("Job CheckEmailAccounts '{$targetId}': EmailAccount is not active.", -1);
}
try {
$service->fetchFromMailServer($entity);
}
catch (Throwable $e) {
throw new Error('Job CheckEmailAccounts '.$entity->id.': [' . $e->getCode() . '] ' .$e->getMessage());
throw new Error(
'Job CheckEmailAccounts ' . $entity->getId() . ': [' . $e->getCode() . '] ' .$e->getMessage()
);
}
}
public function prepare(ScheduledJob $scheduledJob, string $executeTime): void
public function prepare(ScheduledJob $scheduledJob, DateTimeImmutable $executeTime): void
{
$collection = $this->entityManager->getRepository('EmailAccount')
$collection = $this->entityManager
->getRDBRepository('EmailAccount')
->join('assignedUser', 'assignedUserAdditional')
->where([
'status' => 'Active',
@@ -96,12 +101,15 @@ class CheckEmailAccounts implements JobPreperable
foreach ($collection as $entity) {
$running = $this->entityManager
->getRepository('Job')
->getRDBRepository('Job')
->where([
'scheduledJobId' => $scheduledJob->id,
'status' => [JobStatus::RUNNING, JobStatus::READY],
'scheduledJobId' => $scheduledJob->getId(),
'status' => [
JobStatus::RUNNING,
JobStatus::READY,
],
'targetType' => 'EmailAccount',
'targetId' => $entity->id,
'targetId' => $entity->getId(),
])
->findOne();
@@ -110,12 +118,12 @@ class CheckEmailAccounts implements JobPreperable
}
$countPending = $this->entityManager
->getRepository('Job')
->getRDBRepository('Job')
->where([
'scheduledJobId' => $scheduledJob->id,
'scheduledJobId' => $scheduledJob->getId(),
'status' => JobStatus::PENDING,
'targetType' => 'EmailAccount',
'targetId' => $entity->id,
'targetId' => $entity->getId(),
])
->count();
@@ -126,11 +134,11 @@ class CheckEmailAccounts implements JobPreperable
$jobEntity = $this->entityManager->getEntity('Job');
$jobEntity->set([
'name' => $scheduledJob->get('name'),
'scheduledJobId' => $scheduledJob->id,
'executeTime' => $executeTime,
'name' => $scheduledJob->getName(),
'scheduledJobId' => $scheduledJob->getId(),
'executeTime' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT),
'targetType' => 'EmailAccount',
'targetId' => $entity->id,
'targetId' => $entity->getId(),
]);
$this->entityManager->saveEntity($jobEntity);
+23 -16
View File
@@ -37,11 +37,13 @@ use Espo\Core\{
Job\Data,
ServiceFactory,
ORM\EntityManager,
Utils\DateTime,
};
use Espo\Entities\ScheduledJob;
use Throwable;
use DateTimeImmutable;
class CheckInboundEmails implements JobPreperable
{
@@ -68,25 +70,27 @@ class CheckInboundEmails implements JobPreperable
$entity = $this->entityManager->getEntity('InboundEmail', $targetId);
if (!$entity) {
throw new Error("Job CheckInboundEmails '".$targetId."': InboundEmail does not exist.", -1);
throw new Error("Job CheckInboundEmails '{$targetId}': InboundEmail does not exist.", -1);
}
if ($entity->get('status') !== 'Active') {
throw new Error("Job CheckInboundEmails '".$targetId."': InboundEmail is not active.", -1);
throw new Error("Job CheckInboundEmails '{$targetId}': InboundEmail is not active.", -1);
}
try {
$service->fetchFromMailServer($entity);
}
catch (Throwable $e) {
throw new Error('Job CheckInboundEmails '.$entity->id.': [' . $e->getCode() . '] ' .$e->getMessage());
throw new Error(
'Job CheckInboundEmails ' . $entity->getId() . ': [' . $e->getCode() . '] ' .$e->getMessage()
);
}
}
public function prepare(ScheduledJob $scheduledJob, string $executeTime): void
public function prepare(ScheduledJob $scheduledJob, DateTimeImmutable $executeTime): void
{
$collection = $this->entityManager
->getRepository('InboundEmail')
->getRDBRepository('InboundEmail')
->where([
'status' => 'Active',
'useImap' => true,
@@ -95,12 +99,15 @@ class CheckInboundEmails implements JobPreperable
foreach ($collection as $entity) {
$running = $this->entityManager
->getRepository('Job')
->getRDBRepository('Job')
->where([
'scheduledJobId' => $scheduledJob->id,
'status' => [JobStatus::RUNNING, JobStatus::READY],
'scheduledJobId' => $scheduledJob->getId(),
'status' => [
JobStatus::RUNNING,
JobStatus::READY,
],
'targetType' => 'InboundEmail',
'targetId' => $entity->id,
'targetId' => $entity->getId(),
])
->findOne();
@@ -109,12 +116,12 @@ class CheckInboundEmails implements JobPreperable
}
$countPending = $this->entityManager
->getRepository('Job')
->getRDBRepository('Job')
->where([
'scheduledJobId' => $scheduledJob->id,
'scheduledJobId' => $scheduledJob->getId(),
'status' => JobStatus::PENDING,
'targetType' => 'InboundEmail',
'targetId' => $entity->id,
'targetId' => $entity->getId(),
])
->count();
@@ -125,11 +132,11 @@ class CheckInboundEmails implements JobPreperable
$jobEntity = $this->entityManager->getEntity('Job');
$jobEntity->set([
'name' => $scheduledJob->get('name'),
'scheduledJobId' => $scheduledJob->id,
'executeTime' => $executeTime,
'name' => $scheduledJob->getName(),
'scheduledJobId' => $scheduledJob->getId(),
'executeTime' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT),
'targetType' => 'InboundEmail',
'targetId' => $entity->id,
'targetId' => $entity->getId(),
]);
$this->entityManager->saveEntity($jobEntity);