Skip to content

EspoCRM – Open Source CRM Application

Jobs refactoring

Yuriie7cb4e5f6634
diff --git a/application/Espo/Binding.php b/application/Espo/Binding.php
index 7b326b6735..105bb0ead9 100644
--- a/application/Espo/Binding.php
+++ b/application/Espo/Binding.php
@@ -265,6 +265,13 @@ class Binding implements BindingProcessor
'Espo\\Core\\Job\\JobScheduler\\Creator',
'Espo\\Core\\Job\\JobScheduler\\Creators\\EntityCreator',
);
+
+ $binder->inContext('Espo\\Core\\Job\\JobManager', function ($binder) {
+ $binder->bindImplementation(
+ 'Espo\\Core\\Job\\QueueProcessor',
+ 'Espo\\Core\\Job\\QueueProcessor\\QueueProcessors\\DefaultQueueProcessor',
+ );
+ });
}
private function bindMisc(Binder $binder): void
diff --git a/application/Espo/Core/ApplicationRunners/Cron.php b/application/Espo/Core/ApplicationRunners/Cron.php
index c203f8f24e..461b70529e 100644
--- a/application/Espo/Core/ApplicationRunners/Cron.php
+++ b/application/Espo/Core/ApplicationRunners/Cron.php
@@ -29,7 +29,9 @@
namespace Espo\Core\ApplicationRunners;
+use Espo\Core\Application\Exceptions\RunnerException;
use Espo\Core\Application\Runner;
+use Espo\Core\Job\Exceptions\TooFrequentRun;
use Espo\Core\Job\JobManager;
use Espo\Core\Utils\Config\SystemConfig;
use Espo\Core\Utils\Log;
@@ -56,6 +58,12 @@ class Cron implements Runner
return;
}
- $this->jobManager->process();
+ try {
+ $this->jobManager->prepare();
+ } catch (TooFrequentRun $e) {
+ throw new RunnerException('Too frequent run.', previous: $e);
+ }
+
+ $this->jobManager->processMainQueue();
}
}
diff --git a/application/Espo/Core/Job/CronUtil.php b/application/Espo/Core/Job/CronUtil.php
new file mode 100644
index 0000000000..7da33f3574
--- /dev/null
+++ b/application/Espo/Core/Job/CronUtil.php
@@ -0,0 +1,84 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Job;
+
+use Espo\Core\Utils\File\Manager as FileManager;
+use RuntimeException;
+
+/**
+ * @internal
+ */
+class CronUtil
+{
+ protected string $lastRunTimeFile = 'data/cache/application/cronLastRunTime.php';
+
+ public function __construct(
+ private FileManager $fileManager,
+ private ConfigDataProvider $configDataProvider,
+ ) {}
+
+ public function checkLastRunTime(): bool
+ {
+ $currentTime = time();
+ $lastRunTime = $this->getLastRunTime();
+
+ $cronMinInterval = $this->configDataProvider->getCronMinInterval();
+
+ if ($currentTime > ($lastRunTime + $cronMinInterval)) {
+ return true;
+ }
+
+ return false;
+ }
+
+ private function getLastRunTime(): int
+ {
+ if ($this->fileManager->isFile($this->lastRunTimeFile)) {
+ try {
+ $data = $this->fileManager->getPhpContents($this->lastRunTimeFile);
+ } catch (RuntimeException) {
+ $data = null;
+ }
+
+ if (is_array($data) && isset($data['time'])) {
+ return (int) $data['time'];
+ }
+ }
+
+ return time() - $this->configDataProvider->getCronMinInterval() - 1;
+ }
+
+ public function updateLastRunTime(): void
+ {
+ $data = ['time' => time()];
+
+ $this->fileManager->putPhpContents($this->lastRunTimeFile, $data, false, true);
+ }
+}
diff --git a/application/Espo/Core/Job/Exceptions/TooFrequentRun.php b/application/Espo/Core/Job/Exceptions/TooFrequentRun.php
new file mode 100644
index 0000000000..bf10989d49
--- /dev/null
+++ b/application/Espo/Core/Job/Exceptions/TooFrequentRun.php
@@ -0,0 +1,35 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Job\Exceptions;
+
+use Exception;
+
+class TooFrequentRun extends Exception
+{}
diff --git a/application/Espo/Core/Job/JobManager.php b/application/Espo/Core/Job/JobManager.php
index ed9fb64cee..73d9eab47b 100644
--- a/application/Espo/Core/Job/JobManager.php
+++ b/application/Espo/Core/Job/JobManager.php
@@ -29,12 +29,11 @@
namespace Espo\Core\Job;
+use Espo\Core\Job\Exceptions\TooFrequentRun;
use Espo\Core\Job\QueueProcessor\Params;
-use Espo\Core\Utils\File\Manager as FileManager;
-use Espo\Core\Utils\Log;
+use Espo\Core\Job\QueueProcessor\QueueProcessors\SequentialQueueProcessor;
use Espo\Entities\Job as JobEntity;
-use RuntimeException;
use Throwable;
/**
@@ -42,46 +41,31 @@ use Throwable;
*/
class JobManager
{
- private bool $useProcessPool = false;
- protected string $lastRunTimeFile = 'data/cache/application/cronLastRunTime.php';
-
public function __construct(
- private FileManager $fileManager,
private JobRunner $jobRunner,
- private Log $log,
private ScheduleProcessor $scheduleProcessor,
private QueueUtil $queueUtil,
- private AsyncPoolFactory $asyncPoolFactory,
private QueueProcessor $queueProcessor,
- private ConfigDataProvider $configDataProvider
- ) {
- if ($this->configDataProvider->runInParallel()) {
- if ($this->asyncPoolFactory->isSupported()) {
- $this->useProcessPool = true;
- } else {
- $this->log->warning("Enabled `jobRunInParallel` parameter requires pcntl and posix extensions.");
- }
- }
- }
+ private ConfigDataProvider $configDataProvider,
+ private SequentialQueueProcessor $sequentialQueueProcessor,
+ private CronUtil $cronUtil,
+ ) {}
/**
- * Process jobs. Jobs will be created according scheduling. Then pending jobs will be processed.
- * This method supposed to be called on every Cron run or loop iteration of the Daemon.
+ * Jobs are be created according scheduling of scheduled jobs.
+ * This method is meant to be called on every Cron run or loop iteration of the Daemon.
+ *
+ * @throws TooFrequentRun
*/
- public function process(): void
+ public function prepare(): void
{
- if (!$this->checkLastRunTime()) {
- $this->log->info('JobManager: Skip job processing. Too frequent execution.');
-
- return;
+ if (!$this->cronUtil->checkLastRunTime()) {
+ throw new TooFrequentRun('JobManager: Skip job processing. Too frequent execution.');
}
- $this->updateLastRunTime();
- $this->queueUtil->markJobsFailed();
- $this->queueUtil->updateFailedJobAttempts();
- $this->scheduleProcessor->process();
- $this->queueUtil->removePendingJobDuplicates();
- $this->processMainQueue();
+ $this->cronUtil->updateLastRunTime();
+
+ $this->processPrepare();
}
/**
@@ -89,14 +73,11 @@ class JobManager
*/
public function processQueue(string $queue, int $limit): void
{
- $params = Params
- ::create()
+ $params = Params::create()
->withQueue($queue)
- ->withLimit($limit)
- ->withUseProcessPool(false)
- ->withNoLock(true);
+ ->withLimit($limit);
- $this->queueProcessor->process($params);
+ $this->sequentialQueueProcessor->process($params);
}
/**
@@ -104,31 +85,27 @@ class JobManager
*/
public function processGroup(string $group, int $limit): void
{
- $params = Params
- ::create()
+ $params = Params::create()
->withGroup($group)
- ->withLimit($limit)
- ->withUseProcessPool(false)
- ->withNoLock(true);
+ ->withLimit($limit);
- $this->queueProcessor->process($params);
+ $this->sequentialQueueProcessor->process($params);
}
- private function processMainQueue(): void
+ /**
+ * Process the main job queue.
+ */
+ public function processMainQueue(): void
{
$limit = $this->configDataProvider->getMaxPortion();
- $params = Params
- ::create()
- ->withUseProcessPool($this->useProcessPool)
+ $params = Params::create()
->withLimit($limit);
- $subQueueParams = [
+ $params = $params->withSubQueueParamsList([
$params->withWeight(0.5),
$params->withQueue(QueueName::M0)->withWeight(0.5),
- ];
-
- $params = $params->withSubQueueParamsList($subQueueParams);
+ ]);
$this->queueProcessor->process($params);
}
@@ -151,47 +128,11 @@ class JobManager
$this->jobRunner->runThrowingException($job);
}
- /**
- * @todo Move to a separate class.
- */
- private function getLastRunTime(): int
- {
- if ($this->fileManager->isFile($this->lastRunTimeFile)) {
- try {
- $data = $this->fileManager->getPhpContents($this->lastRunTimeFile);
- } catch (RuntimeException) {
- $data = null;
- }
-
- if (is_array($data) && isset($data['time'])) {
- return (int) $data['time'];
- }
- }
-
- return time() - $this->configDataProvider->getCronMinInterval() - 1;
- }
-
- /**
- * @todo Move to a separate class.
- */
- private function updateLastRunTime(): void
+ private function processPrepare(): void
{
- $data = ['time' => time()];
-
- $this->fileManager->putPhpContents($this->lastRunTimeFile, $data, false, true);
- }
-
- private function checkLastRunTime(): bool
- {
- $currentTime = time();
- $lastRunTime = $this->getLastRunTime();
-
- $cronMinInterval = $this->configDataProvider->getCronMinInterval();
-
- if ($currentTime > ($lastRunTime + $cronMinInterval)) {
- return true;
- }
-
- return false;
+ $this->queueUtil->markJobsFailed();
+ $this->queueUtil->updateFailedJobAttempts();
+ $this->scheduleProcessor->process();
+ $this->queueUtil->removePendingJobDuplicates();
}
}
diff --git a/application/Espo/Core/Job/QueueProcessor.php b/application/Espo/Core/Job/QueueProcessor.php
index cd105eb607..07f579ea2e 100644
--- a/application/Espo/Core/Job/QueueProcessor.php
+++ b/application/Espo/Core/Job/QueueProcessor.php
@@ -29,117 +29,12 @@
namespace Espo\Core\Job;
-use Espo\Core\Job\QueueProcessor\Picker;
-use Espo\Entities\Job as JobEntity;
use Espo\Core\Job\QueueProcessor\Params;
-use Espo\Core\ORM\EntityManager;
-use Espo\Core\Utils\System;
-use Espo\Core\Job\Job\Status;
-use Spatie\Async\Pool as AsyncPool;
-
-class QueueProcessor
+/**
+ * @since 10.1.0
+ */
+interface QueueProcessor
{
- private bool $noTableLocking;
-
- public function __construct(
- private QueueUtil $queueUtil,
- private JobRunner $jobRunner,
- private AsyncPoolFactory $asyncPoolFactory,
- private EntityManager $entityManager,
- private Picker $picker,
- ConfigDataProvider $configDataProvider
- ) {
- $this->noTableLocking = $configDataProvider->noTableLocking();
- }
-
- public function process(Params $params): void
- {
- $pool = $params->useProcessPool() ?
- $this->asyncPoolFactory->create() : null;
-
- foreach ($this->picker->pick($params) as $job) {
- $this->processJob($params, $job, $pool);
- }
-
- $pool?->wait();
- }
-
- private function processJob(Params $params, JobEntity $job, ?AsyncPool $pool = null): void
- {
- $noLock = $params->noLock();
- $lockTable = $job->getScheduledJobId() && !$noLock && !$this->noTableLocking;
-
- if ($lockTable) {
- // MySQL doesn't allow to lock non-existent rows. We resort to locking an entire table.
- $this->entityManager->getLocker()->lockExclusive(JobEntity::ENTITY_TYPE);
- }
-
- $skip = $this->toSkip($noLock, $job);
-
- if ($skip) {
- if ($lockTable) {
- $this->entityManager->getLocker()->rollback();
- }
-
- return;
- }
-
- $this->prepareJob($job, $pool);
-
- $this->entityManager->saveEntity($job);
-
- if ($lockTable) {
- $this->entityManager->getLocker()->commit();
- }
-
- $this->runJob($job, $pool);
- }
-
- private function toSkip(bool $noLock, JobEntity $job): bool
- {
- $skip = !$noLock && !$this->queueUtil->isJobPending($job->getId());
-
- if (
- !$skip &&
- $job->getScheduledJobId() &&
- $this->queueUtil->isScheduledJobRunning(
- $job->getScheduledJobId(),
- $job->getTargetId(),
- $job->getTargetType(),
- $job->getTargetGroup()
- )
- ) {
- $skip = true;
- }
-
- return $skip;
- }
-
- private function prepareJob(JobEntity $job, ?AsyncPool $pool): void
- {
- $job->setStartedAtNow();
-
- if ($pool) {
- $job->setStatus(Status::READY);
-
- return;
- }
-
- $job->setStatus(Status::RUNNING);
- $job->setPid(System::getPid());
- }
-
- private function runJob(JobEntity $job, ?AsyncPool $pool): void
- {
- if (!$pool) {
- $this->jobRunner->run($job);
-
- return;
- }
-
- $task = new JobTask($job->getId());
-
- $pool->add($task);
- }
+ public function process(Params $params): void;
}
diff --git a/application/Espo/Core/Job/QueueProcessor/Params.php b/application/Espo/Core/Job/QueueProcessor/Params.php
index a6bc9e05f1..ce025e4fe4 100644
--- a/application/Espo/Core/Job/QueueProcessor/Params.php
+++ b/application/Espo/Core/Job/QueueProcessor/Params.php
@@ -31,8 +31,6 @@ namespace Espo\Core\Job\QueueProcessor;
class Params
{
- private bool $useProcessPool = false;
- private bool $noLock = false;
private ?string $queue = null;
private ?string $group = null;
private int $limit = 0;
@@ -40,22 +38,6 @@ class Params
private ?array $subQueueParamsList = null;
private float $weight = 1.0;
- public function withUseProcessPool(bool $useProcessPool): self
- {
- $obj = clone $this;
- $obj->useProcessPool = $useProcessPool;
-
- return $obj;
- }
-
- public function withNoLock(bool $noLock): self
- {
- $obj = clone $this;
- $obj->noLock = $noLock;
-
- return $obj;
- }
-
public function withQueue(?string $queue): self
{
$obj = clone $this;
@@ -99,16 +81,6 @@ class Params
return $obj;
}
- public function useProcessPool(): bool
- {
- return $this->useProcessPool;
- }
-
- public function noLock(): bool
- {
- return $this->noLock;
- }
-
public function getQueue(): ?string
{
return $this->queue;
diff --git a/application/Espo/Core/Job/QueueProcessor/QueueProcessors/DefaultQueueProcessor.php b/application/Espo/Core/Job/QueueProcessor/QueueProcessors/DefaultQueueProcessor.php
new file mode 100644
index 0000000000..4859d3c523
--- /dev/null
+++ b/application/Espo/Core/Job/QueueProcessor/QueueProcessors/DefaultQueueProcessor.php
@@ -0,0 +1,74 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Job\QueueProcessor\QueueProcessors;
+
+use Espo\Core\Job\AsyncPoolFactory;
+use Espo\Core\Job\ConfigDataProvider;
+use Espo\Core\Job\QueueProcessor;
+use Espo\Core\Utils\Log;
+use Espo\Core\Job\QueueProcessor\Params;
+
+/**
+ * @internal
+ */
+class DefaultQueueProcessor implements QueueProcessor
+{
+ public function __construct(
+ private ProcessPoolQueueProcessor $processPoolQueueProcessor,
+ private SequentialQueueProcessor $sequentialQueueProcessor,
+ private AsyncPoolFactory $asyncPoolFactory,
+ private Log $log,
+ private ConfigDataProvider $configDataProvider,
+ ) {}
+
+ public function process(Params $params): void
+ {
+ if ($this->inParallel()) {
+ $this->processPoolQueueProcessor->process($params);
+ } else {
+ $this->sequentialQueueProcessor->process($params);
+ }
+ }
+
+ private function inParallel(): bool
+ {
+ if (!$this->configDataProvider->runInParallel()) {
+ return false;
+ }
+
+ if (!$this->asyncPoolFactory->isSupported()) {
+ $this->log->warning("Enabled `jobRunInParallel` parameter requires pcntl and posix extensions.");
+
+ return false;
+ }
+
+ return true;
+ }
+}
diff --git a/application/Espo/Core/Job/QueueProcessor/QueueProcessors/ProcessPoolQueueProcessor.php b/application/Espo/Core/Job/QueueProcessor/QueueProcessors/ProcessPoolQueueProcessor.php
new file mode 100644
index 0000000000..0de8b7454f
--- /dev/null
+++ b/application/Espo/Core/Job/QueueProcessor/QueueProcessors/ProcessPoolQueueProcessor.php
@@ -0,0 +1,131 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Job\QueueProcessor\QueueProcessors;
+
+use Espo\Core\Job\AsyncPoolFactory;
+use Espo\Core\Job\ConfigDataProvider;
+use Espo\Core\Job\Job\Status;
+use Espo\Core\Job\JobTask;
+use Espo\Core\Job\QueueProcessor;
+use Espo\Core\Job\QueueProcessor\Params;
+use Espo\Core\Job\QueueProcessor\Picker;
+use Espo\Core\Job\QueueUtil;
+use Espo\Core\ORM\EntityManager;
+use Espo\Entities\Job;
+use Spatie\Async\Pool as AsyncPool;
+
+class ProcessPoolQueueProcessor implements QueueProcessor
+{
+ public function __construct(
+ private QueueUtil $queueUtil,
+ private AsyncPoolFactory $asyncPoolFactory,
+ private EntityManager $entityManager,
+ private Picker $picker,
+ private ConfigDataProvider $configDataProvider,
+ ) {}
+
+ public function process(Params $params): void
+ {
+ $noLock = $this->skipLock($params);
+
+ $pool = $this->asyncPoolFactory->create();
+
+ foreach ($this->picker->pick($params) as $job) {
+ $this->processJob($noLock, $job, $pool);
+ }
+
+ $pool->wait();
+ }
+
+ private function processJob(bool $noLock, Job $job, AsyncPool $pool): void
+ {
+ $lockTable = !$noLock && $job->getScheduledJobId();
+
+ if ($lockTable) {
+ // MySQL doesn't allow to lock non-existent rows. We resort to locking an entire table.
+ $this->entityManager->getLocker()->lockExclusive(Job::ENTITY_TYPE);
+ }
+
+ $skip = $this->toSkip($noLock, $job);
+
+ if ($skip) {
+ if ($lockTable) {
+ $this->entityManager->getLocker()->rollback();
+ }
+
+ return;
+ }
+
+ $this->prepareJob($job);
+
+ if ($lockTable) {
+ $this->entityManager->getLocker()->commit();
+ }
+
+ $this->runJob($job, $pool);
+ }
+
+ private function toSkip(bool $noLock, Job $job): bool
+ {
+ if (!$noLock && !$this->queueUtil->isJobPending($job)) {
+ return true;
+ }
+
+ if ($this->queueUtil->isScheduledJobRunning($job)) {
+ return true;
+ }
+
+ return false;
+ }
+
+ private function prepareJob(Job $job): void
+ {
+ $job
+ ->setStartedAtNow()
+ ->setStatus(Status::READY);
+
+ $this->entityManager->saveEntity($job);
+ }
+
+ private function runJob(Job $job, AsyncPool $pool): void
+ {
+ $task = new JobTask($job->getId());
+
+ $pool->add($task);
+ }
+
+ private function skipLock(Params $params): bool
+ {
+ return
+ $params->getGroup() !== null ||
+ $params->getQueue() !== null ||
+ $this->configDataProvider->noTableLocking();
+ }
+}
diff --git a/application/Espo/Core/Job/QueueProcessor/QueueProcessors/SequentialQueueProcessor.php b/application/Espo/Core/Job/QueueProcessor/QueueProcessors/SequentialQueueProcessor.php
new file mode 100644
index 0000000000..a8507cacd5
--- /dev/null
+++ b/application/Espo/Core/Job/QueueProcessor/QueueProcessors/SequentialQueueProcessor.php
@@ -0,0 +1,83 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Job\QueueProcessor\QueueProcessors;
+
+use Espo\Core\Job\Job\Status;
+use Espo\Core\Job\JobRunner;
+use Espo\Core\Job\QueueProcessor;
+use Espo\Core\Job\QueueProcessor\Params;
+use Espo\Core\Job\QueueProcessor\Picker;
+use Espo\Core\Job\QueueUtil;
+use Espo\Core\ORM\EntityManager;
+use Espo\Core\Utils\System;
+use Espo\Entities\Job;
+
+class SequentialQueueProcessor implements QueueProcessor
+{
+ public function __construct(
+ private QueueUtil $queueUtil,
+ private JobRunner $jobRunner,
+ private EntityManager $entityManager,
+ private Picker $picker,
+ ) {}
+
+ public function process(Params $params): void
+ {
+ foreach ($this->picker->pick($params) as $job) {
+ $this->processJob($job);
+ }
+ }
+
+ private function processJob(Job $job): void
+ {
+ if ($this->toSkip($job)) {
+ return;
+ }
+
+ $this->prepareJob($job);
+
+ $this->jobRunner->run($job);
+ }
+
+ private function toSkip(Job $job): bool
+ {
+ return $this->queueUtil->isScheduledJobRunning($job);
+ }
+
+ private function prepareJob(Job $job): void
+ {
+ $job
+ ->setStartedAtNow()
+ ->setStatus(Status::RUNNING)
+ ->setPid(System::getPid());
+
+ $this->entityManager->saveEntity($job);
+ }
+}
diff --git a/application/Espo/Core/Job/QueueUtil.php b/application/Espo/Core/Job/QueueUtil.php
index 88d9056cc5..0e781e4040 100644
--- a/application/Espo/Core/Job/QueueUtil.php
+++ b/application/Espo/Core/Job/QueueUtil.php
@@ -37,32 +37,30 @@ use Espo\Core\Utils\DateTime as DateTimeUtil;
use Espo\Core\Utils\System;
use Espo\Core\Job\Job\Status;
use Espo\Entities\Job as JobEntity;
-
-use DateTime;
use Espo\ORM\Collection;
use Espo\ORM\Name\Attribute;
+use DateTime;
use Exception;
use LogicException;
class QueueUtil
{
- private const NOT_EXISTING_PROCESS_PERIOD = 300;
- private const READY_NOT_STARTED_PERIOD = 60;
+ private const int NOT_EXISTING_PROCESS_PERIOD = 300;
+ private const int READY_NOT_STARTED_PERIOD = 60;
public function __construct(
private Config $config,
private EntityManager $entityManager,
private ScheduleUtil $scheduleUtil,
- private MetadataProvider $metadataProvider
+ private MetadataProvider $metadataProvider,
) {}
- public function isJobPending(string $id): bool
+ public function isJobPending(JobEntity $job): bool
{
- /** @var ?JobEntity $job */
$job = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
- ->select([Attribute::ID, 'status'])
- ->where([Attribute::ID => $id])
+ ->select([Attribute::ID, JobEntity::FIELD_STATUS])
+ ->where([Attribute::ID => $job->getId()])
->forUpdate()
->findOne();
@@ -111,16 +109,20 @@ class QueueUtil
return $builder->sth()->find();
}
- public function isScheduledJobRunning(
- string $scheduledJobId,
- ?string $targetId = null,
- ?string $targetType = null,
- ?string $targetGroup = null
- ): bool {
+ public function isScheduledJobRunning(JobEntity $job): bool
+ {
+ $scheduledJobId = $job->getScheduledJobId();
+ $targetId = $job->getTargetId();
+ $targetType = $job->getTargetType();
+ $targetGroup = $job->getTargetGroup();
+
+ if (!$scheduledJobId) {
+ return false;
+ }
$where = [
'scheduledJobId' => $scheduledJobId,
- 'status' => [
+ JobEntity::FIELD_STATUS => [
Status::RUNNING,
Status::READY,
],
diff --git a/application/Espo/Entities/Job.php b/application/Espo/Entities/Job.php
index 27047295f3..aa553551b5 100644
--- a/application/Espo/Entities/Job.php
+++ b/application/Espo/Entities/Job.php
@@ -42,12 +42,17 @@ class Job extends Entity
{
public const ENTITY_TYPE = 'Job';
+ /**
+ * @since v10.1.0
+ */
+ public const string FIELD_STATUS = 'status';
+
/**
* Get a status.
*/
public function getStatus(): string
{
- return $this->get('status');
+ return $this->get(self::FIELD_STATUS);
}
/**
@@ -171,7 +176,7 @@ class Job extends Entity
*/
public function setStatus(string $status): self
{
- return $this->set('status', $status);
+ return $this->set(self::FIELD_STATUS, $status);
}
/**
diff --git a/tests/integration/Espo/Core/Job/JobTest.php b/tests/integration/Espo/Core/Job/JobTest.php
index 1dd5394b66..114372a95d 100644
--- a/tests/integration/Espo/Core/Job/JobTest.php
+++ b/tests/integration/Espo/Core/Job/JobTest.php
@@ -120,7 +120,7 @@ class JobTest extends BaseTestCase
'group' => 'group-1',
]);
- $this->jobManager->process();
+ $this->jobManager->processMainQueue();
$job1Reloaded = $this->entityManager->getEntityById('Job', $job1->getId());
$job2Reloaded = $this->entityManager->getEntityById('Job', $job2->getId());
diff --git a/tests/unit/Espo/Core/Job/QueueProcessorParamsTest.php b/tests/unit/Espo/Core/Job/QueueProcessorParamsTest.php
index d0adac1bd1..12e83d3e40 100644
--- a/tests/unit/Espo/Core/Job/QueueProcessorParamsTest.php
+++ b/tests/unit/Espo/Core/Job/QueueProcessorParamsTest.php
@@ -29,11 +29,10 @@
namespace tests\unit\Espo\Core\Job;
-use Espo\Core\{
- Job\QueueProcessor\Params,
-};
+use Espo\Core\Job\QueueProcessor\Params;
+use PHPUnit\Framework\TestCase;
-class QueueProcessorParamsTest extends \PHPUnit\Framework\TestCase
+class QueueProcessorParamsTest extends TestCase
{
protected function setUp() : void
{
@@ -41,33 +40,23 @@ class QueueProcessorParamsTest extends \PHPUnit\Framework\TestCase
public function testParams1()
{
- $params = \Espo\Core\Job\QueueProcessor\Params
+ $params = Params
::create()
->withLimit(10);
- $this->assertFalse($params->useProcessPool());
- $this->assertFalse($params->noLock());
-
$this->assertEquals(10, $params->getLimit());
-
$this->assertNull($params->getQueue());
}
public function testParams2()
{
- $params = \Espo\Core\Job\QueueProcessor\Params
+ $params = Params
::create()
->withLimit(10)
- ->withUseProcessPool(true)
- ->withNoLock(true)
->withGroup('group-0')
->withQueue('q0');
- $this->assertTrue($params->useProcessPool());
- $this->assertTrue($params->noLock());
-
$this->assertEquals('q0', $params->getQueue());
-
$this->assertEquals('group-0', $params->getGroup());
}
}