EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Job worker processing (#3723)
Yurii Kuznietsov36b47065a0d6
diff --git a/application/Espo/Binding.php b/application/Espo/Binding.phpindex 105bb0ead9..fed1c8966c 100644--- a/application/Espo/Binding.php+++ b/application/Espo/Binding.php@@ -272,6 +272,20 @@ class Binding implements BindingProcessor'Espo\\Core\\Job\\QueueProcessor\\QueueProcessors\\DefaultQueueProcessor',);});++ $binder->inContext('Espo\\Core\\Job\\Processing\\EnqueueDaemon', function ($binder) {+ $binder->bindImplementation(+ 'Espo\\Core\\Job\\Processing\\Publisher',+ 'Espo\\Core\\Job\\Processing\\RabbitMq\\Publisher',+ );+ });++ $binder->inContext('Espo\\Core\\Job\\Processing\\WorkerDaemon', function ($binder) {+ $binder->bindImplementation(+ 'Espo\\Core\\Job\\Processing\\Consumer',+ 'Espo\\Core\\Job\\Processing\\RabbitMq\\Consumer',+ );+ });}private function bindMisc(Binder $binder): voiddiff --git a/application/Espo/Classes/ConsoleCommands/JobEnqueue.php b/application/Espo/Classes/ConsoleCommands/JobEnqueue.phpnew file mode 100644index 0000000000..822645c91a--- /dev/null+++ b/application/Espo/Classes/ConsoleCommands/JobEnqueue.php@@ -0,0 +1,122 @@+<?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\Classes\ConsoleCommands;++use Espo\Core\Console\Command;+use Espo\Core\Console\Command\Params;+use Espo\Core\Console\Exceptions\InvalidArgument;+use Espo\Core\Console\IO;+use Espo\Core\Job\Processing\EnqueueDaemon;+use Espo\Core\Job\QueueName;+use RuntimeException;++/**+ * @noinspection PhpUnused+ */+class JobEnqueue implements Command+{+ public function __construct(+ private EnqueueDaemon $enqueueDaemon,+ ) {}++ public function run(Params $params, IO $io): void+ {+ $daemonParams = $this->prepareParams($params);++ $this->enqueueDaemon->run($daemonParams);+ }++ private function prepareParams(Params $params): EnqueueDaemon\Params+ {+ $interval = $this->getInterval($params);+ $limit = $this->getLimit($params);+ $portion = $this->getPortion($params);+ $queue = $params->getOption('queue');++ if ($queue === QueueName::M0) {+ throw new InvalidArgument("Queue 'm0' is not allowed. It is processed in the main queue.");+ }++ return new EnqueueDaemon\Params(+ interval: $interval,+ limit: $limit,+ portion: $portion,+ queue: $queue,+ );+ }++ private function getInterval(Params $params): ?float+ {+ $valueString = $params->getOption('interval');++ if ($valueString === null) {+ return null;+ }++ if (+ filter_var($valueString, FILTER_VALIDATE_INT) !== false ||+ filter_var($valueString, FILTER_VALIDATE_FLOAT) !== false+ ) {+ return (float) $valueString;+ }++ throw new RuntimeException("Bad interval.");+ }++ private function getLimit(Params $params): ?int+ {+ $valueString = $params->getOption('limit');++ if ($valueString === null) {+ return null;+ }++ if (filter_var($valueString, FILTER_VALIDATE_INT) !== false) {+ return (int) $valueString;+ }++ throw new RuntimeException("Bad limit.");+ }++ private function getPortion(Params $params): ?int+ {+ $valueString = $params->getOption('portion');++ if ($valueString === null) {+ return null;+ }++ if (filter_var($valueString, FILTER_VALIDATE_INT) !== false) {+ return (int) $valueString;+ }++ throw new RuntimeException("Bad portion.");+ }+}diff --git a/application/Espo/Classes/ConsoleCommands/JobPrepare.php b/application/Espo/Classes/ConsoleCommands/JobPrepare.phpnew file mode 100644index 0000000000..1d1d538d70--- /dev/null+++ b/application/Espo/Classes/ConsoleCommands/JobPrepare.php@@ -0,0 +1,94 @@+<?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\Classes\ConsoleCommands;++use Espo\Core\Console\Command;+use Espo\Core\Console\Command\Params;+use Espo\Core\Console\IO;+use Espo\Core\Job\Processing\PrepareDaemon;+use RuntimeException;++/**+ * @noinspection PhpUnused+ */+class JobPrepare implements Command+{+ public function __construct(+ private PrepareDaemon $prepareDaemon,+ ) {}++ public function run(Params $params, IO $io): void+ {+ $interval = $this->getInterval($params);+ $limit = $this->getLimit($params);+ $skipQueues = $params->hasFlag('sq');++ $daemonParams = new PrepareDaemon\Params(+ interval: $interval,+ limit: $limit,+ skipQueues: $skipQueues,+ );++ $this->prepareDaemon->run($daemonParams);+ }++ private function getInterval(Params $params): ?float+ {+ $intervalString = $params->getOption('interval');++ if ($intervalString === null) {+ return null;+ }++ if (+ filter_var($intervalString, FILTER_VALIDATE_INT) !== false ||+ filter_var($intervalString, FILTER_VALIDATE_FLOAT) !== false+ ) {+ return (float) $intervalString;+ } else {+ throw new RuntimeException("Bad interval.");+ }+ }++ private function getLimit(Params $params): ?int+ {+ $limitString = $params->getOption('limit');++ if ($limitString === null) {+ return null;+ }++ if (filter_var($limitString, FILTER_VALIDATE_INT) !== false) {+ return (int) $limitString;+ } else {+ throw new RuntimeException("Bad limit.");+ }+ }+}diff --git a/application/Espo/Classes/ConsoleCommands/JobWorker.php b/application/Espo/Classes/ConsoleCommands/JobWorker.phpnew file mode 100644index 0000000000..2733b188de--- /dev/null+++ b/application/Espo/Classes/ConsoleCommands/JobWorker.php@@ -0,0 +1,86 @@+<?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\Classes\ConsoleCommands;++use Espo\Core\Console\Command;+use Espo\Core\Console\Command\Params;+use Espo\Core\Console\Exceptions\InvalidArgument;+use Espo\Core\Console\IO;+use Espo\Core\Job\Processing\WorkerDaemon;+use Espo\Core\Job\QueueName;+use RuntimeException;++/**+ * @noinspection PhpUnused+ */+class JobWorker implements Command+{+ public function __construct(+ private WorkerDaemon $workerDaemon,+ ) {}++ public function run(Params $params, IO $io): void+ {+ $daemonParams = $this->prepareParams($params);++ $this->workerDaemon->run($daemonParams);+ }++ private function prepareParams(Params $params): WorkerDaemon\Params+ {+ $limit = $this->getLimit($params);++ $queue = $params->getOption('queue');++ if ($queue === QueueName::M0) {+ throw new InvalidArgument("Queue 'm0' is not allowed. It is processed in the main queue.");+ }++ return new WorkerDaemon\Params(+ limit: $limit,+ queue: $queue,+ );+ }++ private function getLimit(Params $params): ?int+ {+ $limitString = $params->getOption('limit');++ if ($limitString === null) {+ return null;+ }++ if (filter_var($limitString, FILTER_VALIDATE_INT) !== false) {+ return (int) $limitString;+ }++ throw new RuntimeException("Bad limit.");+ }+}diff --git a/application/Espo/Core/ApplicationRunners/Cron.php b/application/Espo/Core/ApplicationRunners/Cron.phpindex 461b70529e..ce855bb6d9 100644--- a/application/Espo/Core/ApplicationRunners/Cron.php+++ b/application/Espo/Core/ApplicationRunners/Cron.php@@ -33,6 +33,7 @@ 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\Job\PrepareProcessor;use Espo\Core\Utils\Config\SystemConfig;use Espo\Core\Utils\Log;@@ -45,9 +46,10 @@ class Cron implements Runneruse SetupSystemUser;public function __construct(+ private PrepareProcessor $prepareProcessor,private JobManager $jobManager,private SystemConfig $config,- private Log $log+ private Log $log,) {}public function run(): void@@ -59,7 +61,7 @@ class Cron implements Runner}try {- $this->jobManager->prepare();+ $this->prepareProcessor->process();} catch (TooFrequentRun $e) {throw new RunnerException('Too frequent run.', previous: $e);}diff --git a/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueE0.php b/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueE0.phpindex ff34db690e..e0cdfba02d 100644--- a/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueE0.php+++ b/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueE0.php@@ -34,4 +34,6 @@ use Espo\Core\Job\QueueName;class ProcessJobQueueE0 extends AbstractQueueJob{protected string $queue = QueueName::E0;++ public const string NAME = 'ProcessJobQueueE0';}diff --git a/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueQ0.php b/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueQ0.phpindex 8bba7bc922..3b059e0549 100644--- a/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueQ0.php+++ b/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueQ0.php@@ -34,4 +34,6 @@ use Espo\Core\Job\QueueName;class ProcessJobQueueQ0 extends AbstractQueueJob{protected string $queue = QueueName::Q0;++ public const string NAME = 'ProcessJobQueueQ0';}diff --git a/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueQ1.php b/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueQ1.phpindex 800dd38528..bdf1d6313c 100644--- a/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueQ1.php+++ b/application/Espo/Core/Job/Job/Jobs/ProcessJobQueueQ1.php@@ -34,4 +34,6 @@ use Espo\Core\Job\QueueName;class ProcessJobQueueQ1 extends AbstractQueueJob{protected string $queue = QueueName::Q1;++ public const string NAME = 'ProcessJobQueueQ1';}diff --git a/application/Espo/Core/Job/JobManager.php b/application/Espo/Core/Job/JobManager.phpindex 73d9eab47b..d6cec71962 100644--- a/application/Espo/Core/Job/JobManager.php+++ b/application/Espo/Core/Job/JobManager.php@@ -29,7 +29,6 @@namespace Espo\Core\Job;-use Espo\Core\Job\Exceptions\TooFrequentRun;use Espo\Core\Job\QueueProcessor\Params;use Espo\Core\Job\QueueProcessor\QueueProcessors\SequentialQueueProcessor;use Espo\Entities\Job as JobEntity;@@ -43,31 +42,11 @@ class JobManager{public function __construct(private JobRunner $jobRunner,- private ScheduleProcessor $scheduleProcessor,- private QueueUtil $queueUtil,private QueueProcessor $queueProcessor,private ConfigDataProvider $configDataProvider,private SequentialQueueProcessor $sequentialQueueProcessor,- private CronUtil $cronUtil,) {}- /**- * 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 prepare(): void- {- if (!$this->cronUtil->checkLastRunTime()) {- throw new TooFrequentRun('JobManager: Skip job processing. Too frequent execution.');- }-- $this->cronUtil->updateLastRunTime();-- $this->processPrepare();- }-/*** Process pending jobs from a specific queue. Jobs within a queue are processed one by one.*/@@ -127,12 +106,4 @@ class JobManager{$this->jobRunner->runThrowingException($job);}-- private function processPrepare(): void- {- $this->queueUtil->markJobsFailed();- $this->queueUtil->updateFailedJobAttempts();- $this->scheduleProcessor->process();- $this->queueUtil->removePendingJobDuplicates();- }}diff --git a/application/Espo/Core/Job/Preparator/CollectionHelper.php b/application/Espo/Core/Job/Preparator/CollectionHelper.phpindex 84587e7c7c..13b74e72e5 100644--- a/application/Espo/Core/Job/Preparator/CollectionHelper.php+++ b/application/Espo/Core/Job/Preparator/CollectionHelper.php@@ -66,11 +66,13 @@ class CollectionHelperprivate function prepareItem(Entity $entity, Data $data, DateTimeImmutable $executeTime): void{$running = $this->entityManager- ->getRDBRepository(Job::ENTITY_TYPE)+ ->getRDBRepositoryByClass(Job::class)+ // Reduces the chance of race condition.+ ->forUpdate()->select(Attribute::ID)->where(['scheduledJobId' => $data->getId(),- 'status' => [+ Job::FIELD_STATUS => [Status::RUNNING,Status::READY,],@@ -84,10 +86,10 @@ class CollectionHelper}$countPending = $this->entityManager- ->getRDBRepository(Job::ENTITY_TYPE)+ ->getRDBRepositoryByClass(Job::class)->where(['scheduledJobId' => $data->getId(),- 'status' => Status::PENDING,+ Job::FIELD_STATUS => Status::PENDING,'targetType' => $entity->getEntityType(),'targetId' => $entity->getId(),])@@ -97,9 +99,9 @@ class CollectionHelperreturn;}- $job = $this->entityManager->getNewEntity(Job::ENTITY_TYPE);+ $job = $this->entityManager->getRDBRepositoryByClass(Job::class)->getNew();- $job->set([+ $job->setMultiple(['name' => $data->getName(),'scheduledJobId' => $data->getId(),'executeTime' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT),diff --git a/application/Espo/Core/Job/PrepareProcessor.php b/application/Espo/Core/Job/PrepareProcessor.phpnew file mode 100644index 0000000000..3f1141163c--- /dev/null+++ b/application/Espo/Core/Job/PrepareProcessor.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;++use Espo\Core\Job\Exceptions\TooFrequentRun;+use Espo\Core\Job\PrepareProcessor\Params;++/**+ * @since 10.1.0+ */+class PrepareProcessor+{+ public function __construct(+ private QueueUtil $queueUtil,+ private ScheduleProcessor $scheduleProcessor,+ private CronUtil $cronUtil,+ ) {}++ /**+ * 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(Params $params = new Params()): void+ {+ if (!$this->cronUtil->checkLastRunTime()) {+ throw new TooFrequentRun('JobManager: Skip job processing. Too frequent execution.');+ }++ $this->cronUtil->updateLastRunTime();++ $this->processPrepare($params);+ }++ private function processPrepare(Params $params): void+ {+ $scheduleParams = new ScheduleProcessor\Params(+ skipQueues: $params->skipQueues,+ );++ $this->queueUtil->markJobsFailed();+ $this->queueUtil->updateFailedJobAttempts();+ $this->scheduleProcessor->process($scheduleParams);+ $this->queueUtil->removePendingJobDuplicates();+ }+}diff --git a/application/Espo/Core/Job/PrepareProcessor/Params.php b/application/Espo/Core/Job/PrepareProcessor/Params.phpnew file mode 100644index 0000000000..fcb0628ee5--- /dev/null+++ b/application/Espo/Core/Job/PrepareProcessor/Params.php@@ -0,0 +1,40 @@+<?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\PrepareProcessor;++/**+ * @since 10.1.0+ */+readonly class Params+{+ public function __construct(+ public bool $skipQueues = false,+ ) {}+}diff --git a/application/Espo/Core/Job/Processing/Consumer.php b/application/Espo/Core/Job/Processing/Consumer.phpnew file mode 100644index 0000000000..6904d1b3ec--- /dev/null+++ b/application/Espo/Core/Job/Processing/Consumer.php@@ -0,0 +1,42 @@+<?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\Processing;++use Espo\Core\Job\Processing\Consumer\Params;++/**+ * @since 10.1.0+ */+interface Consumer+{+ public function start(Params $params): void;++ public function stop(): void;+}diff --git a/application/Espo/Core/Job/Processing/Consumer/Params.php b/application/Espo/Core/Job/Processing/Consumer/Params.phpnew file mode 100644index 0000000000..6da9945fae--- /dev/null+++ b/application/Espo/Core/Job/Processing/Consumer/Params.php@@ -0,0 +1,38 @@+<?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\Processing\Consumer;++readonly class Params+{+ public function __construct(+ public ?int $limit = null,+ public ?string $queue = null,+ ) {}+}diff --git a/application/Espo/Core/Job/Processing/EnqueueDaemon.php b/application/Espo/Core/Job/Processing/EnqueueDaemon.phpnew file mode 100644index 0000000000..4597d3376b--- /dev/null+++ b/application/Espo/Core/Job/Processing/EnqueueDaemon.php@@ -0,0 +1,217 @@+<?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\Processing;++use Espo\Core\Job\ConfigDataProvider;+use Espo\Core\Job\Job\Status;+use Espo\Core\Job\Processing\Util\ExitPolicy;+use Espo\Core\Job\Processing\Util\ExitSetup;+use Espo\Core\Job\QueueName;+use Espo\Core\Job\QueueProcessor\Params;+use Espo\Core\Job\QueueProcessor\Picker;+use Espo\Core\Job\QueueUtil;+use Espo\Core\Utils\Log;+use Espo\Entities\Job;+use Espo\ORM\EntityManager;+use Espo\ORM\Name\Attribute;+use RuntimeException;+use Throwable;++/**+ * @since 10.1.0+ *+ * @internal+ */+class EnqueueDaemon+{+ private const float INTERVAL = 1.0;++ private bool $stopped = false;++ public function __construct(+ private Picker $picker,+ private ConfigDataProvider $configDataProvider,+ private Publisher $publisher,+ private EntityManager $entityManager,+ private QueueUtil $queueUtil,+ private Log $log,+ private ExitSetup $exitSetup,+ private ExitPolicy $exitPolicy,+ ) {}++ public function run(EnqueueDaemon\Params $params): void+ {+ $interval = $this->getInterval($params);+ $limit = $params->limit;++ $pickerParams = $this->getPickerParams($params);+ $publisherParams = $this->preparePublisherParams($params);++ $this->publisher->initialize($publisherParams);++ $this->exitSetup->setup(function () {+ $this->stopped = true;+ });++ $count = 0;++ while (true) {+ $jobs = $this->picker->pick($pickerParams);++ foreach ($jobs as $job) {+ $this->processJob($job);++ $count ++;++ if ($this->toForceExit() || $this->toExit($limit, $count)) {+ break 2;+ }+ }++ usleep($interval);++ if ($this->toForceExit()) {+ break;+ }+ }++ $this->publisher->close();+ }++ private function isStopped(): bool+ {+ return $this->stopped;+ }++ private function getPickerParams(EnqueueDaemon\Params $daemonParams): Params+ {+ $limit = $daemonParams->portion ?? $this->configDataProvider->getMaxPortion();++ $params = Params::create()+ ->withLimit($limit);++ if ($daemonParams->queue !== null) {+ return $params->withQueue($daemonParams->queue);+ }++ return $params->withSubQueueParamsList([+ $params->withWeight(0.5),+ $params->withQueue(QueueName::M0)->withWeight(0.5),+ ]);+ }++ private function getInterval(EnqueueDaemon\Params $params): int+ {+ $interval = $params->interval ?? self::INTERVAL;++ return (int) ($interval * 1000000);+ }++ private function prepareJob(Job $job): void+ {+ $job+ // Needed for failing not started.+ ->setStartedAtNow()+ ->setStatus(Status::READY);++ $this->entityManager->saveEntity($job);+ }++ private function processJob(Job $job): void+ {+ $this->entityManager->getTransactionManager()->run(function () use ($job) {+ $this->processJobInternal($job);+ });+ }++ private function toSkip(Job $job): bool+ {+ if ($job->getStatus() !== Status::PENDING) {+ return true;+ }++ if ($this->queueUtil->isScheduledJobRunning($job)) {+ return true;+ }++ return false;+ }++ private function fetchLocked(Job $job): ?Job+ {+ return $this->entityManager+ ->getRDBRepositoryByClass(Job::class)+ ->forUpdate()+ ->where([Attribute::ID => $job->getId()])+ ->findOne();+ }++ private function processJobInternal(Job $job): void+ {+ $job = $this->fetchLocked($job);++ if (!$job || $this->toSkip($job)) {+ return;+ }++ $this->prepareJob($job);++ try {+ $this->publisher->publish($job);+ } catch (Throwable $e) {+ $this->log->error("Enqueue: Could not publish job {id}.", [+ 'id' => $job->getId(),+ 'exception' => $e,+ ]);++ throw new RuntimeException(previous: $e);+ }+ }++ private function toExit(?int $limit, int $count): bool+ {+ return $limit && $count >= $limit;+ }++ /**+ * @phpstan-impure+ */+ private function toForceExit(): bool+ {+ return $this->isStopped() || $this->exitPolicy->toExit();+ }++ private function preparePublisherParams(EnqueueDaemon\Params $params): Publisher\Params+ {+ return new Publisher\Params(+ queue: $params->queue,+ );+ }+}diff --git a/application/Espo/Core/Job/Processing/EnqueueDaemon/Params.php b/application/Espo/Core/Job/Processing/EnqueueDaemon/Params.phpnew file mode 100644index 0000000000..0c7830009f--- /dev/null+++ b/application/Espo/Core/Job/Processing/EnqueueDaemon/Params.php@@ -0,0 +1,43 @@+<?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\Processing\EnqueueDaemon;++readonly class Params+{+ /**+ * @param ?float $interval Interval in seconds.+ */+ public function __construct(+ public ?float $interval = null,+ public ?int $limit = null,+ public ?int $portion = null,+ public ?string $queue = null,+ ) {}+}diff --git a/application/Espo/Core/Job/Processing/JobProvider.php b/application/Espo/Core/Job/Processing/JobProvider.phpnew file mode 100644index 0000000000..7972843989--- /dev/null+++ b/application/Espo/Core/Job/Processing/JobProvider.php@@ -0,0 +1,65 @@+<?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\Processing;++use Espo\Core\Job\Job\Status;+use Espo\Entities\Job;+use Espo\ORM\EntityManager;+use Espo\ORM\Name\Attribute;+use Exception;++class JobProvider+{+ public function __construct(+ private EntityManager $entityManager,+ ) {}++ /**+ * @throws Exception+ */+ public function get(string $id): Job+ {+ $job = $this->entityManager+ ->getRDBRepositoryByClass(Job::class)+ ->forUpdate()+ ->where([Attribute::ID => $id])+ ->findOne();++ if (!$job) {+ throw new Exception("Job $id not found.");+ }++ if ($job->getStatus() !== Status::READY) {+ throw new Exception("Job $id is not in status Ready.");+ }++ return $job;+ }+}diff --git a/application/Espo/Core/Job/Processing/PrepareDaemon.php b/application/Espo/Core/Job/Processing/PrepareDaemon.phpnew file mode 100644index 0000000000..61b0497a7e--- /dev/null+++ b/application/Espo/Core/Job/Processing/PrepareDaemon.php@@ -0,0 +1,113 @@+<?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\Processing;++use Espo\Core\Job\Exceptions\TooFrequentRun;+use Espo\Core\Job\PrepareProcessor;+use Espo\Core\Job\Processing\Util\ExitPolicy;+use Espo\Core\Job\Processing\Util\ExitSetup;++/**+ * @since 10.1.0+ */+class PrepareDaemon+{+ private const float INTERVAL = 10.0;++ private bool $stopped = false;++ public function __construct(+ private ExitSetup $exitSetup,+ private ExitPolicy $exitPolicy,+ private PrepareProcessor $prepareProcessor,+ ) {}++ public function run(PrepareDaemon\Params $params): void+ {+ $interval = $this->getInterval($params);++ $prepareParams = new PrepareProcessor\Params(+ skipQueues: $params->skipQueues,+ );++ $this->exitSetup->setup(function () {+ $this->stopped = true;+ });++ $count = 0;++ while (true) {+ try {+ $this->prepareProcessor->process($prepareParams);+ } catch (TooFrequentRun) {+ continue;+ }++ $count ++;++ if ($this->toForceExit() || $this->toExit($params, $count)) {+ break;+ }++ usleep($interval);++ if ($this->toForceExit()) {+ break;+ }+ }++ $this->stopped = false;+ }++ private function getInterval(PrepareDaemon\Params $params): int+ {+ $interval = $params->interval ?? self::INTERVAL;++ return (int) ($interval * 1000000);+ }++ private function isStopped(): bool+ {+ return $this->stopped;+ }++ private function toExit(PrepareDaemon\Params $params, int $count): bool+ {+ return $params->limit && $count >= $params->limit;+ }++ /**+ * @phpstan-impure+ */+ private function toForceExit(): bool+ {+ return $this->isStopped() || $this->exitPolicy->toExit();+ }+}diff --git a/application/Espo/Core/Job/Processing/PrepareDaemon/Params.php b/application/Espo/Core/Job/Processing/PrepareDaemon/Params.phpnew file mode 100644index 0000000000..e8b847dce8--- /dev/null+++ b/application/Espo/Core/Job/Processing/PrepareDaemon/Params.php@@ -0,0 +1,42 @@+<?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\Processing\PrepareDaemon;++readonly class Params+{+ /**+ * @param ?float $interval Interval in seconds.+ */+ public function __construct(+ public ?float $interval = null,+ public ?int $limit = null,+ public bool $skipQueues = false,+ ) {}+}diff --git a/application/Espo/Core/Job/Processing/Publisher.php b/application/Espo/Core/Job/Processing/Publisher.phpnew file mode 100644index 0000000000..8d8de6ff5f--- /dev/null+++ b/application/Espo/Core/Job/Processing/Publisher.php@@ -0,0 +1,45 @@+<?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\Processing;++use Espo\Core\Job\Processing\Publisher\Params;+use Espo\Entities\Job;++/**+ * @since 10.1.0+ */+interface Publisher+{+ public function initialize(Params $params): void;++ public function publish(Job $job): void;++ public function close(): void;+}diff --git a/application/Espo/Core/Job/Processing/Publisher/Params.php b/application/Espo/Core/Job/Processing/Publisher/Params.phpnew file mode 100644index 0000000000..8ead955443--- /dev/null+++ b/application/Espo/Core/Job/Processing/Publisher/Params.php@@ -0,0 +1,37 @@+<?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\Processing\Publisher;++readonly class Params+{+ public function __construct(+ public ?string $queue,+ ) {}+}diff --git a/application/Espo/Core/Job/Processing/RabbitMq/ConnectionFactory.php b/application/Espo/Core/Job/Processing/RabbitMq/ConnectionFactory.phpnew file mode 100644index 0000000000..2dd94e472a--- /dev/null+++ b/application/Espo/Core/Job/Processing/RabbitMq/ConnectionFactory.php@@ -0,0 +1,58 @@+<?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\Processing\RabbitMq;++use Espo\Core\Utils\Config;+use Exception;+use PhpAmqpLib\Connection\AMQPStreamConnection;+use RuntimeException;++class ConnectionFactory+{+ public function __construct(+ private Config $config,+ ) {}++ public function create(): AMQPStreamConnection+ {+ $host = $this->config->get('rabbitMq.host');+ $port = $this->config->get('rabbitMq.port');+ $user = $this->config->get('rabbitMq.user');+ $password = $this->config->get('rabbitMq.password');++ try {+ $connection = new AMQPStreamConnection($host, $port, $user, $password);+ } catch (Exception $e) {+ throw new RuntimeException("Connection error.", previous: $e);+ }++ return $connection;+ }+}diff --git a/application/Espo/Core/Job/Processing/RabbitMq/Consumer.php b/application/Espo/Core/Job/Processing/RabbitMq/Consumer.phpnew file mode 100644index 0000000000..c9a2e110a5--- /dev/null+++ b/application/Espo/Core/Job/Processing/RabbitMq/Consumer.php@@ -0,0 +1,198 @@+<?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\Processing\RabbitMq;++use Espo\Core\Job\JobRunner;+use Espo\Core\Job\Processing\JobProvider;+use Espo\Core\Job\Processing\Consumer as ConsumerInterface;+use Espo\Core\Job\Processing\Consumer\Params;+use Espo\Core\Job\Processing\Util\ExitPolicy;+use Espo\Core\Utils\Log;+use Exception;+use PhpAmqpLib\Channel\AMQPChannel;+use PhpAmqpLib\Connection\AMQPStreamConnection;+use PhpAmqpLib\Exception\AMQPTimeoutException;+use PhpAmqpLib\Message\AMQPMessage;+use RuntimeException;+use Throwable;++class Consumer implements ConsumerInterface+{+ private bool $stopped = false;++ private const float ITERATION_TIMEOUT = 1.0;++ public function __construct(+ private ConnectionFactory $connectionFactory,+ private JobRunner $jobRunner,+ private Log $log,+ private JobProvider $jobProvider,+ private ExitPolicy $exitPolicy,+ ) {}++ public function start(Params $params): void+ {+ if ($this->stopped) {+ $this->stopped = false;++ return;+ }++ $queue = Util::composeQueueName($params->queue);++ $connection = $this->connectionFactory->create();+ $channel = $this->prepareChannel($connection, $queue);++ $this->setupConsume($channel, $queue);++ $count = 0;++ while ($channel->is_consuming()) {+ try {+ $channel->wait(timeout: self::ITERATION_TIMEOUT);+ } catch (AMQPTimeoutException) {+ if ($this->toStop($params, $count)) {+ break;+ }++ continue;+ }++ $count ++;++ if ($this->toStop($params, $count)) {+ break;+ }+ }++ $channel->close();+ $this->closeConnection($connection);++ $this->stopped = false;+ }++ public function stop(): void+ {+ $this->stopped = true;+ }++ private function prepareChannel(AMQPStreamConnection $connection, string $queue): AMQPChannel+ {+ $channel = $connection->channel();++ $channel->queue_declare(+ queue: $queue,+ durable: true,+ auto_delete: false,+ );++ $channel->basic_qos(+ prefetch_size: 0,+ prefetch_count: 1,+ a_global: false,+ );++ return $channel;+ }++ private function closeConnection(AMQPStreamConnection $connection): void+ {+ try {+ $connection->close();+ } catch (Exception $e) {+ throw new RuntimeException("Connection closing error.", previous: $e);+ }+ }++ private function getJobId(AMQPMessage $message): string+ {+ $payload = json_decode($message->getBody());++ if (!is_object($payload)) {+ throw new RuntimeException("Bad payload.");+ }++ $id = $payload->id ?? null;++ if (!is_string($id)) {+ throw new RuntimeException("No string ID.");+ }++ return $id;+ }++ private function nack(AMQPMessage $message): void+ {+ $message->nack(false, true);+ }++ private function setupConsume(AMQPChannel $channel, string $queue): void+ {+ $channel->basic_consume(+ queue: $queue,+ callback: function (AMQPMessage $message) {+ try {+ $id = $this->getJobId($message);+ } catch (Throwable $e) {+ $this->log->error("Worker: Could not get job ID.", ['exception' => $e]);++ $this->nack($message);++ return;+ }++ try {+ $job = $this->jobProvider->get($id);++ $this->jobRunner->run($job);+ } catch (Throwable $e) {+ $this->log->error("Worker: Job {id} failed.", [+ 'exception' => $e,+ 'id' => $id,+ ]);++ $this->nack($message);++ return;+ }++ $message->ack();+ },+ );+ }++ private function toStop(Params $params, int $count): bool+ {+ return+ $this->stopped ||+ $params->limit && $count >= $params->limit ||+ $this->exitPolicy->toExit();+ }+}diff --git a/application/Espo/Core/Job/Processing/RabbitMq/Publisher.php b/application/Espo/Core/Job/Processing/RabbitMq/Publisher.phpnew file mode 100644index 0000000000..789b78cca8--- /dev/null+++ b/application/Espo/Core/Job/Processing/RabbitMq/Publisher.php@@ -0,0 +1,117 @@+<?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\Processing\RabbitMq;++use Espo\Core\Job\Processing\Publisher as PublisherInterface;+use Espo\Core\Job\Processing\Publisher\Params;+use Espo\Core\Utils\Json;+use Espo\Entities\Job;+use Espo\ORM\Name\Attribute;+use Exception;+use PhpAmqpLib\Channel\AMQPChannel;+use PhpAmqpLib\Connection\AMQPStreamConnection;+use PhpAmqpLib\Message\AMQPMessage;+use RuntimeException;++class Publisher implements PublisherInterface+{+ public const string QUEUE = 'espo.jobs';++ private ?AMQPStreamConnection $connection = null;+ private ?AMQPChannel $channel = null;+ private string $queue = self::QUEUE;++ public function __construct(+ private ConnectionFactory $connectionFactory,+ ) {}++ public function initialize(Params $params): void+ {+ $this->queue = Util::composeQueueName($params->queue);++ $this->connection = $this->connectionFactory->create();+ $this->channel = $this->prepareChannel($this->connection);+ }++ public function publish(Job $job): void+ {+ if (!$this->channel) {+ throw new RuntimeException("No channel.");+ }++ $message = $this->prepareMessage($job);++ $this->channel->basic_publish(+ msg: $message,+ routing_key: $this->queue,+ );+ }++ public function close(): void+ {+ $this->channel?->close();++ if ($this->connection) {+ $this->closeConnection($this->connection);+ }++ $this->channel = null;+ $this->connection = null;+ }++ private function prepareChannel(AMQPStreamConnection $connection): AMQPChannel+ {+ $channel = $connection->channel();++ $channel->queue_declare(+ queue: $this->queue,+ durable: true,+ auto_delete: false,+ );++ return $channel;+ }++ private function closeConnection(AMQPStreamConnection $connection): void+ {+ try {+ $connection->close();+ } catch (Exception $e) {+ throw new RuntimeException("Connection closing error.", previous: $e);+ }+ }++ private function prepareMessage(Job $job): AMQPMessage+ {+ $payload = Json::encode([Attribute::ID => $job->getId()]);++ return new AMQPMessage($payload, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);+ }+}diff --git a/application/Espo/Core/Job/Processing/RabbitMq/Util.php b/application/Espo/Core/Job/Processing/RabbitMq/Util.phpnew file mode 100644index 0000000000..b54df686fa--- /dev/null+++ b/application/Espo/Core/Job/Processing/RabbitMq/Util.php@@ -0,0 +1,44 @@+<?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\Processing\RabbitMq;++class Util+{+ public static function composeQueueName(?string $queue): string+ {+ $name = Publisher::QUEUE;++ if ($queue !== null) {+ $name .= '.' . $queue;+ }++ return $name;+ }+}diff --git a/application/Espo/Core/Job/Processing/Util/ExitPolicy.php b/application/Espo/Core/Job/Processing/Util/ExitPolicy.phpnew file mode 100644index 0000000000..b656ad153a--- /dev/null+++ b/application/Espo/Core/Job/Processing/Util/ExitPolicy.php@@ -0,0 +1,50 @@+<?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\Processing\Util;++use Espo\Core\Utils\Config;+use Espo\Core\Utils\Config\StateConfig;++class ExitPolicy+{+ private int $cacheTimestamp;++ public function __construct(+ private StateConfig $stateConfig,+ private Config\StateConfigDirect $stateConfigDirect,+ ) {+ $this->cacheTimestamp = $this->stateConfig->getCacheTimestamp();+ }++ public function toExit(): bool+ {+ return $this->cacheTimestamp !== $this->stateConfigDirect->getCacheTimestamp();+ }+}diff --git a/application/Espo/Core/Job/Processing/Util/ExitSetup.php b/application/Espo/Core/Job/Processing/Util/ExitSetup.phpnew file mode 100644index 0000000000..5063d354f1--- /dev/null+++ b/application/Espo/Core/Job/Processing/Util/ExitSetup.php@@ -0,0 +1,46 @@+<?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\Processing\Util;++use Closure;++class ExitSetup+{+ public function setup(Closure $handler): void+ {+ if (!extension_loaded('pcntl')) {+ return;+ }++ pcntl_async_signals(true);+ pcntl_signal(SIGTERM, fn () => $handler());+ pcntl_signal(SIGINT, fn () => $handler());+ }+}diff --git a/application/Espo/Core/Job/Processing/WorkerDaemon.php b/application/Espo/Core/Job/Processing/WorkerDaemon.phpnew file mode 100644index 0000000000..fb3ee3b0d4--- /dev/null+++ b/application/Espo/Core/Job/Processing/WorkerDaemon.php@@ -0,0 +1,67 @@+<?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\Processing;++use Espo\Core\Job\Processing\Consumer\Params;+use Espo\Core\Job\Processing\Util\ExitSetup;++/**+ * @since 10.1.0+ * @internal+ */+class WorkerDaemon+{+ public function __construct(+ private Consumer $consumer,+ private ExitSetup $exitSetup,+ ) {}++ public function run(WorkerDaemon\Params $params): void+ {+ $consumerParams = $this->prepareParams($params);++ $this->setupExit();++ $this->consumer->start($consumerParams);+ }++ private function setupExit(): void+ {+ $this->exitSetup->setup(fn () => $this->consumer->stop());+ }++ private function prepareParams(WorkerDaemon\Params $params): Params+ {+ return new Params(+ limit: $params->limit,+ queue: $params->queue,+ );+ }+}diff --git a/application/Espo/Core/Job/Processing/WorkerDaemon/Params.php b/application/Espo/Core/Job/Processing/WorkerDaemon/Params.phpnew file mode 100644index 0000000000..6a06a30f91--- /dev/null+++ b/application/Espo/Core/Job/Processing/WorkerDaemon/Params.php@@ -0,0 +1,38 @@+<?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\Processing\WorkerDaemon;++readonly class Params+{+ public function __construct(+ public ?int $limit = null,+ public ?string $queue = null,+ ) {}+}diff --git a/application/Espo/Core/Job/QueueName.php b/application/Espo/Core/Job/QueueName.phpindex ca8bc4371b..ec2b36f4e3 100644--- a/application/Espo/Core/Job/QueueName.php+++ b/application/Espo/Core/Job/QueueName.php@@ -32,19 +32,19 @@ namespace Espo\Core\Job;class QueueName{/**- * Executes as soon as possible. Non-parallel.+ * Executes as soon as possible. Non-parallel by default.*/- public const Q0 = 'q0';+ public const string Q0 = 'q0';/**- * Executes every minute. Non-parallel.+ * Executes every minute. Non-parallel by default.*/- public const Q1 = 'q1';+ public const string Q1 = 'q1';/**- * Executes as soon as possible. For email processing. Non-parallel.+ * Executes as soon as possible. For email processing. Non-parallel by default.*/- public const E0 = 'e0';+ public const string E0 = 'e0';/*** Executes in the main queue pool in parallel. Along with jobs without specified queue.@@ -54,5 +54,5 @@ class QueueName** @since 9.2.0*/- const M0 = 'm0';+ const string M0 = 'm0';}diff --git a/application/Espo/Core/Job/QueueProcessor/QueueProcessors/ProcessPoolQueueProcessor.php b/application/Espo/Core/Job/QueueProcessor/QueueProcessors/ProcessPoolQueueProcessor.phpindex 0de8b7454f..0579833688 100644--- a/application/Espo/Core/Job/QueueProcessor/QueueProcessors/ProcessPoolQueueProcessor.php+++ b/application/Espo/Core/Job/QueueProcessor/QueueProcessors/ProcessPoolQueueProcessor.php@@ -108,6 +108,7 @@ class ProcessPoolQueueProcessor implements QueueProcessorprivate function prepareJob(Job $job): void{$job+ // Needed for failing not started.->setStartedAtNow()->setStatus(Status::READY);diff --git a/application/Espo/Core/Job/QueueUtil.php b/application/Espo/Core/Job/QueueUtil.phpindex 0e781e4040..458565f767 100644--- a/application/Espo/Core/Job/QueueUtil.php+++ b/application/Espo/Core/Job/QueueUtil.php@@ -46,7 +46,7 @@ use LogicException;class QueueUtil{private const int NOT_EXISTING_PROCESS_PERIOD = 300;- private const int READY_NOT_STARTED_PERIOD = 60;+ private const int READY_NOT_STARTED_PERIOD = 600;public function __construct(private Config $config,@@ -476,7 +476,7 @@ class QueueUtil])->find();- foreach ($jobCollection as $job) {+ foreach ($jobCollection as $job) {$failedAttempts = $job->getFailedAttempts();$attempts = $job->getAttempts();diff --git a/application/Espo/Core/Job/ScheduleProcessor.php b/application/Espo/Core/Job/ScheduleProcessor.phpindex 403bd2ca7f..e8e79cc665 100644--- a/application/Espo/Core/Job/ScheduleProcessor.php+++ b/application/Espo/Core/Job/ScheduleProcessor.php@@ -31,6 +31,7 @@ namespace Espo\Core\Job;use DateTimeZone;use Espo\Core\Job\Preparator\Data as PreparatorData;+use Espo\Core\Job\ScheduleProcessor\Params;use Espo\Core\ORM\EntityManager;use Espo\Core\Utils\DateTime as DateTimeUtil;use Espo\Core\Utils\Log;@@ -66,15 +67,15 @@ class ScheduleProcessorprivate ScheduleUtil $scheduleUtil,private PreparatorFactory $preparatorFactory,private MetadataProvider $metadataProvider,- private ConfigDataProvider $configDataProvider+ private ConfigDataProvider $configDataProvider,) {}- public function process(): void+ public function process(Params $params = new Params()): void{- $activeScheduledJobList = $this->scheduleUtil->getActiveScheduledJobList();+ $activeScheduledJobs = $this->scheduleUtil->getActiveScheduledJobs($params);$runningScheduledJobIdList = $this->queueUtil->getRunningScheduledJobIdList();- foreach ($activeScheduledJobList as $scheduledJob) {+ foreach ($activeScheduledJobs as $scheduledJob) {try {$isRunning = in_array($scheduledJob->getId(), $runningScheduledJobIdList);diff --git a/application/Espo/Core/Job/ScheduleProcessor/Params.php b/application/Espo/Core/Job/ScheduleProcessor/Params.phpnew file mode 100644index 0000000000..7fcc091490--- /dev/null+++ b/application/Espo/Core/Job/ScheduleProcessor/Params.php@@ -0,0 +1,40 @@+<?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\ScheduleProcessor;++/**+ * @since 10.1.0+ */+readonly class Params+{+ public function __construct(+ public bool $skipQueues = false,+ ) {}+}diff --git a/application/Espo/Core/Job/ScheduleUtil.php b/application/Espo/Core/Job/ScheduleUtil.phpindex 7413fdb533..71ee7632ea 100644--- a/application/Espo/Core/Job/ScheduleUtil.php+++ b/application/Espo/Core/Job/ScheduleUtil.php@@ -29,14 +29,22 @@namespace Espo\Core\Job;+use Espo\Core\Job\Job\Jobs\ProcessJobQueueE0;+use Espo\Core\Job\Job\Jobs\ProcessJobQueueQ0;+use Espo\Core\Job\Job\Jobs\ProcessJobQueueQ1;+use Espo\Core\Job\ScheduleProcessor\Params;+use Espo\Core\Name\Field;use Espo\Core\Utils\DateTime as DateTimeUtil;use Espo\Core\ORM\Repository\Option\SaveOption;use Espo\ORM\Collection;use Espo\ORM\EntityManager;-use Espo\Entities\ScheduledJob as ScheduledJobEntity;+use Espo\Entities\ScheduledJob;use Espo\Entities\ScheduledJobLogRecord as ScheduledJobLogRecordEntity;use Espo\ORM\Name\Attribute;+/**+ * @internal+ */class ScheduleUtil{public function __construct(private EntityManager $entityManager)@@ -45,24 +53,33 @@ class ScheduleUtil/*** Get active scheduled job list.*- * @return Collection<ScheduledJobEntity>+ * @return Collection<ScheduledJob>*/- public function getActiveScheduledJobList(): Collection+ public function getActiveScheduledJobs(Params $params): Collection{- /** @var Collection<ScheduledJobEntity> $collection */$collection = $this->entityManager- ->getRDBRepository(ScheduledJobEntity::ENTITY_TYPE)+ ->getRDBRepositoryByClass(ScheduledJob::class)->select([Attribute::ID,- 'scheduling',- 'job',- 'name',+ ScheduledJob::FIELD_SCHEDULING,+ ScheduledJob::FIELD_JOB,+ Field::NAME,])->where([- 'status' => ScheduledJobEntity::STATUS_ACTIVE,+ ScheduledJob::FIELD_STATUS => ScheduledJob::STATUS_ACTIVE,])->find();+ if ($params->skipQueues) {+ $collection = $collection->filter(function (ScheduledJob $entity) {+ return !in_array($entity->getJob(), [+ ProcessJobQueueE0::NAME,+ ProcessJobQueueQ0::NAME,+ ProcessJobQueueQ1::NAME,+ ]);+ });+ }+return $collection;}@@ -81,8 +98,8 @@ class ScheduleUtil$runTime = date(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);}- /** @var ScheduledJobEntity|null $scheduledJob */- $scheduledJob = $this->entityManager->getEntityById(ScheduledJobEntity::ENTITY_TYPE, $scheduledJobId);+ /** @var ScheduledJob|null $scheduledJob */+ $scheduledJob = $this->entityManager->getEntityById(ScheduledJob::ENTITY_TYPE, $scheduledJobId);if (!$scheduledJob) {return;@@ -94,7 +111,7 @@ class ScheduleUtil$scheduledJobLog = $this->entityManager->getNewEntity(ScheduledJobLogRecordEntity::ENTITY_TYPE);- $scheduledJobLog->set([+ $scheduledJobLog->setMultiple(['scheduledJobId' => $scheduledJobId,'name' => $scheduledJob->getName(),'status' => $status,diff --git a/application/Espo/Core/Utils/Config/StateConfig.php b/application/Espo/Core/Utils/Config/StateConfig.phpnew file mode 100644index 0000000000..373b56ab9f--- /dev/null+++ b/application/Espo/Core/Utils/Config/StateConfig.php@@ -0,0 +1,47 @@+<?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\Utils\Config;++use Espo\Core\Utils\Config;++/**+ * @since 10.1.0+ */+class StateConfig+{+ public function __construct(+ private Config $config,+ ) {}++ public function getCacheTimestamp(): int+ {+ return $this->config->get('cacheTimestamp') ?? 0;+ }+}diff --git a/application/Espo/Core/Utils/Config/StateConfigDirect.php b/application/Espo/Core/Utils/Config/StateConfigDirect.phpnew file mode 100644index 0000000000..b67f51cf1b--- /dev/null+++ b/application/Espo/Core/Utils/Config/StateConfigDirect.php@@ -0,0 +1,52 @@+<?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\Utils\Config;++/**+ * @internal+ * @since 10.1.0+ */+class StateConfigDirect+{+ private string $stateConfigPath = 'data/state.php';++ private const string CACHE_TIMESTAMP_KEY = 'cacheTimestamp';++ public function __construct(+ private ConfigFileManager $configFileManager,+ ) {}++ public function getCacheTimestamp(): int+ {+ $data = $this->configFileManager->getPhpContents($this->stateConfigPath);++ return $data[self::CACHE_TIMESTAMP_KEY] ?? 0;+ }+}diff --git a/application/Espo/Entities/ScheduledJob.php b/application/Espo/Entities/ScheduledJob.phpindex ffd55c1d9e..d2d223df08 100644--- a/application/Espo/Entities/ScheduledJob.php+++ b/application/Espo/Entities/ScheduledJob.php@@ -38,6 +38,15 @@ class ScheduledJob extends Entitypublic const string STATUS_ACTIVE = 'Active';+ /**+ * @since 10.1.0+ */+ public const string FIELD_SCHEDULING = 'scheduling';+ /**+ * @since 10.1.0+ */+ public const string FIELD_STATUS = 'status';+/*** @since 10.0.0*/@@ -50,7 +59,7 @@ class ScheduledJob extends Entitypublic function getScheduling(): ?string{- return $this->get('scheduling');+ return $this->get(self::FIELD_SCHEDULING);}public function getJob(): ?string@@ -63,7 +72,7 @@ class ScheduledJob extends Entity*/public function setActive(): self{- return $this->set('status', self::STATUS_ACTIVE);+ return $this->set(self::FIELD_STATUS, self::STATUS_ACTIVE);}/**@@ -79,7 +88,7 @@ class ScheduledJob extends Entity*/public function setScheduling(string $scheduling): self{- return $this->set('scheduling', $scheduling);+ return $this->set(self::FIELD_SCHEDULING, $scheduling);}/**diff --git a/application/Espo/Resources/defaults/systemConfig.php b/application/Espo/Resources/defaults/systemConfig.phpindex e93d8be953..348b336bcc 100644--- a/application/Espo/Resources/defaults/systemConfig.php+++ b/application/Espo/Resources/defaults/systemConfig.php@@ -120,6 +120,7 @@ return ['passwordRecoveryInternalIntervalPeriod','cleanupAppLog','cleanupAppLogPeriod',+ 'rabbitMq',],'adminItems' => ['devMode',diff --git a/application/Espo/Resources/metadata/app/consoleCommands.json b/application/Espo/Resources/metadata/app/consoleCommands.jsonindex e89648a385..2558ed1e6e 100644--- a/application/Espo/Resources/metadata/app/consoleCommands.json+++ b/application/Espo/Resources/metadata/app/consoleCommands.json@@ -106,5 +106,36 @@"migrationVersionStep": {"listed": false,"noSystemUser": true+ },+ "job:prepare": {+ "className": "Espo\\Classes\\ConsoleCommands\\JobPrepare",+ "listed": false,+ "allowedFlags": [+ "sq"+ ],+ "allowedOptions": [+ "interval",+ "limit"+ ]+ },+ "job:enqueue": {+ "className": "Espo\\Classes\\ConsoleCommands\\JobEnqueue",+ "listed": false,+ "allowedFlags": [],+ "allowedOptions": [+ "interval",+ "limit",+ "portion",+ "queue"+ ]+ },+ "job:worker": {+ "className": "Espo\\Classes\\ConsoleCommands\\JobWorker",+ "listed": false,+ "allowedFlags": [],+ "allowedOptions": [+ "limit",+ "queue"+ ]}}diff --git a/composer.json b/composer.jsonindex 8d0cc76832..dce4186af1 100644--- a/composer.json+++ b/composer.json@@ -56,7 +56,8 @@"directorytree/imapengine": "^1.19","zbateson/mail-mime-parser": "^3.0","guzzlehttp/guzzle": "^7.10",- "devtheorem/php-handlebars": "^1.0"+ "devtheorem/php-handlebars": "^1.0",+ "php-amqplib/php-amqplib": "^3.7"},"require-dev": {"phpunit/phpunit": "^11.5",diff --git a/composer.lock b/composer.lockindex 4ad373ad7e..7a18c8f8e6 100644--- a/composer.lock+++ b/composer.lock@@ -4,7 +4,7 @@"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies","This file is @generated automatically"],- "content-hash": "c4971184fe7b94063ee84e91227012bc",+ "content-hash": "e8dd3b2f9887137751d0a383c4e27ce5","packages": [{"name": "async-aws/core",@@ -3990,6 +3990,87 @@},"time": "2020-10-15T08:29:30+00:00"},+ {+ "name": "php-amqplib/php-amqplib",+ "version": "v3.7.4",+ "source": {+ "type": "git",+ "url": "https://github.com/php-amqplib/php-amqplib.git",+ "reference": "381b6f7c600e0e0c7463cdd7f7a1a3bc6268e5fd"+ },+ "dist": {+ "type": "zip",+ "url": "https://api.github.com/repos/php-amqplib/php-amqplib/zipball/381b6f7c600e0e0c7463cdd7f7a1a3bc6268e5fd",+ "reference": "381b6f7c600e0e0c7463cdd7f7a1a3bc6268e5fd",+ "shasum": ""+ },+ "require": {+ "ext-mbstring": "*",+ "ext-sockets": "*",+ "php": "^7.2||^8.0",+ "phpseclib/phpseclib": "^2.0|^3.0"+ },+ "conflict": {+ "php": "7.4.0 - 7.4.1"+ },+ "replace": {+ "videlalvaro/php-amqplib": "self.version"+ },+ "require-dev": {+ "ext-curl": "*",+ "nategood/httpful": "^0.2.20",+ "phpunit/phpunit": "^7.5|^9.5",+ "squizlabs/php_codesniffer": "^3.6"+ },+ "type": "library",+ "extra": {+ "branch-alias": {+ "dev-master": "3.0-dev"+ }+ },+ "autoload": {+ "psr-4": {+ "PhpAmqpLib\\": "PhpAmqpLib/"+ }+ },+ "notification-url": "https://packagist.org/downloads/",+ "license": [+ "LGPL-2.1-or-later"+ ],+ "authors": [+ {+ "name": "Alvaro Videla",+ "role": "Original Maintainer"+ },+ {+ "name": "Raúl Araya",+ "email": "[email protected]",+ "role": "Maintainer"+ },+ {+ "name": "Luke Bakken",+ "email": "[email protected]",+ "role": "Maintainer"+ },+ {+ "name": "Ramūnas Dronga",+ "email": "[email protected]",+ "role": "Maintainer"+ }+ ],+ "description": "Formerly videlalvaro/php-amqplib. This library is a pure PHP implementation of the AMQP protocol. It's been tested against RabbitMQ.",+ "homepage": "https://github.com/php-amqplib/php-amqplib/",+ "keywords": [+ "message",+ "queue",+ "rabbitmq"+ ],+ "support": {+ "issues": "https://github.com/php-amqplib/php-amqplib/issues",+ "source": "https://github.com/php-amqplib/php-amqplib/tree/v3.7.4"+ },+ "time": "2025-11-23T17:00:56+00:00"+ },{"name": "php-di/invoker","version": "2.3.7",diff --git a/tests/integration/Espo/Core/Job/JobTest.php b/tests/integration/Espo/Core/Job/JobTest.phpindex e320b55fc3..a0f79f6693 100644--- a/tests/integration/Espo/Core/Job/JobTest.php+++ b/tests/integration/Espo/Core/Job/JobTest.php@@ -33,6 +33,7 @@ use Espo\Core\InjectableFactory;use Espo\Core\Job\Job\Status;use Espo\Core\Job\JobManager;use Espo\Core\Job\JobSchedulerFactory;+use Espo\Core\Job\PrepareProcessor;use Espo\Core\Job\QueueName;use Espo\Entities\Job as JobEntity;@@ -56,11 +57,14 @@ class JobTest extends BaseTestCase*/private $schedulerFactory;+ private ?PrepareProcessor $prepareProcessor = null;+protected function setUp(): void{parent::setUp();- $this->jobManager = $this->getContainer()->get('jobManager');+ $this->jobManager = $this->getContainer()->getByClass(JobManager::class);+ $this->prepareProcessor = $this->getInjectableFactory()->create(PrepareProcessor::class);$this->entityManager = $this->getContainer()->getByClass(EntityManager::class);@@ -124,7 +128,7 @@ class JobTest extends BaseTestCase]);- $this->jobManager->prepare();+ $this->prepareProcessor->process();$this->jobManager->processMainQueue();$job1Reloaded = $this->entityManager->getEntityById('Job', $job1->getId());