snapshot of https://github.com/espocrm/espocrm.git (master, 2026-09-18; blobs > 131072B omitted)
Some checks failed
Test / Test on PHP 8.3 (push) Failing after 5s
Test / Test on PHP 8.4 (push) Failing after 4s
Test / Test on PHP 8.5 (push) Failing after 3s
TypeScript static analysis / TypeScript check (push) Failing after 14s
Test Integration on MySQL / Test on PHP 8.3 (push) Failing after 2s
Test Integration on MySQL / Test on PHP 8.5 (push) Failing after 0s
Test Integration on PostgreSQL / Test on PHP 8.4 fix (push) Failing after 1s
Test Integration on PostgreSQL / Test on PHP 8.4 master (push) Failing after 1s

This commit is contained in:
upstream-mirror
2026-09-18 04:19:15 +05:00
commit 7cc7e6b4f3
10077 changed files with 900756 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<?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\Config;
use Spatie\Async\Pool;
class AsyncPoolFactory
{
public function __construct(private Config $config)
{}
public function isSupported(): bool
{
return Pool::isSupported();
}
public function create(): Pool
{
return Pool
::create()
->autoload(getcwd() . '/vendor/autoload.php')
->concurrency($this->config->get('jobPoolConcurrencyNumber'))
->timeout($this->config->get('jobPeriodForActiveProcess'));
}
}

View File

@@ -0,0 +1,71 @@
<?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\Config;
class ConfigDataProvider
{
private const MAX_PORTION = 15;
public function __construct(
private Config $config,
private Config\ApplicationConfig $applicationConfig,
) {}
public function runInParallel(): bool
{
return (bool) $this->config->get('jobRunInParallel');
}
public function getMaxPortion(): int
{
return (int) $this->config->get('jobMaxPortion', self::MAX_PORTION);
}
public function getCronMinInterval(): int
{
return (int) $this->config->get('cronMinInterval', 0);
}
public function noTableLocking(): bool
{
return (bool) $this->config->get('jobNoTableLocking');
}
public function getTimeZone(): string
{
if ($this->config->get('jobForceUtc')) {
return 'UTC';
}
return $this->applicationConfig->getTimeZone();
}
}

View File

@@ -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);
}
}

View File

@@ -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
{}

View File

@@ -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;
use Espo\Core\Job\Job\Data;
/**
* A job.
*/
interface Job
{
/**
* Run a job.
*/
public function run(Data $data): void;
}

View File

@@ -0,0 +1,114 @@
<?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\Job;
use Espo\Core\Utils\ObjectUtil;
use TypeError;
use stdClass;
class Data
{
private stdClass $data;
private ?string $targetId = null;
private ?string $targetType = null;
public function __construct(?stdClass $data = null)
{
$this->data = $data ?? (object) [];
}
/**
* Create an instance.
*
* @param stdClass|array<string, mixed>|null $data Raw data.
* @return self
*/
public static function create($data = null): self
{
/** @var mixed $data */
if ($data !== null && !is_object($data) && !is_array($data)) {
throw new TypeError();
}
if (is_array($data)) {
$data = (object) $data;
}
/** @var ?stdClass $data */
return new self($data);
}
public function getRaw(): stdClass
{
return ObjectUtil::clone($this->data);
}
/**
* @return mixed
*/
public function get(string $name)
{
return $this->getRaw()->$name ?? null;
}
public function has(string $name): bool
{
return property_exists($this->data, $name);
}
public function getTargetId(): ?string
{
return $this->targetId;
}
public function getTargetType(): ?string
{
return $this->targetType;
}
public function withTargetId(?string $targetId): self
{
$obj = clone $this;
$obj->targetId = $targetId;
return $obj;
}
public function withTargetType(?string $targetType): self
{
$obj = clone $this;
$obj->targetType = $targetType;
return $obj;
}
}

View File

@@ -0,0 +1,51 @@
<?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\Job\Jobs;
use Espo\Core\Job\JobDataLess;
use Espo\Core\Job\JobManager;
use Espo\Core\Job\QueuePortionNumberProvider;
abstract class AbstractQueueJob implements JobDataLess
{
protected string $queue;
public function __construct(
private JobManager $jobManager,
private QueuePortionNumberProvider $portionNumberProvider)
{}
public function run(): void
{
$limit = $this->portionNumberProvider->get($this->queue);
$this->jobManager->processQueue($this->queue, $limit);
}
}

View File

@@ -0,0 +1,54 @@
<?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\Job\Jobs;
use Espo\Core\Utils\Config;
use Espo\Core\Job\Job;
use Espo\Core\Job\Job\Data;
use Espo\Core\Job\JobManager;
class ProcessJobGroup implements Job
{
private const PORTION_NUMBER = 100;
public function __construct(
private JobManager $jobManager,
private Config $config
) {}
public function run(Data $data): void
{
$limit = $this->config->get('jobGroupMaxPortion') ?? self::PORTION_NUMBER;
$group = $data->get('group');
$this->jobManager->processGroup($group, $limit);
}
}

View File

@@ -0,0 +1,39 @@
<?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\Job\Jobs;
use Espo\Core\Job\QueueName;
class ProcessJobQueueE0 extends AbstractQueueJob
{
protected string $queue = QueueName::E0;
public const string NAME = 'ProcessJobQueueE0';
}

View File

@@ -0,0 +1,39 @@
<?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\Job\Jobs;
use Espo\Core\Job\QueueName;
class ProcessJobQueueQ0 extends AbstractQueueJob
{
protected string $queue = QueueName::Q0;
public const string NAME = 'ProcessJobQueueQ0';
}

View File

@@ -0,0 +1,39 @@
<?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\Job\Jobs;
use Espo\Core\Job\QueueName;
class ProcessJobQueueQ1 extends AbstractQueueJob
{
protected string $queue = QueueName::Q1;
public const string NAME = 'ProcessJobQueueQ1';
}

View File

@@ -0,0 +1,39 @@
<?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\Job;
class Status
{
public const PENDING = 'Pending';
public const READY = 'Ready';
public const RUNNING = 'Running';
public const SUCCESS = 'Success';
public const FAILED = 'Failed';
}

View File

@@ -0,0 +1,41 @@
<?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;
/**
* A job w/o data.
*/
interface JobDataLess
{
/**
* Run a job.
*/
public function run(): void;
}

