Skip to content

EspoCRM – Open Source CRM Application

Job preparators ref, use transaction

Yurii546ba4a4bf76
diff --git a/application/Espo/Classes/JobPreparators/CheckEmailAccounts.php b/application/Espo/Classes/JobPreparators/CheckEmailAccounts.php
index 9db05dc253..bf1fd5a804 100644
--- a/application/Espo/Classes/JobPreparators/CheckEmailAccounts.php
+++ b/application/Espo/Classes/JobPreparators/CheckEmailAccounts.php
@@ -38,6 +38,9 @@ use Espo\Core\Job\Preparator\CollectionHelper;
use DateTimeImmutable;
+/**
+ * @noinspection PhpUnused
+ */
class CheckEmailAccounts implements Preparator
{
/**
@@ -45,7 +48,7 @@ class CheckEmailAccounts implements Preparator
*/
public function __construct(
private EntityManager $entityManager,
- private CollectionHelper $helper
+ private CollectionHelper $helper,
) {}
public function prepare(Data $data, DateTimeImmutable $executeTime): void
diff --git a/application/Espo/Classes/JobPreparators/CheckInboundEmails.php b/application/Espo/Classes/JobPreparators/CheckInboundEmails.php
index 6b5497c159..43aa2d95ae 100644
--- a/application/Espo/Classes/JobPreparators/CheckInboundEmails.php
+++ b/application/Espo/Classes/JobPreparators/CheckInboundEmails.php
@@ -37,6 +37,9 @@ use Espo\Core\Job\Preparator\CollectionHelper;
use DateTimeImmutable;
+/**
+ * @noinspection PhpUnused
+ */
class CheckInboundEmails implements Preparator
{
/**
@@ -44,7 +47,7 @@ class CheckInboundEmails implements Preparator
*/
public function __construct(
private EntityManager $entityManager,
- private CollectionHelper $helper
+ private CollectionHelper $helper,
) {}
public function prepare(Data $data, DateTimeImmutable $executeTime): void
diff --git a/application/Espo/Core/Job/Preparator/CollectionHelper.php b/application/Espo/Core/Job/Preparator/CollectionHelper.php
index 13b74e72e5..df6d919180 100644
--- a/application/Espo/Core/Job/Preparator/CollectionHelper.php
+++ b/application/Espo/Core/Job/Preparator/CollectionHelper.php
@@ -29,9 +29,10 @@
namespace Espo\Core\Job\Preparator;
+use Espo\Core\Field\DateTime;
use Espo\Core\Job\Job\Status;
-use Espo\Core\Utils\DateTime;
use Espo\Entities\Job;
+use Espo\Entities\ScheduledJob;
use Espo\ORM\Collection;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
@@ -65,19 +66,32 @@ class CollectionHelper
*/
private function prepareItem(Entity $entity, Data $data, DateTimeImmutable $executeTime): void
{
+ $this->entityManager->getTransactionManager()
+ ->run(fn () => $this->prepareItemInternal($entity, $data, $executeTime));
+ }
+
+ /**
+ * @param TEntity $entity
+ */
+ private function prepareItemInternal(Entity $entity, Data $data, DateTimeImmutable $executeTime): void
+ {
+ $this->entityManager->getRDBRepositoryByClass(ScheduledJob::class)
+ ->select(Attribute::ID)
+ ->forUpdate()
+ ->where([Attribute::ID => $data->getId()])
+ ->findOne();
+
$running = $this->entityManager
->getRDBRepositoryByClass(Job::class)
- // Reduces the chance of race condition.
- ->forUpdate()
->select(Attribute::ID)
->where([
- 'scheduledJobId' => $data->getId(),
Job::FIELD_STATUS => [
Status::RUNNING,
Status::READY,
],
- 'targetType' => $entity->getEntityType(),
- 'targetId' => $entity->getId(),
+ Job::ATTR_SCHEDULED_JOB_ID => $data->getId(),
+ Job::ATTR_TARGET_TYPE => $entity->getEntityType(),
+ Job::ATTR_TARGET_ID => $entity->getId(),
])
->findOne();
@@ -88,10 +102,10 @@ class CollectionHelper
$countPending = $this->entityManager
->getRDBRepositoryByClass(Job::class)
->where([
- 'scheduledJobId' => $data->getId(),
+ Job::ATTR_SCHEDULED_JOB_ID => $data->getId(),
Job::FIELD_STATUS => Status::PENDING,
- 'targetType' => $entity->getEntityType(),
- 'targetId' => $entity->getId(),
+ Job::ATTR_TARGET_TYPE => $entity->getEntityType(),
+ Job::ATTR_TARGET_ID => $entity->getId(),
])
->count();
@@ -99,15 +113,12 @@ class CollectionHelper
return;
}
- $job = $this->entityManager->getRDBRepositoryByClass(Job::class)->getNew();
-
- $job->setMultiple([
- 'name' => $data->getName(),
- 'scheduledJobId' => $data->getId(),
- 'executeTime' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT),
- 'targetType' => $entity->getEntityType(),
- 'targetId' => $entity->getId(),
- ]);
+ $job = $this->entityManager->getRDBRepositoryByClass(Job::class)->getNew()
+ ->setName($data->getName())
+ ->setExecuteTime(DateTime::fromDateTime($executeTime))
+ ->setTargetId($entity->getId())
+ ->setTargetType($entity->getEntityType())
+ ->setScheduleJobId($data->getId());
$this->entityManager->saveEntity($job);
}
diff --git a/application/Espo/Core/Job/Preparator/Preparators/ProcessJobGroupPreparator.php b/application/Espo/Core/Job/Preparator/Preparators/ProcessJobGroupPreparator.php
index fc1602051a..2e0dab125c 100644
--- a/application/Espo/Core/Job/Preparator/Preparators/ProcessJobGroupPreparator.php
+++ b/application/Espo/Core/Job/Preparator/Preparators/ProcessJobGroupPreparator.php
@@ -29,22 +29,26 @@
namespace Espo\Core\Job\Preparator\Preparators;
-use Espo\Core\Utils\DateTime;
+use Espo\Core\Field\DateTime;
+use Espo\Core\Job\Job\Data as JobData;
+use Espo\Core\Utils\DateTime as DateTimeUtil;
use Espo\Core\Job\Job\Status;
use Espo\Core\Job\Preparator;
use Espo\Core\Job\Preparator\Data;
-
+use Espo\Entities\ScheduledJob;
use Espo\ORM\EntityManager;
-
-use Espo\Entities\Job as JobEntity;
-
-use DateTimeImmutable;
+use Espo\Entities\Job;
use Espo\ORM\Name\Attribute;
+use DateTimeImmutable;
+/**
+ * @noinspection PhpUnused
+ */
class ProcessJobGroupPreparator implements Preparator
{
- public function __construct(private EntityManager $entityManager)
- {}
+ public function __construct(
+ private EntityManager $entityManager,
+ ) {}
public function prepare(Data $data, DateTimeImmutable $executeTime): void
{
@@ -52,21 +56,21 @@ class ProcessJobGroupPreparator implements Preparator
$query = $this->entityManager
->getQueryBuilder()
- ->select('group')
- ->from(JobEntity::ENTITY_TYPE)
+ ->select(Job::FIELD_GROUP)
+ ->from(Job::ENTITY_TYPE)
->where([
- 'status' => Status::PENDING,
- 'queue' => null,
- 'group!=' => null,
- 'executeTime<=' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT),
+ Job::FIELD_STATUS => Status::PENDING,
+ Job::FIELD_QUEUE => null,
+ Job::FIELD_GROUP . '!=' => null,
+ Job::FIELD_EXECUTION_TIME . '<=' => $executeTime->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT),
])
- ->group('group')
+ ->group(Job::FIELD_GROUP)
->build();
$sth = $this->entityManager->getQueryExecutor()->execute($query);
while ($row = $sth->fetch()) {
- $group = $row['group'];
+ $group = $row[Job::FIELD_GROUP];
if ($group === null) {
continue;
@@ -80,35 +84,51 @@ class ProcessJobGroupPreparator implements Preparator
}
foreach ($groupList as $group) {
- $existingJob = $this->entityManager
- ->getRDBRepository(JobEntity::ENTITY_TYPE)
- ->select(Attribute::ID)
- ->where([
- 'scheduledJobId' => $data->getId(),
- 'targetGroup' => $group,
- 'status' => [
- Status::RUNNING,
- Status::READY,
- Status::PENDING,
- ],
- ])
- ->findOne();
-
- if ($existingJob) {
- continue;
- }
+ $this->processGroup($group, $data, $executeTime);
+ }
+ }
- $name = $data->getName() . ' :: ' . $group;
+ private function processGroup(string $group, Data $data, DateTimeImmutable $executeTime): void
+ {
+ $this->entityManager->getTransactionManager()
+ ->run(fn () => $this->processGroupInternal($group, $data, $executeTime));
+ }
- $this->entityManager->createEntity(JobEntity::ENTITY_TYPE, [
- 'scheduledJobId' => $data->getId(),
- 'executeTime' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT),
- 'name' => $name,
- 'data' => [
- 'group' => $group,
+ private function processGroupInternal(string $group, Data $data, DateTimeImmutable $executeTime): void
+ {
+ $this->entityManager->getRDBRepositoryByClass(ScheduledJob::class)
+ ->select(Attribute::ID)
+ ->forUpdate()
+ ->where([Attribute::ID => $data->getId()])
+ ->findOne();
+
+ $existingJob = $this->entityManager
+ ->getRDBRepositoryByClass(Job::class)
+ ->select(Attribute::ID)
+ ->where([
+ Job::FIELD_STATUS => [
+ Status::RUNNING,
+ Status::READY,
+ Status::PENDING,
],
- 'targetGroup' => $group,
- ]);
+ Job::ATTR_SCHEDULED_JOB_ID => $data->getId(),
+ Job::FIELD_TARGET_GROUP => $group,
+ ])
+ ->findOne();
+
+ if ($existingJob) {
+ return;
}
+
+ $name = $data->getName() . ' :: ' . $group;
+
+ $job = $this->entityManager->getRDBRepositoryByClass(Job::class)->getNew()
+ ->setName($name)
+ ->setScheduleJobId($data->getId())
+ ->setExecuteTime(DateTime::fromDateTime($executeTime))
+ ->setTargetGroup($group)
+ ->setData(JobData::create(['group' => $group]));
+
+ $this->entityManager->saveEntity($job);
}
}
diff --git a/application/Espo/Entities/Job.php b/application/Espo/Entities/Job.php
index aa553551b5..851821598d 100644
--- a/application/Espo/Entities/Job.php
+++ b/application/Espo/Entities/Job.php
@@ -40,12 +40,25 @@ use stdClass;
class Job extends Entity
{
- public const ENTITY_TYPE = 'Job';
+ public const string ENTITY_TYPE = 'Job';
- /**
- * @since v10.1.0
- */
+ /** @since v10.1.0 */
public const string FIELD_STATUS = 'status';
+ /** @since v10.1.0 */
+ public const string FIELD_TARGET_GROUP = 'targetGroup';
+ /** @since v10.1.0 */
+ public const string FIELD_GROUP = 'group';
+ /** @since v10.1.0 */
+ public const string FIELD_QUEUE = 'queue';
+ /** @since v10.1.0 */
+ public const string FIELD_EXECUTION_TIME = 'executeTime';
+
+ /** @since v10.1.0 */
+ public const string ATTR_SCHEDULED_JOB_ID = 'scheduledJobId';
+ /** @since v10.1.0 */
+ public const string ATTR_TARGET_ID = 'targetId';
+ /** @since v10.1.0 */
+ public const string ATTR_TARGET_TYPE = 'targetType';
/**
* Get a status.
@@ -134,7 +147,7 @@ class Job extends Entity
*/
public function getScheduledJobId(): ?string
{
- return $this->get('scheduledJobId');
+ return $this->get(self::ATTR_SCHEDULED_JOB_ID);
}
/**
@@ -225,12 +238,12 @@ class Job extends Entity
public function setTargetId(?string $targetId): self
{
- return $this->set('targetId', $targetId);
+ return $this->set(self::ATTR_TARGET_ID, $targetId);
}
public function setTargetType(?string $targetType): self
{
- return $this->set('targetType', $targetType);
+ return $this->set(self::ATTR_TARGET_TYPE, $targetType);
}
public function setData(?JobJob\Data $data): self
@@ -244,6 +257,19 @@ class Job extends Entity
public function setExecuteTime(?DateTime $executeTime): self
{
- return $this->setValueObject('executeTime', $executeTime);
+ return $this->setValueObject(self::FIELD_EXECUTION_TIME, $executeTime);
+ }
+
+ /**
+ * @since 10.1.0
+ */
+ public function setScheduleJobId(?string $scheduledJobId): self
+ {
+ return $this->set(self::ATTR_SCHEDULED_JOB_ID, $scheduledJobId);
+ }
+
+ public function setTargetGroup(?string $group): self
+ {
+ return $this->set(self::FIELD_TARGET_GROUP, $group);
}
}
diff --git a/application/Espo/Resources/metadata/entityDefs/Job.json b/application/Espo/Resources/metadata/entityDefs/Job.json
index e06a79d30c..76bdba4222 100644
--- a/application/Espo/Resources/metadata/entityDefs/Job.json
+++ b/application/Espo/Resources/metadata/entityDefs/Job.json
@@ -122,8 +122,13 @@
"status": {
"columns": ["status", "deleted"]
},
- "statusScheduledJobId": {
- "columns": ["status", "scheduledJobId"]
+ "statusScheduledJobIdTarget": {
+ "columns": [
+ "status",
+ "scheduledJobId",
+ "targetType",
+ "targetId"
+ ]
}
},
"hooksDisabled": true