EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Ticker and events
Yurii24a8aa1931a0
diff --git a/application/Espo/Core/Acl/Cache/Clearer.php b/application/Espo/Core/Acl/Cache/Clearer.phpindex a6e75a6507..0e18376a27 100644--- a/application/Espo/Core/Acl/Cache/Clearer.php+++ b/application/Espo/Core/Acl/Cache/Clearer.php@@ -29,6 +29,9 @@namespace Espo\Core\Acl\Cache;+use Espo\Core\Acl\Events\InvalidatePortalUserCache;+use Espo\Core\Acl\Events\InvalidateUserCache;+use Espo\Core\Utils\Event\EventDispatcher;use Espo\Core\Utils\File\Manager as FileManager;use Espo\Core\Utils\System\SystemState;use Espo\Entities\Portal;@@ -37,7 +40,7 @@ use Espo\ORM\EntityManager;use Espo\ORM\Name\Attribute;/**- * @todo Clear cache in AclManager.+ * @todo Clear loaded data in AclManager.*/class Clearer{@@ -45,6 +48,7 @@ class Clearerprivate FileManager $fileManager,private EntityManager $entityManager,private SystemState $systemState,+ private EventDispatcher $eventDispatcher,) {}public function clearForAllInternalUsers(): void@@ -76,7 +80,7 @@ class Clearer$this->fileManager->remove('data/cache/application/acl/' . $part);$this->fileManager->remove('data/cache/application/aclMap/' . $part);- $this->bumpSystemStateVersionNumber();+ $this->eventDispatcher->dispatch(new InvalidateUserCache($user->getId()));}private function clearForPortalUser(User $user): void@@ -91,9 +95,14 @@ class Clearer$this->fileManager->remove('data/cache/application/aclPortal/' . $part);$this->fileManager->remove('data/cache/application/aclPortalMap/' . $part);- }- $this->bumpSystemStateVersionNumber();+ $event = new InvalidatePortalUserCache(+ userId: $user->getId(),+ portalId: $portal->getId(),+ );++ $this->eventDispatcher->dispatch($event);+ }}private function bumpSystemStateVersionNumber(): voiddiff --git a/application/Espo/Core/Acl/Events/InvalidatePortalUserCache.php b/application/Espo/Core/Acl/Events/InvalidatePortalUserCache.phpnew file mode 100644index 0000000000..535a88cb6e--- /dev/null+++ b/application/Espo/Core/Acl/Events/InvalidatePortalUserCache.php@@ -0,0 +1,73 @@+<?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\Acl\Events;++use Espo\Core\Utils\Event\CrossInstanceEvent;+use stdClass;+use UnexpectedValueException;++/**+ * @since 10.1.0+ */+class InvalidatePortalUserCache implements CrossInstanceEvent+{+ public function __construct(+ public string $userId,+ public string $portalId,+ ) {}++ public static function fromRaw(stdClass $payload): self+ {+ $userId = $payload->userId ?? null;++ if (!is_string($userId)) {+ throw new UnexpectedValueException("No user ID.");+ }++ $portalId = $payload->portalId ?? null;++ if (!is_string($portalId)) {+ throw new UnexpectedValueException("No portal ID.");+ }++ return new self(+ userId: $userId,+ portalId: $portalId,+ );+ }++ public function toRaw(): stdClass+ {+ return (object) [+ 'userId' => $this->userId,+ 'portalId' => $this->portalId,+ ];+ }+}diff --git a/application/Espo/Core/Acl/Events/InvalidateUserCache.php b/application/Espo/Core/Acl/Events/InvalidateUserCache.phpnew file mode 100644index 0000000000..ee4d009fc2--- /dev/null+++ b/application/Espo/Core/Acl/Events/InvalidateUserCache.php@@ -0,0 +1,64 @@+<?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\Acl\Events;++use Espo\Core\Utils\Event\CrossInstanceEvent;+use stdClass;+use UnexpectedValueException;++/**+ * @since 10.1.0+ */+readonly class InvalidateUserCache implements CrossInstanceEvent+{+ public function __construct(+ public string $userId,+ ) {}++ public static function fromRaw(stdClass $payload): self+ {+ $userId = $payload->userId ?? null;++ if (!is_string($userId)) {+ throw new UnexpectedValueException();+ }++ return new self(+ userId: $userId,+ );+ }++ public function toRaw(): stdClass+ {+ return (object) [+ 'userId' => $this->userId,+ ];+ }+}diff --git a/application/Espo/Core/Currency/InternalRatesProvider.php b/application/Espo/Core/Currency/InternalRatesProvider.phpindex efaa58ee66..65a812a4e0 100644--- a/application/Espo/Core/Currency/InternalRatesProvider.php+++ b/application/Espo/Core/Currency/InternalRatesProvider.php@@ -32,6 +32,8 @@ namespace Espo\Core\Currency;use Espo\Core\Field\Date;use Espo\Core\Utils\Cache\DataCacheAccess;use Espo\Core\Utils\DateTime;+use Espo\Core\Utils\Event\EventDispatcher;+use Espo\Tools\Currency\Events\CurrencyRateUpdate;use LogicException;use RuntimeException;use stdClass;@@ -53,6 +55,7 @@ class InternalRatesProviderprivate DateTime $dateTime,private InternalRateEntryProvider $rateEntryProvider,private DataCacheAccess $dataCacheAccess,+ private EventDispatcher $eventDispatcher,) {$this->dataCacheAccess->init(key: $this->cacheKey,@@ -73,6 +76,10 @@ class InternalRatesProviderreturn $date === $this->today->toString();},);++ $this->eventDispatcher->subscribe(CurrencyRateUpdate::class, function () {+ $this->dataCacheAccess->reset();+ });}/**diff --git a/application/Espo/Core/Job/Processing/Exceptions/TickFailure.php b/application/Espo/Core/Job/Processing/Exceptions/TickFailure.phpnew file mode 100644index 0000000000..85cd7592a2--- /dev/null+++ b/application/Espo/Core/Job/Processing/Exceptions/TickFailure.php@@ -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+{}diff --git a/application/Espo/Core/Job/Processing/RabbitMq/Consumer.php b/application/Espo/Core/Job/Processing/RabbitMq/Consumer.phpindex c9a2e110a5..ab047f5335 100644--- a/application/Espo/Core/Job/Processing/RabbitMq/Consumer.php+++ b/application/Espo/Core/Job/Processing/RabbitMq/Consumer.php@@ -30,10 +30,12 @@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;@@ -55,6 +57,7 @@ class Consumer implements ConsumerInterfaceprivate Log $log,private JobProvider $jobProvider,private ExitPolicy $exitPolicy,+ private Ticker $ticker,) {}public function start(Params $params): void@@ -75,6 +78,14 @@ class Consumer implements ConsumerInterface$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) {diff --git a/application/Espo/Core/Job/Processing/Util/ExitPolicy.php b/application/Espo/Core/Job/Processing/Util/ExitPolicy.phpindex 68a8d3793d..b656ad153a 100644--- a/application/Espo/Core/Job/Processing/Util/ExitPolicy.php+++ b/application/Espo/Core/Job/Processing/Util/ExitPolicy.php@@ -31,7 +31,6 @@ namespace Espo\Core\Job\Processing\Util;use Espo\Core\Utils\Config;use Espo\Core\Utils\Config\StateConfig;-use Espo\Core\Utils\Event\EventDispatcherTransport;class ExitPolicy{@@ -40,15 +39,12 @@ class ExitPolicypublic function __construct(private StateConfig $stateConfig,private Config\StateConfigDirect $stateConfigDirect,- private EventDispatcherTransport $eventDispatcherTransport,) {$this->cacheTimestamp = $this->stateConfig->getCacheTimestamp();}public function toExit(): bool{- return- $this->cacheTimestamp !== $this->stateConfigDirect->getCacheTimestamp() ||- $this->eventDispatcherTransport->shouldReconnect();+ return $this->cacheTimestamp !== $this->stateConfigDirect->getCacheTimestamp();}}diff --git a/application/Espo/Core/Job/Processing/Util/Ticker.php b/application/Espo/Core/Job/Processing/Util/Ticker.phpnew file mode 100644index 0000000000..9b203b0556--- /dev/null+++ b/application/Espo/Core/Job/Processing/Util/Ticker.php@@ -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\EventDispatcherTransport;+use Espo\Core\Utils\Event\Exceptions\TransportNotConnected;++class Ticker+{+ public function __construct(+ private EventDispatcherTransport $transport+ ) {}++ /**+ * @throws TickFailure+ */+ public function tick(): void+ {+ try {+ $this->transport->tick();+ } catch (TransportNotConnected $e) {+ throw new TickFailure(previous: $e);+ }+ }+}diff --git a/application/Espo/Core/Utils/Cache/DataCacheAccess.php b/application/Espo/Core/Utils/Cache/DataCacheAccess.phpindex ac5588743c..d91f155150 100644--- a/application/Espo/Core/Utils/Cache/DataCacheAccess.php+++ b/application/Espo/Core/Utils/Cache/DataCacheAccess.php@@ -61,8 +61,6 @@ class DataCacheAccess) {}/**- * @todo Event clearing loaded data.- ** @param Closure(): T $loader* @param (Closure(T): bool)|null $validityChecker*/diff --git a/application/Espo/Core/Utils/Event/BypassEventDispatcherTransport.php b/application/Espo/Core/Utils/Event/BypassEventDispatcherTransport.phpindex 9e635da90e..a1e12a3050 100644--- a/application/Espo/Core/Utils/Event/BypassEventDispatcherTransport.php+++ b/application/Espo/Core/Utils/Event/BypassEventDispatcherTransport.php@@ -39,8 +39,6 @@ class BypassEventDispatcherTransport implements EventDispatcherTransportpublic function dispatch(Envelope $envelope): void{}- public function shouldReconnect(): bool- {- return false;- }+ public function tick(): void+ {}}diff --git a/application/Espo/Core/Utils/Event/CrossInstanceEvent.php b/application/Espo/Core/Utils/Event/CrossInstanceEvent.phpindex 63ab30a0d3..a81edb4a82 100644--- a/application/Espo/Core/Utils/Event/CrossInstanceEvent.php+++ b/application/Espo/Core/Utils/Event/CrossInstanceEvent.php@@ -33,7 +33,7 @@ use stdClass;interface CrossInstanceEvent extends Event{- public static function fromRaw(stdClass $payload): static;+ public static function fromRaw(stdClass $payload): self;public function toRaw(): stdClass;}diff --git a/application/Espo/Core/Utils/Event/EventDispatcherTransport.php b/application/Espo/Core/Utils/Event/EventDispatcherTransport.phpindex 4516c6b988..6242a1496f 100644--- a/application/Espo/Core/Utils/Event/EventDispatcherTransport.php+++ b/application/Espo/Core/Utils/Event/EventDispatcherTransport.php@@ -30,6 +30,7 @@namespace Espo\Core\Utils\Event;use Closure;+use Espo\Core\Utils\Event\Exceptions\TransportNotConnected;/*** @since 10.1.0@@ -43,5 +44,8 @@ interface EventDispatcherTransportpublic function dispatch(Envelope $envelope): void;- public function shouldReconnect(): bool;+ /**+ * @throws TransportNotConnected+ */+ public function tick(): void;}diff --git a/application/Espo/Core/Utils/Event/Exceptions/TransportNotConnected.php b/application/Espo/Core/Utils/Event/Exceptions/TransportNotConnected.phpnew file mode 100644index 0000000000..9fe8c30fd5--- /dev/null+++ b/application/Espo/Core/Utils/Event/Exceptions/TransportNotConnected.php@@ -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\Utils\Event\Exceptions;++use Exception;++class TransportNotConnected extends Exception+{}diff --git a/application/Espo/Tools/Currency/Events/CurrencyRateUpdate.php b/application/Espo/Tools/Currency/Events/CurrencyRateUpdate.phpnew file mode 100644index 0000000000..6602789ee3--- /dev/null+++ b/application/Espo/Tools/Currency/Events/CurrencyRateUpdate.php@@ -0,0 +1,49 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Tools\Currency\Events;++use Espo\Core\Utils\Event\CrossInstanceEvent;+use stdClass;++/**+ * @since 10.1.0+ */+readonly class CurrencyRateUpdate implements CrossInstanceEvent+{+ public function toRaw(): stdClass+ {+ return (object) [];+ }++ public static function fromRaw(stdClass $payload): self+ {+ return new self();+ }+}diff --git a/application/Espo/Tools/Currency/SyncManager.php b/application/Espo/Tools/Currency/SyncManager.phpindex efa8d26b11..00a047d302 100644--- a/application/Espo/Tools/Currency/SyncManager.php+++ b/application/Espo/Tools/Currency/SyncManager.php@@ -33,11 +33,12 @@ use Espo\Core\Currency\ConfigDataProvider;use Espo\Core\Utils\Config\ConfigWriter;use Espo\Core\Utils\Config\SystemConfig;use Espo\Core\Utils\DataCache;-use Espo\Core\Utils\System\SystemState;+use Espo\Core\Utils\Event\EventDispatcher;use Espo\Entities\CurrencyRecord;use Espo\ORM\EntityManager;use Espo\ORM\Name\Attribute;use Espo\ORM\Query\UpdateBuilder;+use Espo\Tools\Currency\Events\CurrencyRateUpdate;use Espo\Tools\Currency\Exceptions\NotEnabled;/**@@ -55,7 +56,7 @@ class SyncManagerprivate RateEntryProvider $rateEntryProvider,private DataCache $dataCache,private SystemConfig $systemConfig,- private SystemState $systemState,+ private EventDispatcher $eventDispatcher,) {}public function sync(): void@@ -129,7 +130,7 @@ class SyncManager$this->syncToConfigInTransaction();});- $this->getBumpVersionNumber();+ $this->dispatchUpdateEvent();$this->clearCache();}@@ -157,7 +158,7 @@ class SyncManager$this->configWriter->set('currencyRates', $rates);$this->configWriter->save();- $this->getBumpVersionNumber();+ $this->dispatchUpdateEvent();$this->clearCache();}@@ -170,8 +171,8 @@ class SyncManager$this->dataCache->clear($this->cacheKey);}- private function getBumpVersionNumber(): void+ private function dispatchUpdateEvent(): void{- $this->systemState->bumpVersionNumber();+ $this->eventDispatcher->dispatch(new CurrencyRateUpdate());}}diff --git a/tests/unit/Espo/Core/Utils/Event/TestCiEvent1.php b/tests/unit/Espo/Core/Utils/Event/TestCiEvent1.phpindex 9e89933c60..88b47fb1d9 100644--- a/tests/unit/Espo/Core/Utils/Event/TestCiEvent1.php+++ b/tests/unit/Espo/Core/Utils/Event/TestCiEvent1.php@@ -38,7 +38,7 @@ class TestCiEvent1 implements CrossInstanceEventpublic string $value,) {}- public static function fromRaw(stdClass $payload): static+ public static function fromRaw(stdClass $payload): self{return new self($payload->value ?? '');}diff --git a/tests/unit/Espo/Core/Utils/Event/TestCiEvent2.php b/tests/unit/Espo/Core/Utils/Event/TestCiEvent2.phpindex 083164e09c..40a2b58c0f 100644--- a/tests/unit/Espo/Core/Utils/Event/TestCiEvent2.php+++ b/tests/unit/Espo/Core/Utils/Event/TestCiEvent2.php@@ -34,7 +34,7 @@ use stdClass;class TestCiEvent2 implements CrossInstanceEvent{- public static function fromRaw(stdClass $payload): static+ public static function fromRaw(stdClass $payload): self{return new self();}diff --git a/tests/unit/Espo/Core/Utils/Event/TestTransport.php b/tests/unit/Espo/Core/Utils/Event/TestTransport.phpindex be217ad9d5..675115dfe4 100644--- a/tests/unit/Espo/Core/Utils/Event/TestTransport.php+++ b/tests/unit/Espo/Core/Utils/Event/TestTransport.php@@ -52,10 +52,8 @@ class TestTransport implements EventDispatcherTransportpublic function dispatch(Envelope $envelope): void{}- public function shouldReconnect(): bool- {- return true;- }+ public function tick(): void+ {}public function dispatchForTest(string $eventClassName, stdClass $payload): void{