View File

@@ -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;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\ClassFinder;
use RuntimeException;
class JobFactory
{
public function __construct(
private ClassFinder $classFinder,
private InjectableFactory $injectableFactory,
private MetadataProvider $metadataProvider,
) {}
/**
* Create a job by a scheduled job name.
*/
public function create(string $name): Job|JobDataLess
{
$className = $this->getClassName($name);
if (!$className) {
throw new RuntimeException("Job '$name' not found.");
}
return $this->createByClassName($className);
}
/**
* Create a job by a class name.
*
* @param class-string<Job|JobDataLess> $className
*/
public function createByClassName(string $className): Job|JobDataLess
{
return $this->injectableFactory->create($className);
}
/**
* @return ?class-string<Job|JobDataLess>
*/
private function getClassName(string $name): ?string
{
/** @var ?class-string<Job|JobDataLess> $className */
$className = $this->metadataProvider->getJobClassName($name);
if ($className) {
return $className;
}
/** @var ?class-string<Job|JobDataLess> */
return $this->classFinder->find('Jobs', ucfirst($name));
}
}

View File

@@ -0,0 +1,109 @@
<?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\QueueProcessor\Params;
use Espo\Core\Job\QueueProcessor\QueueProcessors\SequentialQueueProcessor;
use Espo\Entities\Job as JobEntity;
use Throwable;
/**
* Handles processing jobs.
*/
class JobManager
{
public function __construct(
private JobRunner $jobRunner,
private QueueProcessor $queueProcessor,
private ConfigDataProvider $configDataProvider,
private SequentialQueueProcessor $sequentialQueueProcessor,
) {}
/**
* Process pending jobs from a specific queue. Jobs within a queue are processed one by one.
*/
public function processQueue(string $queue, int $limit): void
{
$params = Params::create()
->withQueue($queue)
->withLimit($limit);
$this->sequentialQueueProcessor->process($params);
}
/**
* Process pending jobs from a specific group. Jobs within a group are processed one by one.
*/
public function processGroup(string $group, int $limit): void
{
$params = Params::create()
->withGroup($group)
->withLimit($limit);
$this->sequentialQueueProcessor->process($params);
}
/**
* Process the main job queue.
*/
public function processMainQueue(): void
{
$limit = $this->configDataProvider->getMaxPortion();
$params = Params::create()
->withLimit($limit);
$params = $params->withSubQueueParamsList([
$params->withWeight(0.5),
$params->withQueue(QueueName::M0)->withWeight(0.5),
]);
$this->queueProcessor->process($params);
}
/**
* Run a specific job by ID. A job status should be set to 'Ready'.
*/
public function runJobById(string $id): void
{
$this->jobRunner->runById($id);
}
/**
* Run a specific job.
*
* @throws Throwable
*/
public function runJob(JobEntity $job): void
{
$this->jobRunner->runThrowingException($job);
}
}

View File

@@ -0,0 +1,226 @@
<?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\ORM\EntityManager;
use Espo\Core\Utils\Log;
use Espo\Core\Utils\System;
use Espo\Core\Job\Job\Data;
use Espo\Core\Job\Job\Status;
use Espo\Entities\Job as JobEntity;
use Espo\ORM\Name\Attribute;
use LogicException;
use RuntimeException;
use Throwable;
class JobRunner
{
public function __construct(
private JobFactory $jobFactory,
private ScheduleUtil $scheduleUtil,
private EntityManager $entityManager,
private Log $log,
) {}
/**
* Run a job entity. Does not throw exceptions.
*/
public function run(JobEntity $jobEntity): void
{
try {
$this->runInternal($jobEntity);
} catch (Throwable $e) {
throw new LogicException($e->getMessage());
}
}
/**
* Run a job entity. Throws exceptions.
*
* @throws Throwable
*/
public function runThrowingException(JobEntity $jobEntity): void
{
$this->runInternal($jobEntity, true);
}
/**
* Run a job by ID. A job must have status 'Ready'.
* Used when running jobs in parallel processes.
*/
public function runById(string $id): void
{
if ($id === '') {
throw new RuntimeException("Empty job ID.");
}
$jobEntity = $this->entityManager->getRDBRepositoryByClass(JobEntity::class)->getById($id);
if (!$jobEntity) {
throw new RuntimeException("Job '$id' not found.");
}
if ($jobEntity->getStatus() !== Status::READY) {
throw new RuntimeException("Can't run job '$id' with not Ready status.");
}
$this->setJobRunning($jobEntity);
$this->run($jobEntity);
}
/**
* @throws Throwable
*/
private function runInternal(JobEntity $jobEntity, bool $throwException = false): void
{
$isSuccess = true;
$exception = null;
if ($jobEntity->getStatus() !== Status::RUNNING) {
$this->setJobRunning($jobEntity);
}
try {
if ($jobEntity->getScheduledJobId()) {
$this->runScheduledJob($jobEntity);
} else if ($jobEntity->getJob()) {
$this->runJobNamed($jobEntity);
} else if ($jobEntity->getClassName()) {
$this->runJobWithClassName($jobEntity);
} else {
$id = $jobEntity->getId();
throw new RuntimeException("Not runnable job '$id'.");
}
} catch (Throwable $e) {
$isSuccess = false;
$jobId = $jobEntity->hasId() ? $jobEntity->getId() : null;
$this->log->critical("Failed job {id}.", [
'exception' => $e,
Attribute::ID => $jobId,
]);
if ($throwException) {
$exception = $e;
}
}
$status = $isSuccess ? Status::SUCCESS : Status::FAILED;
$jobEntity->setStatus($status);
if ($isSuccess) {
$jobEntity->setExecutedAtNow();
}
$this->entityManager->saveEntity($jobEntity);
if ($throwException && $exception) {
throw new $exception($exception->getMessage());
}
if ($jobEntity->getScheduledJobId()) {
$this->scheduleUtil->addLogRecord(
scheduledJobId: $jobEntity->getScheduledJobId(),
status: $status,
targetId: $jobEntity->getTargetId(),
targetType: $jobEntity->getTargetType(),
);
}
}
private function runJobNamed(JobEntity $jobEntity): void
{
$jobName = $jobEntity->getJob();
if (!$jobName) {
throw new RuntimeException("No job name.");
}
$job = $this->jobFactory->create($jobName);
$this->runJob($job, $jobEntity);
}
private function runScheduledJob(JobEntity $jobEntity): void
{
$jobName = $jobEntity->getScheduledJobJob();
if (!$jobName) {
throw new RuntimeException("Can't run job '{$jobEntity->getId()}'. Not a scheduled job.");
}
$job = $this->jobFactory->create($jobName);
$this->runJob($job, $jobEntity);
}
private function runJobWithClassName(JobEntity $jobEntity): void
{
$className = $jobEntity->getClassName();
if (!$className) {
throw new RuntimeException("No className in job {$jobEntity->getId()}.");
}
$job = $this->jobFactory->createByClassName($className);
$this->runJob($job, $jobEntity);
}
private function runJob(Job|JobDataLess $job, JobEntity $jobEntity): void
{
if ($job instanceof JobDataLess) {
$job->run();
return;
}
$data = Data::create($jobEntity->getData())
->withTargetId($jobEntity->getTargetId())
->withTargetType($jobEntity->getTargetType());
$job->run($data);
}
private function setJobRunning(JobEntity $jobEntity): void
{
if (!$jobEntity->getStartedAt()) {
$jobEntity->setStartedAtNow();
}
$jobEntity->setStatus(Status::RUNNING);
$jobEntity->setPid(System::getPid());
$this->entityManager->saveEntity($jobEntity);
}
}

