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

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

View File

@@ -0,0 +1,42 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing;
use Espo\Core\Job\Processing\Consumer\Params;
/**
* @since 10.1.0
*/
interface Consumer
{
public function start(Params $params): void;
public function stop(): void;
}

View File

@@ -0,0 +1,38 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\Consumer;
readonly class Params
{
public function __construct(
public ?int $limit = null,
public ?string $queue = null,
) {}
}

View File

@@ -0,0 +1,217 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing;
use Espo\Core\Job\ConfigDataProvider;
use Espo\Core\Job\Job\Status;
use Espo\Core\Job\Processing\Util\ExitPolicy;
use Espo\Core\Job\Processing\Util\ExitSetup;
use Espo\Core\Job\QueueName;
use Espo\Core\Job\QueueProcessor\Params;
use Espo\Core\Job\QueueProcessor\Picker;
use Espo\Core\Job\QueueUtil;
use Espo\Core\Utils\Log;
use Espo\Entities\Job;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
use RuntimeException;
use Throwable;
/**
* @since 10.1.0
*
* @internal
*/
class EnqueueDaemon
{
private const float INTERVAL = 1.0;
private bool $stopped = false;
public function __construct(
private Picker $picker,
private ConfigDataProvider $configDataProvider,
private Publisher $publisher,
private EntityManager $entityManager,
private QueueUtil $queueUtil,
private Log $log,
private ExitSetup $exitSetup,
private ExitPolicy $exitPolicy,
) {}
public function run(EnqueueDaemon\Params $params): void
{
$interval = $this->getInterval($params);
$limit = $params->limit;
$pickerParams = $this->getPickerParams($params);
$publisherParams = $this->preparePublisherParams($params);
$this->publisher->initialize($publisherParams);
$this->exitSetup->setup(function () {
$this->stopped = true;
});
$count = 0;
while (true) {
$jobs = $this->picker->pick($pickerParams);
foreach ($jobs as $job) {
$this->processJob($job);
$count ++;
if ($this->toForceExit() || $this->toExit($limit, $count)) {
break 2;
}
}
usleep($interval);
if ($this->toForceExit()) {
break;
}
}
$this->publisher->close();
}
private function isStopped(): bool
{
return $this->stopped;
}
private function getPickerParams(EnqueueDaemon\Params $daemonParams): Params
{
$limit = $daemonParams->portion ?? $this->configDataProvider->getMaxPortion();
$params = Params::create()
->withLimit($limit);
if ($daemonParams->queue !== null) {
return $params->withQueue($daemonParams->queue);
}
return $params->withSubQueueParamsList([
$params->withWeight(0.5),
$params->withQueue(QueueName::M0)->withWeight(0.5),
]);
}
private function getInterval(EnqueueDaemon\Params $params): int
{
$interval = $params->interval ?? self::INTERVAL;
return (int) ($interval * 1000000);
}
private function prepareJob(Job $job): void
{
$job
// Needed for failing not started.
->setStartedAtNow()
->setStatus(Status::READY);
$this->entityManager->saveEntity($job);
}
private function processJob(Job $job): void
{
$this->entityManager->getTransactionManager()->run(function () use ($job) {
$this->processJobInternal($job);
});
}
private function toSkip(Job $job): bool
{
if ($job->getStatus() !== Status::PENDING) {
return true;
}
if ($this->queueUtil->isScheduledJobRunning($job)) {
return true;
}
return false;
}
private function fetchLocked(Job $job): ?Job
{
return $this->entityManager
->getRDBRepositoryByClass(Job::class)
->forUpdate()
->where([Attribute::ID => $job->getId()])
->findOne();
}
private function processJobInternal(Job $job): void
{
$job = $this->fetchLocked($job);
if (!$job || $this->toSkip($job)) {
return;
}
$this->prepareJob($job);
try {
$this->publisher->publish($job);
} catch (Throwable $e) {
$this->log->error("Enqueue: Could not publish job {id}.", [
'id' => $job->getId(),
'exception' => $e,
]);
throw new RuntimeException(previous: $e);
}
}
private function toExit(?int $limit, int $count): bool
{
return $limit && $count >= $limit;
}
/**
* @phpstan-impure
*/
private function toForceExit(): bool
{
return $this->isStopped() || $this->exitPolicy->toExit();
}
private function preparePublisherParams(EnqueueDaemon\Params $params): Publisher\Params
{
return new Publisher\Params(
queue: $params->queue,
);
}
}

