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
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
204
application/Espo/Core/Job/Processing/RabbitMq/Consumer.php
Normal file
204
application/Espo/Core/Job/Processing/RabbitMq/Consumer.php
Normal 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();
|
||||
}
|
||||
}
|
||||
117
application/Espo/Core/Job/Processing/RabbitMq/Publisher.php
Normal file
117
application/Espo/Core/Job/Processing/RabbitMq/Publisher.php
Normal 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]);
|
||||
}
|
||||
}
|
||||
44
application/Espo/Core/Job/Processing/RabbitMq/Util.php
Normal file
44
application/Espo/Core/Job/Processing/RabbitMq/Util.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user