View File

@@ -0,0 +1,193 @@
<?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\Field\DateTime as DateTimeField;
use Espo\Core\Job\Job\Data;
use Espo\Core\Job\JobScheduler\Creator;
use ReflectionClass;
use DateTimeInterface;
use DateTimeImmutable;
use DateInterval;
use RuntimeException;
use TypeError;
/**
* Creates jobs in a queue.
*/
class JobScheduler
{
/** @var ?class-string<Job|JobDataLess> */
private ?string $className = null;
private ?string $queue = null;
private ?string $group = null;
private ?Data $data = null;
private ?DateTimeImmutable $time = null;
private ?DateInterval $delay = null;
public function __construct(
private Creator $creator,
) {}
/**
* A class name of the job. Should implement the `Job` interface.
*
* @param class-string<Job|JobDataLess> $className
*/
public function setClassName(string $className): self
{
if (!class_exists($className)) {
throw new RuntimeException("Class '$className' does not exist.");
}
$class = new ReflectionClass($className);
if (
!$class->implementsInterface(Job::class) &&
!$class->implementsInterface(JobDataLess::class)
) {
throw new RuntimeException("Class '$className' does not implement 'Job' or 'JobDataLess' interface.");
}
$this->className = $className;
return $this;
}
/**
* In what queue to run the job.
*
* @param ?string $queue A queue name. Available names are defined in the `QueueName` class.
*/
public function setQueue(?string $queue): self
{
$this->queue = $queue;
return $this;
}
/**
* In what group to run the job. Jobs within a group will run one-by-one. Jobs with different group
* can run in parallel. The job can't have both queue and group set.
*
* @param ?string $group A group. Any string ID value can be used as a group name. E.g. a user ID.
*/
public function setGroup(?string $group): self
{
$this->group = $group;
return $this;
}
/**
* Set an execution time. If not set, then the current time will be used.
*/
public function setTime(?DateTimeInterface $time): self
{
$timeCopy = $time;
if (!is_null($time) && !$time instanceof DateTimeImmutable) {
/** @noinspection PhpParamsInspection */
$timeCopy = DateTimeImmutable::createFromMutable($time);
}
/** @var ?DateTimeImmutable $timeCopy */
$this->time = $timeCopy;
return $this;
}
/**
* Set an execution delay.
*/
public function setDelay(?DateInterval $delay): self
{
$this->delay = $delay;
return $this;
}
/**
* Set data to be passed to the job.
*
* @param Data|array<string, mixed>|null $data
*/
public function setData($data): self
{
/** @var mixed $data */
if (!is_null($data) && !is_array($data) && !$data instanceof Data) {
throw new TypeError();
}
if (!$data instanceof Data) {
$data = Data::create($data);
}
$this->data = $data;
return $this;
}
public function schedule(): void
{
if (!$this->className) {
throw new RuntimeException("Class name is not set.");
}
if ($this->group && $this->queue) {
throw new RuntimeException("Can't have both queue and group.");
}
$time = $this->time;
if (!$this->time && $this->delay) {
$time = new DateTimeImmutable();
}
if ($time && $this->delay) {
$time = $time->add($this->delay);
}
$data = $this->data ?? Data::create();
$creatorData = new Creator\Data(
className: $this->className,
queue: $this->queue,
group: $this->group,
data: $data,
time: $time ? DateTimeField::fromDateTime($time) : null,
);
$this->creator->create($creatorData);
}
}

View File

@@ -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\JobScheduler;
use Espo\Core\Job\JobScheduler\Creator\Data;
/**
* @since 9.2.0
*/
interface Creator
{
public function create(Data $data): void;
}

View File

@@ -0,0 +1,49 @@
<?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\JobScheduler\Creator;
use Espo\Core\Field\DateTime;
use Espo\Core\Job\Job;
use Espo\Core\Job\Job\Data as JobData;
use Espo\Core\Job\JobDataLess;
readonly class Data
{
/**
* @param class-string<Job|JobDataLess> $className
*/
public function __construct(
public string $className,
public ?string $queue,
public ?string $group,
public JobData $data,
public ?DateTime $time,
) {}
}

View File

@@ -0,0 +1,61 @@
<?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\JobScheduler\Creators;
use Espo\Core\Field\DateTime;
use Espo\Core\Job\JobScheduler\Creator;
use Espo\Entities\Job;
use Espo\ORM\EntityManager;
class EntityCreator implements Creator
{
public function __construct(
private EntityManager $entityManager,
) {}
public function create(Creator\Data $data): void
{
$time = $data->time ?? DateTime::createNow();
$job = $this->entityManager->getRDBRepositoryByClass(Job::class)->getNew();
$job
->setName($data->className)
->setClassName($data->className)
->setQueue($data->queue)
->setGroup($data->group)
->setData($data->data)
->setTargetId($data->data->getTargetId())
->setTargetType($data->data->getTargetType())
->setExecuteTime($time);
$this->entityManager->saveEntity($job);
}
}