View File

@@ -0,0 +1,43 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\EnqueueDaemon;
readonly class Params
{
/**
* @param ?float $interval Interval in seconds.
*/
public function __construct(
public ?float $interval = null,
public ?int $limit = null,
public ?int $portion = null,
public ?string $queue = null,
) {}
}

View File

@@ -0,0 +1,35 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\Exceptions;
use Exception;
class TickFailure extends Exception
{}

View File

@@ -0,0 +1,65 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing;
use Espo\Core\Job\Job\Status;
use Espo\Entities\Job;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
use Exception;
class JobProvider
{
public function __construct(
private EntityManager $entityManager,
) {}
/**
* @throws Exception
*/
public function get(string $id): Job
{
$job = $this->entityManager
->getRDBRepositoryByClass(Job::class)
->forUpdate()
->where([Attribute::ID => $id])
->findOne();
if (!$job) {
throw new Exception("Job $id not found.");
}
if ($job->getStatus() !== Status::READY) {
throw new Exception("Job $id is not in status Ready.");
}
return $job;
}
}

View File

@@ -0,0 +1,113 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing;
use Espo\Core\Job\Exceptions\TooFrequentRun;
use Espo\Core\Job\PrepareProcessor;
use Espo\Core\Job\Processing\Util\ExitPolicy;
use Espo\Core\Job\Processing\Util\ExitSetup;
/**
* @since 10.1.0
*/
class PrepareDaemon
{
private const float INTERVAL = 10.0;
private bool $stopped = false;
public function __construct(
private ExitSetup $exitSetup,
private ExitPolicy $exitPolicy,
private PrepareProcessor $prepareProcessor,
) {}
public function run(PrepareDaemon\Params $params): void
{
$interval = $this->getInterval($params);
$prepareParams = new PrepareProcessor\Params(
skipQueues: $params->skipQueues,
);
$this->exitSetup->setup(function () {
$this->stopped = true;
});
$count = 0;
while (true) {
try {
$this->prepareProcessor->process($prepareParams);
} catch (TooFrequentRun) {
continue;
}
$count ++;
if ($this->toForceExit() || $this->toExit($params, $count)) {
break;
}
usleep($interval);
if ($this->toForceExit()) {
break;
}
}
$this->stopped = false;
}
private function getInterval(PrepareDaemon\Params $params): int
{
$interval = $params->interval ?? self::INTERVAL;
return (int) ($interval * 1000000);
}
private function isStopped(): bool
{
return $this->stopped;
}
private function toExit(PrepareDaemon\Params $params, int $count): bool
{
return $params->limit && $count >= $params->limit;
}
/**
* @phpstan-impure
*/
private function toForceExit(): bool
{
return $this->isStopped() || $this->exitPolicy->toExit();
}
}

View File

@@ -0,0 +1,42 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\PrepareDaemon;
readonly class Params
{
/**
* @param ?float $interval Interval in seconds.
*/
public function __construct(
public ?float $interval = null,
public ?int $limit = null,
public bool $skipQueues = false,
) {}
}

View File

@@ -0,0 +1,45 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing;
use Espo\Core\Job\Processing\Publisher\Params;
use Espo\Entities\Job;
/**
* @since 10.1.0
*/
interface Publisher
{
public function initialize(Params $params): void;
public function publish(Job $job): void;
public function close(): void;
}

View File

@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\Publisher;
readonly class Params
{
public function __construct(
public ?string $queue,
) {}
}

View File

@@ -0,0 +1,58 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\RabbitMq;
use Espo\Core\Utils\Config;
use Exception;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use RuntimeException;
class ConnectionFactory
{
public function __construct(
private Config $config,
) {}
public function create(): AMQPStreamConnection
{
$host = $this->config->get('rabbitMq.host');
$port = $this->config->get('rabbitMq.port');
$user = $this->config->get('rabbitMq.user');
$password = $this->config->get('rabbitMq.password');
try {
$connection = new AMQPStreamConnection($host, $port, $user, $password);
} catch (Exception $e) {
throw new RuntimeException("Connection error.", previous: $e);
}
return $connection;
}
}

