Initial commit

This commit is contained in:
root
2026-01-19 17:44:46 +01:00
commit 823af8b11d
8721 changed files with 1130846 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-2025 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-2025 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,43 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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-2025 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-2025 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-2025 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,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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;
}

View File

@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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;
}

View File

@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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;
}

View File

@@ -0,0 +1,39 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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-2025 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,86 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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.
*
* @return Job|JobDataLess
*/
public function create(string $name): object
{
$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
* @return Job|JobDataLess
*/
public function createByClassName(string $className): object
{
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,197 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Utils\File\Manager as FileManager;
use Espo\Core\Utils\Log;
use Espo\Entities\Job as JobEntity;
use RuntimeException;
use Throwable;
/**
* Handles processing jobs.
*/
class JobManager
{
private bool $useProcessPool = false;
protected string $lastRunTimeFile = 'data/cache/application/cronLastRunTime.php';
public function __construct(
private FileManager $fileManager,
private JobRunner $jobRunner,
private Log $log,
private ScheduleProcessor $scheduleProcessor,
private QueueUtil $queueUtil,
private AsyncPoolFactory $asyncPoolFactory,
private QueueProcessor $queueProcessor,
private ConfigDataProvider $configDataProvider
) {
if ($this->configDataProvider->runInParallel()) {
if ($this->asyncPoolFactory->isSupported()) {
$this->useProcessPool = true;
} else {
$this->log->warning("Enabled `jobRunInParallel` parameter requires pcntl and posix extensions.");
}
}
}
/**
* Process jobs. Jobs will be created according scheduling. Then pending jobs will be processed.
* This method supposed to be called on every Cron run or loop iteration of the Daemon.
*/
public function process(): void
{
if (!$this->checkLastRunTime()) {
$this->log->info('JobManager: Skip job processing. Too frequent execution.');
return;
}
$this->updateLastRunTime();
$this->queueUtil->markJobsFailed();
$this->queueUtil->updateFailedJobAttempts();
$this->scheduleProcessor->process();
$this->queueUtil->removePendingJobDuplicates();
$this->processMainQueue();
}
/**
* 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)
->withUseProcessPool(false)
->withNoLock(true);
$this->queueProcessor->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)
->withUseProcessPool(false)
->withNoLock(true);
$this->queueProcessor->process($params);
}
private function processMainQueue(): void
{
$limit = $this->configDataProvider->getMaxPortion();
$params = Params
::create()
->withUseProcessPool($this->useProcessPool)
->withLimit($limit);
$subQueueParams = [
$params->withWeight(0.5),
$params->withQueue(QueueName::M0)->withWeight(0.5),
];
$params = $params->withSubQueueParamsList($subQueueParams);
$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);
}
/**
* @todo Move to a separate class.
*/
private function getLastRunTime(): int
{
if ($this->fileManager->isFile($this->lastRunTimeFile)) {
try {
$data = $this->fileManager->getPhpContents($this->lastRunTimeFile);
} catch (RuntimeException) {
$data = null;
}
if (is_array($data) && isset($data['time'])) {
return (int) $data['time'];
}
}
return time() - $this->configDataProvider->getCronMinInterval() - 1;
}
/**
* @todo Move to a separate class.
*/
private function updateLastRunTime(): void
{
$data = ['time' => time()];
$this->fileManager->putPhpContents($this->lastRunTimeFile, $data, false, true);
}
private function checkLastRunTime(): bool
{
$currentTime = time();
$lastRunTime = $this->getLastRunTime();
$cronMinInterval = $this->configDataProvider->getCronMinInterval();
if ($currentTime > ($lastRunTime + $cronMinInterval)) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,270 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\ServiceFactory;
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 ServiceFactory $serviceFactory,
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.");
}
/** @var ?JobEntity $jobEntity */
$jobEntity = $this->entityManager->getEntityById(JobEntity::ENTITY_TYPE, $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 if ($jobEntity->getServiceName()) {
$this->runService($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}. {message}", [
'exception' => $e,
'message' => $e->getMessage(),
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(
$jobEntity->getScheduledJobId(),
$status,
null,
$jobEntity->getTargetId(),
$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);
}
/**
* @param Job|JobDataLess $job
* @internal Native type is not used for bc.
*/
private function runJob($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 runService(JobEntity $jobEntity): void
{
$serviceName = $jobEntity->getServiceName();
if (!$serviceName) {
throw new RuntimeException("Job with empty serviceName.");
}
if (!$this->serviceFactory->checkExists($serviceName)) {
throw new RuntimeException("No service $serviceName.");
}
$service = $this->serviceFactory->create($serviceName);
$methodName = $jobEntity->getMethodName();
if (!$methodName) {
throw new RuntimeException('Job with empty methodName.');
}
if (!method_exists($service, $methodName)) {
throw new RuntimeException("No method '$methodName' in service '$serviceName'.");
}
$service->$methodName(
$jobEntity->getData(),
$jobEntity->getTargetId(),
$jobEntity->getTargetType()
);
}
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-2025 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-2025 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-2025 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-2025 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-2025 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-2025 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-2025 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-2025 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,112 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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
->getRDBRepository(Job::ENTITY_TYPE)
->select(Attribute::ID)
->where([
'scheduledJobId' => $data->getId(),
'status' => [
Status::RUNNING,
Status::READY,
],
'targetType' => $entity->getEntityType(),
'targetId' => $entity->getId(),
])
->findOne();
if ($running) {
return;
}
$countPending = $this->entityManager
->getRDBRepository(Job::ENTITY_TYPE)
->where([
'scheduledJobId' => $data->getId(),
'status' => Status::PENDING,
'targetType' => $entity->getEntityType(),
'targetId' => $entity->getId(),
])
->count();
if ($countPending > 1) {
return;
}
$job = $this->entityManager->getNewEntity(Job::ENTITY_TYPE);
$job->set([
'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-2025 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-2025 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-2025 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,58 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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.
*/
public const Q0 = 'q0';
/**
* Executes every minute. Non-parallel.
*/
public const Q1 = 'q1';
/**
* Executes as soon as possible. For email processing. Non-parallel.
*/
public const 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 M0 = 'm0';
}

View File

@@ -0,0 +1,67 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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,145 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Picker;
use Espo\Entities\Job as JobEntity;
use Espo\Core\Job\QueueProcessor\Params;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Utils\System;
use Espo\Core\Job\Job\Status;
use Spatie\Async\Pool as AsyncPool;
class QueueProcessor
{
private bool $noTableLocking;
public function __construct(
private QueueUtil $queueUtil,
private JobRunner $jobRunner,
private AsyncPoolFactory $asyncPoolFactory,
private EntityManager $entityManager,
private Picker $picker,
ConfigDataProvider $configDataProvider
) {
$this->noTableLocking = $configDataProvider->noTableLocking();
}
public function process(Params $params): void
{
$pool = $params->useProcessPool() ?
$this->asyncPoolFactory->create() : null;
foreach ($this->picker->pick($params) as $job) {
$this->processJob($params, $job, $pool);
}
$pool?->wait();
}
private function processJob(Params $params, JobEntity $job, ?AsyncPool $pool = null): void
{
$noLock = $params->noLock();
$lockTable = $job->getScheduledJobId() && !$noLock && !$this->noTableLocking;
if ($lockTable) {
// MySQL doesn't allow to lock non-existent rows. We resort to locking an entire table.
$this->entityManager->getLocker()->lockExclusive(JobEntity::ENTITY_TYPE);
}
$skip = $this->toSkip($noLock, $job);
if ($skip) {
if ($lockTable) {
$this->entityManager->getLocker()->rollback();
}
return;
}
$this->prepareJob($job, $pool);
$this->entityManager->saveEntity($job);
if ($lockTable) {
$this->entityManager->getLocker()->commit();
}
$this->runJob($job, $pool);
}
private function toSkip(bool $noLock, JobEntity $job): bool
{
$skip = !$noLock && !$this->queueUtil->isJobPending($job->getId());
if (
!$skip &&
$job->getScheduledJobId() &&
$this->queueUtil->isScheduledJobRunning(
$job->getScheduledJobId(),
$job->getTargetId(),
$job->getTargetType(),
$job->getTargetGroup()
)
) {
$skip = true;
}
return $skip;
}
private function prepareJob(JobEntity $job, ?AsyncPool $pool): void
{
$job->setStartedAtNow();
if ($pool) {
$job->setStatus(Status::READY);
return;
}
$job->setStatus(Status::RUNNING);
$job->setPid(System::getPid());
}
private function runJob(JobEntity $job, ?AsyncPool $pool): void
{
if (!$pool) {
$this->jobRunner->run($job);
return;
}
$task = new JobTask($job->getId());
$pool->add($task);
}
}

View File

@@ -0,0 +1,144 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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 bool $useProcessPool = false;
private bool $noLock = false;
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 withUseProcessPool(bool $useProcessPool): self
{
$obj = clone $this;
$obj->useProcessPool = $useProcessPool;
return $obj;
}
public function withNoLock(bool $noLock): self
{
$obj = clone $this;
$obj->noLock = $noLock;
return $obj;
}
public function withQueue(?string $queue): self
{
$obj = clone $this;
$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 useProcessPool(): bool
{
return $this->useProcessPool;
}
public function noLock(): bool
{
return $this->noLock;
}
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-2025 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,492 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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 DateTime;
use Espo\ORM\Collection;
use Espo\ORM\Name\Attribute;
use Exception;
use LogicException;
class QueueUtil
{
private const NOT_EXISTING_PROCESS_PERIOD = 300;
private const READY_NOT_STARTED_PERIOD = 60;
public function __construct(
private Config $config,
private EntityManager $entityManager,
private ScheduleUtil $scheduleUtil,
private MetadataProvider $metadataProvider
) {}
public function isJobPending(string $id): bool
{
/** @var ?JobEntity $job */
$job = $this->entityManager
->getRDBRepositoryByClass(JobEntity::class)
->select([Attribute::ID, 'status'])
->where([Attribute::ID => $id])
->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',
'methodName',
'serviceName',
'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(
string $scheduledJobId,
?string $targetId = null,
?string $targetType = null,
?string $targetGroup = null
): bool {
$where = [
'scheduledJobId' => $scheduledJobId,
'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,188 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\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(): void
{
$activeScheduledJobList = $this->scheduleUtil->getActiveScheduledJobList();
$runningScheduledJobIdList = $this->queueUtil->getRunningScheduledJobIdList();
foreach ($activeScheduledJobList 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,108 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\DateTime as DateTimeUtil;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\ORM\Collection;
use Espo\ORM\EntityManager;
use Espo\Entities\ScheduledJob as ScheduledJobEntity;
use Espo\Entities\ScheduledJobLogRecord as ScheduledJobLogRecordEntity;
use Espo\ORM\Name\Attribute;
class ScheduleUtil
{
public function __construct(private EntityManager $entityManager)
{}
/**
* Get active scheduled job list.
*
* @return Collection<ScheduledJobEntity>
*/
public function getActiveScheduledJobList(): Collection
{
/** @var Collection<ScheduledJobEntity> $collection */
$collection = $this->entityManager
->getRDBRepository(ScheduledJobEntity::ENTITY_TYPE)
->select([
Attribute::ID,
'scheduling',
'job',
'name',
])
->where([
'status' => ScheduledJobEntity::STATUS_ACTIVE,
])
->find();
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 ScheduledJobEntity|null $scheduledJob */
$scheduledJob = $this->entityManager->getEntityById(ScheduledJobEntity::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->set([
'scheduledJobId' => $scheduledJobId,
'name' => $scheduledJob->getName(),
'status' => $status,
'executionTime' => $runTime,
'targetId' => $targetId,
'targetType' => $targetType,
]);
$this->entityManager->saveEntity($scheduledJobLog);
}
}