View File

@@ -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;
use Espo\Core\InjectableFactory;
/**
* Creates job scheduler instances.
*/
class JobSchedulerFactory
{
public function __construct(private InjectableFactory $injectableFactory)
{}
public function create(): JobScheduler
{
return $this->injectableFactory->create(JobScheduler::class);
}
}

View File

@@ -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\ORM\Name\Attribute;
use Spatie\Async\Task as AsyncTask;
use Espo\Core\Application;
use Espo\Core\Application\Runner\Params as RunnerParams;
use Espo\Core\ApplicationRunners\Job as JobRunner;
use Espo\Core\Utils\Log;
use Throwable;
class JobTask extends AsyncTask
{
private string $jobId;
public function __construct(string $jobId)
{
$this->jobId = $jobId;
}
/**
* @return void
*/
public function configure()
{}
/**
* @return void
*/
public function run()
{
$app = new Application();
$params = RunnerParams::create()->with(Attribute::ID, $this->jobId);
try {
$app->run(JobRunner::class, $params);
} catch (Throwable $e) {
$log = $app->getContainer()->getByClass(Log::class);
$log->error("JobTask: Failed to run job '$this->jobId'. Error: " . $e->getMessage());
}
}
}

View File

@@ -0,0 +1,104 @@
<?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\Metadata;
class MetadataProvider
{
public function __construct(private Metadata $metadata)
{}
/**
* @return string[]
*/
public function getPreparableJobNameList(): array
{
$list = [];
$items = $this->metadata->get(['app', 'scheduledJobs']) ?? [];
foreach ($items as $name => $item) {
$isPreparable = (bool) ($item['preparatorClassName'] ?? null);
if ($isPreparable) {
$list[] = $name;
}
}
return $list;
}
public function isJobSystem(string $name): bool
{
return (bool) $this->metadata->get(['app', 'scheduledJobs', $name, 'isSystem']);
}
public function isJobPreparable(string $name): bool
{
return (bool) $this->metadata->get(['app', 'scheduledJobs', $name, 'preparatorClassName']);
}
public function getPreparatorClassName(string $name): ?string
{
return $this->metadata->get(['app', 'scheduledJobs', $name, 'preparatorClassName']);
}
public function getJobClassName(string $name): ?string
{
return $this->metadata->get(['app', 'scheduledJobs', $name, 'jobClassName']);
}
/**
* @return string[]
*/
public function getScheduledJobNameList(): array
{
/** @var array<string, mixed> $items */
$items = $this->metadata->get(['app', 'scheduledJobs']) ?? [];
return array_keys($items);
}
/**
* @return string[]
*/
public function getNonSystemScheduledJobNameList(): array
{
return array_filter(
$this->getScheduledJobNameList(),
function (string $item) {
$isSystem = (bool) $this->metadata->get(['app', 'scheduledJobs', $item, 'isSystem']);
return !$isSystem;
}
);
}
}

View File

@@ -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;
use Espo\Core\Job\Preparator\Data;
use DateTimeImmutable;
/**
* Creates multiple jobs for different targets according scheduling.
*/
interface Preparator
{
/**
* Create multiple job records for a scheduled job.
*/
public function prepare(Data $data, DateTimeImmutable $executeTime): void;
}

View File

@@ -0,0 +1,114 @@
<?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\Preparator;
use Espo\Core\Job\Job\Status;
use Espo\Core\Utils\DateTime;
use Espo\Entities\Job;
use Espo\ORM\Collection;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use DateTimeImmutable;
use Espo\ORM\Name\Attribute;
/**
* Creates jobs for each entity of a collection.
* To be used by Preparator implementations.
*
* @template TEntity of Entity
*/
class CollectionHelper
{
public function __construct(private EntityManager $entityManager)
{}
/**
* @param Collection<TEntity> $collection
*/
public function prepare(Collection $collection, Data $data, DateTimeImmutable $executeTime): void
{
foreach ($collection as $entity) {
$this->prepareItem($entity, $data, $executeTime);
}
}
/**
* @param TEntity $entity
*/
private function prepareItem(Entity $entity, Data $data, DateTimeImmutable $executeTime): void
{
$running = $this->entityManager
->getRDBRepositoryByClass(Job::class)
// Reduces the chance of race condition.
->forUpdate()
->select(Attribute::ID)
->where([
'scheduledJobId' => $data->getId(),
Job::FIELD_STATUS => [
Status::RUNNING,
Status::READY,
],
'targetType' => $entity->getEntityType(),
'targetId' => $entity->getId(),
])
->findOne();
if ($running) {
return;
}
$countPending = $this->entityManager
->getRDBRepositoryByClass(Job::class)
->where([
'scheduledJobId' => $data->getId(),
Job::FIELD_STATUS => Status::PENDING,
'targetType' => $entity->getEntityType(),
'targetId' => $entity->getId(),
])
->count();
if ($countPending > 1) {
return;
}
$job = $this->entityManager->getRDBRepositoryByClass(Job::class)->getNew();
$job->setMultiple([
'name' => $data->getName(),
'scheduledJobId' => $data->getId(),
'executeTime' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT),
'targetType' => $entity->getEntityType(),
'targetId' => $entity->getId(),
]);
$this->entityManager->saveEntity($job);
}
}

View File

@@ -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\Job\Preparator;
class Data
{
public function __construct(private string $id, private string $name)
{}
/**
* A scheduled job ID.
*/
public function getId(): string
{
return $this->id;
}
/**
* A scheduled job name.
*/
public function getName(): string
{
return $this->name;
}
}

View File