View File

@@ -0,0 +1,204 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\RabbitMq;
use Espo\Core\Job\JobRunner;
use Espo\Core\Job\Processing\Exceptions\TickFailure;
use Espo\Core\Job\Processing\JobProvider;
use Espo\Core\Job\Processing\Consumer as ConsumerInterface;
use Espo\Core\Job\Processing\Consumer\Params;
use Espo\Core\Job\Processing\Util\ExitPolicy;
use Espo\Core\Job\Processing\Util\Ticker;
use Espo\Core\Utils\Log;
use Exception;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Message\AMQPMessage;
use RuntimeException;
use Throwable;
class Consumer implements ConsumerInterface
{
private bool $stopped = false;
private const float ITERATION_TIMEOUT = 1.0;
public function __construct(
private ConnectionFactory $connectionFactory,
private JobRunner $jobRunner,
private Log $log,
private JobProvider $jobProvider,
private ExitPolicy $exitPolicy,
private Ticker $ticker,
) {}
public function start(Params $params): void
{
if ($this->stopped) {
$this->stopped = false;
return;
}
$queue = Util::composeQueueName($params->queue);
$connection = $this->connectionFactory->create();
$channel = $this->prepareChannel($connection, $queue);
$this->setupConsume($channel, $queue);
$count = 0;
while ($channel->is_consuming()) {
try {
$this->ticker->tick();
} catch (TickFailure $e) {
$this->log->warning("Tick failure.", ['exception' => $e]);
break;
}
try {
$channel->wait(timeout: self::ITERATION_TIMEOUT);
} catch (AMQPTimeoutException) {
if ($this->toStop($params, $count)) {
break;
}
continue;
}
$count ++;
if ($this->toStop($params, $count)) {
break;
}
}
$channel->close();
$this->closeConnection($connection);
$this->stopped = false;
}
public function stop(): void
{
$this->stopped = true;
}
private function prepareChannel(AMQPStreamConnection $connection, string $queue): AMQPChannel
{
$channel = $connection->channel();
$channel->queue_declare(
queue: $queue,
durable: true,
auto_delete: false,
);
$channel->basic_qos(
prefetch_size: 0,
prefetch_count: 1,
a_global: false,
);
return $channel;
}
private function closeConnection(AMQPStreamConnection $connection): void
{
try {
$connection->close();
} catch (Exception $e) {
throw new RuntimeException("Connection closing error.", previous: $e);
}
}
private function getJobId(AMQPMessage $message): string
{
$payload = json_decode($message->getBody());
if (!is_object($payload)) {
throw new RuntimeException("Bad payload.");
}
$id = $payload->id ?? null;
if (!is_string($id)) {
throw new RuntimeException("No string ID.");
}
return $id;
}
private function setupConsume(AMQPChannel $channel, string $queue): void
{
$channel->basic_consume(
queue: $queue,
callback: function (AMQPMessage $message) {
try {
$id = $this->getJobId($message);
} catch (Throwable $e) {
$this->log->error("Worker: Could not get job ID.", ['exception' => $e]);
$message->nack();
return;
}
try {
$job = $this->jobProvider->get($id);
$this->jobRunner->run($job);
} catch (Throwable $e) {
$this->log->error("Worker: Job {id} failed.", [
'exception' => $e,
'id' => $id,
]);
$message->nack();
return;
}
$message->ack();
},
);
}
private function toStop(Params $params, int $count): bool
{
return
$this->stopped ||
$params->limit && $count >= $params->limit ||
$this->exitPolicy->toExit();
}
}

View File