@@ -0,0 +1,114 @@
<?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\Preparator\Preparators;
use Espo\Core\Utils\DateTime;
use Espo\Core\Job\Job\Status;
use Espo\Core\Job\Preparator;
use Espo\Core\Job\Preparator\Data;
use Espo\ORM\EntityManager;
use Espo\Entities\Job as JobEntity;
use DateTimeImmutable;
use Espo\ORM\Name\Attribute;
class ProcessJobGroupPreparator implements Preparator
{
public function __construct(private EntityManager $entityManager)
{}
public function prepare(Data $data, DateTimeImmutable $executeTime): void
{
$groupList = [];
$query = $this->entityManager
->getQueryBuilder()
->select('group')
->from(JobEntity::ENTITY_TYPE)
->where([
'status' => Status::PENDING,
'queue' => null,
'group!=' => null,
'executeTime<=' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT),
])
->group('group')
->build();
$sth = $this->entityManager->getQueryExecutor()->execute($query);
while ($row = $sth->fetch()) {
$group = $row['group'];
if ($group === null) {
continue;
}
$groupList[] = $group;
}
if (!count($groupList)) {
return;
}
foreach ($groupList as $group) {
$existingJob = $this->entityManager
->getRDBRepository(JobEntity::ENTITY_TYPE)
->select(Attribute::ID)
->where([
'scheduledJobId' => $data->getId(),
'targetGroup' => $group,
'status' => [
Status::RUNNING,
Status::READY,
Status::PENDING,
],
])
->findOne();
if ($existingJob) {
continue;
}
$name = $data->getName() . ' :: ' . $group;
$this->entityManager->createEntity(JobEntity::ENTITY_TYPE, [
'scheduledJobId' => $data->getId(),
'executeTime' => $executeTime->format(DateTime::SYSTEM_DATE_TIME_FORMAT),
'name' => $name,
'data' => [
'group' => $group,
],
'targetGroup' => $group,
]);
}
}
}

View File

@@ -0,0 +1,57 @@
<?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\InjectableFactory;
use RuntimeException;
class PreparatorFactory
{
public function __construct(
private MetadataProvider $metadataProvider,
private InjectableFactory $injectableFactory
) {}
/**
* Create a preparator.
*/
public function create(string $name): Preparator
{
/** @var ?class-string<Preparator> $className */
$className = $this->metadataProvider->getPreparatorClassName($name);
if (!$className) {
throw new RuntimeException("Preparator for job '$name' not found.");
}
return $this->injectableFactory->create($className);
}
}

View File

@@ -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();
}
}

View File

@@ -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,
) {}
}

View File

@@ -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;
}

View File

@@ -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,
) {}
}

View File

@@ -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,
);
}
}

View File

@@ -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,
) {}
}

View File

@@ -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\Processing\Exceptions;
use Exception;
class TickFailure extends Exception
{}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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,
) {}
}

View File

@@ -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;
}

View File

@@ -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,
) {}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,204 @@
<?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\Exceptions\TickFailure;
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\Job\Processing\Util\Ticker;
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,
private Ticker $ticker,
) {}
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 {
$this->ticker->tick();
} catch (TickFailure $e) {
$this->log->warning("Tick failure.", ['exception' => $e]);
break;
}
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 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]);
$message->nack();
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,
]);
$message->nack();
return;
}
$message->ack();
},
);
}
private function toStop(Params $params, int $count): bool
{
return
$this->stopped ||
$params->limit && $count >= $params->limit ||
$this->exitPolicy->toExit();
}
}

View File

@@ -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]);
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,53 @@
<?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\Job\Processing\Exceptions\TickFailure;
use Espo\Core\Utils\Event\EventTransport;
use Espo\Core\Utils\Event\Exceptions\TransportNotConnected;
class Ticker
{
public function __construct(
private EventTransport $transport
) {}
/**
* @throws TickFailure
*/
public function tick(): void
{
try {
$this->transport->tick();
} catch (TransportNotConnected $e) {
throw new TickFailure(previous: $e);
}
}
}

View File

@@ -0,0 +1,71 @@
<?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;
use Espo\Core\Utils\Event\Configuration;
/**
* @since 10.1.0
* @internal
*/
class WorkerDaemon
{
public function __construct(
private Consumer $consumer,
private ExitSetup $exitSetup,
private Configuration $eventConfiguration,
) {}
public function run(WorkerDaemon\Params $params): void
{
$consumerParams = $this->prepareParams($params);
$this->setupExit();
$this->eventConfiguration->setSubscribeToCrossInstanceEvents(true);
$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,
);
}
}

View File

@@ -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,
) {}
}

View File

@@ -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;
class QueueName
{
/**
* Executes as soon as possible. Non-parallel by default.
*/
public const string Q0 = 'q0';
/**
* Executes every minute. Non-parallel by default.
*/
public const string Q1 = 'q1';
/**
* Executes as soon as possible. For email processing. Non-parallel by default.
*/
public const string E0 = 'e0';
/**
* Executes in the main queue pool in parallel. Along with jobs without specified queue.
* A portion is always picked for a queue iteration, even if there are no-queue
* jobs ordered before. E.g. if the portion size is 100, and there are 200 empty-queue
* jobs and 5 m0 jobs, 95 and 5 will be picked respectfully.
*
* @since 9.2.0
*/
const string M0 = 'm0';
}

View File

@@ -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;
use Espo\Core\Utils\Config;
class QueuePortionNumberProvider
{
/** @var array<string, int> */
private $queueNumberMap = [
QueueName::Q0 => self::Q0_PORTION_NUMBER,
QueueName::Q1 => self::Q1_PORTION_NUMBER,
QueueName::E0 => self::E0_PORTION_NUMBER,
];
/** @var array<string, string> */
private $queueParamNameMap = [
QueueName::Q0 => 'jobQ0MaxPortion',
QueueName::Q1 => 'jobQ1MaxPortion',
QueueName::E0 => 'jobE0MaxPortion',
];
private const Q0_PORTION_NUMBER = 200;
private const Q1_PORTION_NUMBER = 500;
private const E0_PORTION_NUMBER = 100;
private const DEFAULT_PORTION_NUMBER = 200;
public function __construct(private Config $config)
{}
public function get(string $queue): int
{
$paramName = $this->queueParamNameMap[$queue] ?? 'job' . ucfirst($queue) . 'MaxPortion';
return
$this->config->get($paramName) ??
$this->queueNumberMap[$queue] ??
self::DEFAULT_PORTION_NUMBER;
}
}

View File

@@ -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;
use Espo\Core\Job\QueueProcessor\Params;
/**
* @since 10.1.0
*/
interface QueueProcessor
{
public function process(Params $params): void;
}

View File

@@ -0,0 +1,116 @@
<?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;
class Params
{
private ?string $queue = null;
private ?string $group = null;
private int $limit = 0;
/** @var ?Params[] */
private ?array $subQueueParamsList = null;
private float $weight = 1.0;
public function withQueue(?string $queue): self
{
$obj = clone $this;
$obj->queue = $queue;
return $obj;
}
public function withGroup(?string $group): self
{
$obj = clone $this;
$obj->group = $group;
return $obj;
}
public function withLimit(int $limit): self
{
$obj = clone $this;
$obj->limit = $limit;
return $obj;
}
public function withWeight(float $weight): self
{
$obj = clone $this;
$obj->weight = $weight;
return $obj;
}
/**
* @param ?Params[] $subQueueParamsList
*/
public function withSubQueueParamsList(?array $subQueueParamsList): self
{
$obj = clone $this;
$obj->subQueueParamsList = $subQueueParamsList;
return $obj;
}
public function getQueue(): ?string
{
return $this->queue;
}
public function getGroup(): ?string
{
return $this->group;
}
public function getLimit(): int
{
return $this->limit;
}
public function getWeight(): float
{
return $this->weight;
}
/**
* @return ?Params[]
*/
public function getSubQueueParamsList(): ?array
{
return $this->subQueueParamsList;
}
public static function create(): self
{
return new self();
}
}

View File