@@ -0,0 +1,117 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\RabbitMq;
use Espo\Core\Job\Processing\Publisher as PublisherInterface;
use Espo\Core\Job\Processing\Publisher\Params;
use Espo\Core\Utils\Json;
use Espo\Entities\Job;
use Espo\ORM\Name\Attribute;
use Exception;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use RuntimeException;
class Publisher implements PublisherInterface
{
public const string QUEUE = 'espo.jobs';
private ?AMQPStreamConnection $connection = null;
private ?AMQPChannel $channel = null;
private string $queue = self::QUEUE;
public function __construct(
private ConnectionFactory $connectionFactory,
) {}
public function initialize(Params $params): void
{
$this->queue = Util::composeQueueName($params->queue);
$this->connection = $this->connectionFactory->create();
$this->channel = $this->prepareChannel($this->connection);
}
public function publish(Job $job): void
{
if (!$this->channel) {
throw new RuntimeException("No channel.");
}
$message = $this->prepareMessage($job);
$this->channel->basic_publish(
msg: $message,
routing_key: $this->queue,
);
}
public function close(): void
{
$this->channel?->close();
if ($this->connection) {
$this->closeConnection($this->connection);
}
$this->channel = null;
$this->connection = null;
}
private function prepareChannel(AMQPStreamConnection $connection): AMQPChannel
{
$channel = $connection->channel();
$channel->queue_declare(
queue: $this->queue,
durable: true,
auto_delete: false,
);
return $channel;
}
private function closeConnection(AMQPStreamConnection $connection): void
{
try {
$connection->close();
} catch (Exception $e) {
throw new RuntimeException("Connection closing error.", previous: $e);
}
}
private function prepareMessage(Job $job): AMQPMessage
{
$payload = Json::encode([Attribute::ID => $job->getId()]);
return new AMQPMessage($payload, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
}
}

View File

@@ -0,0 +1,44 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\RabbitMq;
class Util
{
public static function composeQueueName(?string $queue): string
{
$name = Publisher::QUEUE;
if ($queue !== null) {
$name .= '.' . $queue;
}
return $name;
}
}

View File

@@ -0,0 +1,50 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\Util;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Config\StateConfig;
class ExitPolicy
{
private int $cacheTimestamp;
public function __construct(
private StateConfig $stateConfig,
private Config\StateConfigDirect $stateConfigDirect,
) {
$this->cacheTimestamp = $this->stateConfig->getCacheTimestamp();
}
public function toExit(): bool
{
return $this->cacheTimestamp !== $this->stateConfigDirect->getCacheTimestamp();
}
}

View File

@@ -0,0 +1,46 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\Util;
use Closure;
class ExitSetup
{
public function setup(Closure $handler): void
{
if (!extension_loaded('pcntl')) {
return;
}
pcntl_async_signals(true);
pcntl_signal(SIGTERM, fn () => $handler());
pcntl_signal(SIGINT, fn () => $handler());
}
}

View File

@@ -0,0 +1,53 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\Util;
use Espo\Core\Job\Processing\Exceptions\TickFailure;
use Espo\Core\Utils\Event\EventTransport;
use Espo\Core\Utils\Event\Exceptions\TransportNotConnected;
class Ticker
{
public function __construct(
private EventTransport $transport
) {}
/**
* @throws TickFailure
*/
public function tick(): void
{
try {
$this->transport->tick();
} catch (TransportNotConnected $e) {
throw new TickFailure(previous: $e);
}
}
}

View File

@@ -0,0 +1,71 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing;
use Espo\Core\Job\Processing\Consumer\Params;
use Espo\Core\Job\Processing\Util\ExitSetup;
use Espo\Core\Utils\Event\Configuration;
/**
* @since 10.1.0
* @internal
*/
class WorkerDaemon
{
public function __construct(
private Consumer $consumer,
private ExitSetup $exitSetup,
private Configuration $eventConfiguration,
) {}
public function run(WorkerDaemon\Params $params): void
{
$consumerParams = $this->prepareParams($params);
$this->setupExit();
$this->eventConfiguration->setSubscribeToCrossInstanceEvents(true);
$this->consumer->start($consumerParams);
}
private function setupExit(): void
{
$this->exitSetup->setup(fn () => $this->consumer->stop());
}
private function prepareParams(WorkerDaemon\Params $params): Params
{
return new Params(
limit: $params->limit,
queue: $params->queue,
);
}
}

View File

@@ -0,0 +1,38 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job\Processing\WorkerDaemon;
readonly class Params
{
public function __construct(
public ?int $limit = null,
public ?string $queue = null,
) {}
}