@@ -0,0 +1,134 @@
<?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;
use Espo\Core\Job\QueueUtil;
use Espo\Entities\Job;
use RuntimeException;
/**
* Picks jobs for a portion distributing by weights if needed.
*/
class Picker
{
public function __construct(
private QueueUtil $queueUtil,
) {}
/**
* @param Params $params
* @return iterable<Job>
*/
public function pick(Params $params): iterable
{
$paramsList = $params->getSubQueueParamsList();
if (!$paramsList) {
return $this->queueUtil->getPendingJobs($params);
}
$groups = [];
foreach ($paramsList as $itemParams) {
$groups[] = iterator_to_array($this->queueUtil->getPendingJobs($itemParams));
}
return $this->pickJobsRecursively($paramsList, $groups, $params->getLimit());
}
/**
* @param Params[] $paramsList,
* @param Job[][] $groups
* @return Job[]
*/
private function pickJobsRecursively(
array $paramsList,
array $groups,
int $limit,
): array {
$totalWeight = array_reduce($paramsList, fn ($c, $it) => $c + $it->getWeight(), 0.0);
/** @var Job[][] $leftovers */
$leftovers = [];
$output = [];
foreach ($paramsList as $i => $itemParams) {
if (!array_key_exists($i, $groups)) {
throw new RuntimeException();
}
$jobs = $groups[$i];
$weight = $itemParams->getWeight();
$portion = (int) round($weight / $totalWeight * $limit);
$pickedJobs = [];
while (count($pickedJobs) < $portion) {
if (count($jobs) === 0) {
break;
}
$pickedJobs[] = array_shift($jobs);
}
$output = array_merge($output, $pickedJobs);
$leftovers[] = $jobs;
}
$left = $limit - count($output);
$leftoverCount = array_reduce($leftovers, fn ($c, $it) => $c + count($it), 0);
if ($left && $leftoverCount) {
foreach ($leftovers as $i => $jobs) {
if (count($jobs) === 0) {
unset($leftovers[$i]);
unset($paramsList[$i]);
}
}
$leftovers = array_values($leftovers);
$paramsList = array_values($paramsList);
$rest = $this->pickJobsRecursively(
paramsList: $paramsList,
groups: $leftovers,
limit: $leftoverCount,
);
$output = array_merge($output, $rest);
$output = array_slice($output, 0, $limit);
}
return $output;
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,132 @@
<?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
// Needed for failing not started.
->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();
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,492 @@
<?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 Countable;
use Espo\Core\Job\QueueProcessor\Params;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Utils\Config;
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 Espo\ORM\Collection;
use Espo\ORM\Name\Attribute;
use DateTime;
use Exception;
use LogicException;
class QueueUtil
{
private const int NOT_EXISTING_PROCESS_PERIOD = 300;
private const int READY_NOT_STARTED_PERIOD = 600;
public function __construct(
private Config $config,
private EntityManager $entityManager,
private ScheduleUtil $scheduleUtil,
private MetadataProvider $metadataProvider,
) {}
public function isJobPending(JobEntity $job): bool
{
$job = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([Attribute::ID, JobEntity::FIELD_STATUS])
->where([Attribute::ID => $job->getId()])
->forUpdate()
->findOne();
if (!$job) {
return false;
}
return $job->getStatus() === Status::PENDING;
}
/**
* @return Collection<JobEntity>&Countable
*/
public function getPendingJobs(Params $params): Collection
{
$queue = $params->getQueue();
$group = $params->getGroup();
$limit = $params->getLimit();
$builder = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([
Attribute::ID,
'scheduledJobId',
'scheduledJobJob',
'executeTime',
'targetId',
'targetType',
'targetGroup',
'className',
'job',
'data',
])
->where([
'status' => Status::PENDING,
'executeTime<=' => DateTimeUtil::getSystemNowString(),
'queue' => $queue,
'group' => $group,
])
->order('number');
if ($limit) {
$builder->limit(0, $limit);
}
return $builder->sth()->find();
}
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,
JobEntity::FIELD_STATUS => [
Status::RUNNING,
Status::READY,
],
];
if ($targetId && $targetType) {
$where['targetId'] = $targetId;
$where['targetType'] = $targetType;
}
if ($targetGroup) {
$where['targetGroup'] = $targetGroup;
}
return (bool) $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([Attribute::ID])
->where($where)
->findOne();
}
/**
* @return string[]
*/
public function getRunningScheduledJobIdList(): array
{
$list = [];
$jobList = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select(['scheduledJobId'])
->leftJoin('scheduledJob')
->where([
'status' => [
Status::RUNNING,
Status::READY,
],
'scheduledJobId!=' => null,
'scheduledJob.job!=' => $this->metadataProvider->getPreparableJobNameList(),
])
->order('executeTime')
->find();
foreach ($jobList as $job) {
$scheduledJobId = $job->getScheduledJobId();
if (!$scheduledJobId) {
continue;
}
$list[] = $scheduledJobId;
}
return $list;
}
public function hasScheduledJobOnMinute(string $scheduledJobId, string $time): bool
{
try {
$dateObj = new DateTime($time);
} catch (Exception $e) {
throw new LogicException($e->getMessage());
}
$fromString = $dateObj->format('Y-m-d H:i:00');
$toString = $dateObj->format('Y-m-d H:i:59');
$job = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([Attribute::ID])
->where([
'scheduledJobId' => $scheduledJobId,
'status' => [ // This forces usage of an appropriate index.
Status::PENDING,
Status::READY,
Status::RUNNING,
Status::SUCCESS,
],
'executeTime>=' => $fromString,
'executeTime<=' => $toString,
])
->findOne();
return (bool) $job;
}
public function getPendingCountByScheduledJobId(string $scheduledJobId): int
{
return $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->where([
'scheduledJobId' => $scheduledJobId,
'status' => Status::PENDING,
])
->count();
}
public function markJobsFailed(): void
{
$this->markJobsFailedByNotExistingProcesses();
$this->markJobsFailedReadyNotStarted();
$this->markJobsFailedByPeriod(true);
$this->markJobsFailedByPeriod();
}
private function markJobsFailedByNotExistingProcesses(): void
{
$timeThreshold = time() - $this->config->get(
'jobPeriodForNotExistingProcess',
self::NOT_EXISTING_PROCESS_PERIOD
);
$dateTimeThreshold = date(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT, $timeThreshold);
$runningJobList = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([
Attribute::ID,
'scheduledJobId',
'executeTime',
'targetId',
'targetType',
'pid',
'startedAt',
])
->where([
'status' => Status::RUNNING,
'startedAt<' => $dateTimeThreshold,
])
->find();
$failedJobList = [];
foreach ($runningJobList as $job) {
$pid = $job->getPid();
if ($pid && !System::isProcessActive($pid)) {
$failedJobList[] = $job;
}
}
$this->markJobListFailed($failedJobList);
}
private function markJobsFailedReadyNotStarted(): void
{
$timeThreshold = time() -
$this->config->get('jobPeriodForReadyNotStarted', self::READY_NOT_STARTED_PERIOD);
$dateTimeThreshold = date(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT, $timeThreshold);
$failedJobList = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([
Attribute::ID,
'scheduledJobId',
'executeTime',
'targetId',
'targetType',
'pid',
'startedAt',
])
->where([
'status' => Status::READY,
'startedAt<' => $dateTimeThreshold,
])
->find();
$this->markJobListFailed($failedJobList);
}
protected function markJobsFailedByPeriod(bool $isForActiveProcesses = false): void
{
$period = 'jobPeriod';
if ($isForActiveProcesses) {
$period = 'jobPeriodForActiveProcess';
}
$timeThreshold = time() - $this->config->get($period, 7800);
$dateTimeThreshold = date(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT, $timeThreshold);
$runningJobList = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([
Attribute::ID,
'scheduledJobId',
'executeTime',
'targetId',
'targetType',
'pid',
'startedAt'
])
->where([
'status' => Status::RUNNING,
'executeTime<' => $dateTimeThreshold,
])
->find();
$failedJobList = [];
foreach ($runningJobList as $job) {
if ($isForActiveProcesses) {
$failedJobList[] = $job;
continue;
}
$pid = $job->getPid();
if (!$pid || !System::isProcessActive($pid)) {
$failedJobList[] = $job;
}
}
$this->markJobListFailed($failedJobList);
}
/**
* @param iterable<JobEntity> $jobList
*/
protected function markJobListFailed(iterable $jobList): void
{
if (is_countable($jobList) && !count($jobList)) {
return;
}
$jobIdList = [];
foreach ($jobList as $job) {
$jobIdList[] = $job->getId();
}
$updateQuery = $this->entityManager
->getQueryBuilder()
->update()
->in(JobEntity::ENTITY_TYPE)
->set([
'status' => Status::FAILED,
'attempts' => 0,
])
->where([
Attribute::ID => $jobIdList,
])
->build();
$this->entityManager->getQueryExecutor()->execute($updateQuery);
foreach ($jobList as $job) {
$scheduledJobId = $job->getScheduledJobId();
if (!$scheduledJobId) {
continue;
}
$this->scheduleUtil->addLogRecord(
$scheduledJobId,
Status::FAILED,
$job->getStartedAt(),
$job->getTargetId(),
$job->getTargetType()
);
}
}
/**
* Remove pending duplicate jobs, no need to run twice the same job.
*/
public function removePendingJobDuplicates(): void
{
$duplicateJobList = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select(['scheduledJobId'])
->leftJoin('scheduledJob')
->where([
'scheduledJobId!=' => null,
'status' => Status::PENDING,
'executeTime<=' => DateTimeUtil::getSystemNowString(),
'scheduledJob.job!=' => $this->metadataProvider->getPreparableJobNameList(),
'targetId' => null,
])
->group(['scheduledJobId'])
->having([
'COUNT:id>' => 1,
])
->order('MAX:executeTime')
->find();
$scheduledJobIdList = [];
foreach ($duplicateJobList as $duplicateJob) {
$scheduledJobId = $duplicateJob->getScheduledJobId();
if (!$scheduledJobId) {
continue;
}
$scheduledJobIdList[] = $scheduledJobId;
}
foreach ($scheduledJobIdList as $scheduledJobId) {
$toRemoveJobList = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([Attribute::ID])
->where([
'scheduledJobId' => $scheduledJobId,
'status' => Status::PENDING,
])
->order('executeTime')
->limit(0, 1000)
->find();
$jobIdList = [];
foreach ($toRemoveJobList as $job) {
$jobIdList[] = $job->getId();
}
if (!count($jobIdList)) {
continue;
}
$delete = $this->entityManager
->getQueryBuilder()
->delete()
->from(JobEntity::ENTITY_TYPE)
->where([Attribute::ID => $jobIdList])
->build();
$this->entityManager->getQueryExecutor()->execute($delete);
}
}
/**
* Handle job attempts. Change failed to pending if attempts left.
*/
public function updateFailedJobAttempts(): void
{
$jobCollection = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([
Attribute::ID,
'attempts',
'failedAttempts',
])
->where([
'status' => Status::FAILED,
'executeTime<=' => DateTimeUtil::getSystemNowString(),
'attempts>' => 0,
])
->find();
foreach ($jobCollection as $job) {
$failedAttempts = $job->getFailedAttempts();
$attempts = $job->getAttempts();
$job->set([
'status' => Status::PENDING,
'attempts' => $attempts - 1,
'failedAttempts' => $failedAttempts + 1,
]);
$this->entityManager->saveEntity($job);
}
}
}

View File

@@ -0,0 +1,189 @@
<?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 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;
use Espo\Core\Job\Job\Status;
use Espo\Entities\Job as JobEntity;
use Espo\Entities\ScheduledJob as ScheduledJobEntity;
use Cron\CronExpression;
use Throwable;
use Exception;
use DateTimeImmutable;
/**
* Creates jobs from scheduled jobs according scheduling.
*/
class ScheduleProcessor
{
/** @var string[] */
private $asSoonAsPossibleSchedulingList = [
'*',
'* *',
'* * *',
'* * * *',
'* * * * *',
'* * * * * *',
];
public function __construct(
private Log $log,
private EntityManager $entityManager,
private QueueUtil $queueUtil,
private ScheduleUtil $scheduleUtil,
private PreparatorFactory $preparatorFactory,
private MetadataProvider $metadataProvider,
private ConfigDataProvider $configDataProvider,
) {}
public function process(Params $params = new Params()): void
{
$activeScheduledJobs = $this->scheduleUtil->getActiveScheduledJobs($params);
$runningScheduledJobIdList = $this->queueUtil->getRunningScheduledJobIdList();
foreach ($activeScheduledJobs as $scheduledJob) {
try {
$isRunning = in_array($scheduledJob->getId(), $runningScheduledJobIdList);
$this->createJobsFromScheduledJob($scheduledJob, $isRunning);
} catch (Throwable $e) {
$id = $scheduledJob->getId();
$this->log->error("Scheduled Job '$id': " . $e->getMessage());
}
}
}
private function createJobsFromScheduledJob(ScheduledJobEntity $scheduledJob, bool $isRunning): void
{
$id = $scheduledJob->getId();
$executeTime = $this->findExecuteTime($scheduledJob);
if ($executeTime === null) {
return;
}
$asSoonAsPossible = $this->checkAsSoonAsPossible($scheduledJob);
if (!$asSoonAsPossible) {
if ($this->queueUtil->hasScheduledJobOnMinute($id, $executeTime)) {
return;
}
}
$jobName = $scheduledJob->getJob();
if ($jobName && $this->metadataProvider->isJobPreparable($jobName)) {
$preparator = $this->preparatorFactory->create($jobName);
$data = new PreparatorData($scheduledJob->getId(), $scheduledJob->getName() ?? $jobName);
/** @var DateTimeImmutable $executeTimeObj */
$executeTimeObj = DateTimeImmutable
::createFromFormat(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT, $executeTime);
$preparator->prepare($data, $executeTimeObj);
return;
}
if ($isRunning) {
return;
}
$pendingCount = $this->queueUtil->getPendingCountByScheduledJobId($id);
$pendingLimit = $asSoonAsPossible ? 0 : 1;
if ($pendingCount > $pendingLimit) {
return;
}
$this->entityManager->createEntity(JobEntity::ENTITY_TYPE, [
'name' => $scheduledJob->getName(),
'status' => Status::PENDING,
'scheduledJobId' => $id,
'executeTime' => $executeTime,
]);
}
private function checkAsSoonAsPossible(ScheduledJobEntity $scheduledJob): bool
{
return in_array($scheduledJob->getScheduling(), $this->asSoonAsPossibleSchedulingList);
}
private function findExecuteTime(ScheduledJobEntity $scheduledJob): ?string
{
$scheduling = $scheduledJob->getScheduling();
if ($scheduling === null) {
return null;
}
$id = $scheduledJob->getId();
$asSoonAsPossible = in_array($scheduling, $this->asSoonAsPossibleSchedulingList);
if ($asSoonAsPossible) {
return DateTimeUtil::getSystemNowString();
}
try {
$cronExpression = CronExpression::factory($scheduling);
} catch (Exception $e) {
$this->log->error(
"Scheduled Job '$id': Scheduling expression error: " .
$e->getMessage() . '.');
return null;
}
$timeZone = $this->configDataProvider->getTimeZone();
try {
$next = $cronExpression->getNextRunDate(timeZone: $timeZone)
->setTimezone(new DateTimeZone('UTC'));
} catch (Exception) {
$this->log->error("Scheduled Job '$id': Unsupported scheduling expression '$scheduling'.");
return null;
}
return $next->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
}
}

View File

@@ -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,
) {}
}

View File

@@ -0,0 +1,125 @@
<?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\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;
use Espo\Entities\ScheduledJobLogRecord as ScheduledJobLogRecordEntity;
use Espo\ORM\Name\Attribute;
/**
* @internal
*/
class ScheduleUtil
{
public function __construct(private EntityManager $entityManager)
{}
/**
* Get active scheduled job list.
*
* @return Collection<ScheduledJob>
*/
public function getActiveScheduledJobs(Params $params): Collection
{
$collection = $this->entityManager
->getRDBRepositoryByClass(ScheduledJob::class)
->select([
Attribute::ID,
ScheduledJob::FIELD_SCHEDULING,
ScheduledJob::FIELD_JOB,
Field::NAME,
])
->where([
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;
}
/**
* Add record to ScheduledJobLogRecord about executed job.
*/
public function addLogRecord(
string $scheduledJobId,
string $status,
?string $runTime = null,
?string $targetId = null,
?string $targetType = null
): void {
if (!isset($runTime)) {
$runTime = date(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
}
/** @var ScheduledJob|null $scheduledJob */
$scheduledJob = $this->entityManager->getEntityById(ScheduledJob::ENTITY_TYPE, $scheduledJobId);
if (!$scheduledJob) {
return;
}
$scheduledJob->set('lastRun', $runTime);
$this->entityManager->saveEntity($scheduledJob, [SaveOption::SILENT => true]);
$scheduledJobLog = $this->entityManager->getNewEntity(ScheduledJobLogRecordEntity::ENTITY_TYPE);
$scheduledJobLog->setMultiple([
'scheduledJobId' => $scheduledJobId,
'name' => $scheduledJob->getName(),
'status' => $status,
'executionTime' => $runTime,
'targetId' => $targetId,
'targetType' => $targetType,
]);
$this->entityManager->saveEntity($scheduledJobLog);
}
}