From f72cfe756affa98de399e238454b755b45f9138f Mon Sep 17 00:00:00 2001 From: GitHub Date: Sun, 29 Sep 2024 21:01:52 +0000 Subject: [PATCH] [viera-connector-homekit-connector-bridge] Prepared viera connector to homekit connector bridge (#306) --- src/DI/ApplicationExtension.php | 16 ++++---- src/Events/DbTransactionFinished.php | 31 +++++++++++++++ src/Events/DbTransactionStarted.php | 31 +++++++++++++++ src/Helpers/Database.php | 29 ++++++-------- src/Helpers/Logger.php | 14 ++++--- src/Subscribers/EventLoop.php | 59 ++++++++++++++++++++++++++++ src/Utilities/EventLoopStatus.php | 45 +++++++++++++++++++++ 7 files changed, 193 insertions(+), 32 deletions(-) create mode 100644 src/Events/DbTransactionFinished.php create mode 100644 src/Events/DbTransactionStarted.php create mode 100644 src/Subscribers/EventLoop.php create mode 100644 src/Utilities/EventLoopStatus.php diff --git a/src/DI/ApplicationExtension.php b/src/DI/ApplicationExtension.php index c250aba..1088857 100644 --- a/src/DI/ApplicationExtension.php +++ b/src/DI/ApplicationExtension.php @@ -157,10 +157,7 @@ public function loadConfiguration(): void */ if ($configuration->logging->console->enabled) { - $builder->addDefinition( - $this->prefix('subscribers.console'), - new DI\Definitions\ServiceDefinition(), - ) + $builder->addDefinition($this->prefix('subscribers.console'), new DI\Definitions\ServiceDefinition()) ->setType(Subscribers\Console::class) ->setArguments([ 'handler' => $consoleHandler, @@ -176,15 +173,15 @@ public function loadConfiguration(): void ->setType(Subscribers\EntityDiscriminator::class); } + $builder->addDefinition($this->prefix('subscribers.eventLoop'), new DI\Definitions\ServiceDefinition()) + ->setType(Subscribers\EventLoop::class); + /** * SENTRY ISSUES LOGGER */ if (interface_exists('\Sentry\ClientInterface')) { - $builder->addDefinition( - $this->prefix('helpers.sentry'), - new DI\Definitions\ServiceDefinition(), - ) + $builder->addDefinition($this->prefix('helpers.sentry'), new DI\Definitions\ServiceDefinition()) ->setType(Helpers\Sentry::class); } @@ -244,6 +241,9 @@ public function loadConfiguration(): void $builder->addDefinition($this->prefix('utilities.doctrineDateProvider'), new DI\Definitions\ServiceDefinition()) ->setType(Utilities\DateTimeProvider::class); + + $builder->addDefinition($this->prefix('utilities.eventLoop.status'), new DI\Definitions\ServiceDefinition()) + ->setType(Utilities\EventLoopStatus::class); } /** diff --git a/src/Events/DbTransactionFinished.php b/src/Events/DbTransactionFinished.php new file mode 100644 index 0000000..be1dd88 --- /dev/null +++ b/src/Events/DbTransactionFinished.php @@ -0,0 +1,31 @@ + + * @package FastyBird:ApplicationLibrary! + * @subpackage Events + * @since 1.0.0 + * + * @date 11.09.24 + */ + +namespace FastyBird\Library\Application\Events; + +use Symfony\Contracts\EventDispatcher; + +/** + * Database transaction finished event + * + * @package FastyBird:ApplicationLibrary! + * @subpackage Events + * + * @author Adam Kadlec + */ +class DbTransactionFinished extends EventDispatcher\Event +{ + +} diff --git a/src/Events/DbTransactionStarted.php b/src/Events/DbTransactionStarted.php new file mode 100644 index 0000000..fb8c172 --- /dev/null +++ b/src/Events/DbTransactionStarted.php @@ -0,0 +1,31 @@ + + * @package FastyBird:ApplicationLibrary! + * @subpackage Events + * @since 1.0.0 + * + * @date 11.09.24 + */ + +namespace FastyBird\Library\Application\Events; + +use Symfony\Contracts\EventDispatcher; + +/** + * Database transaction started event + * + * @package FastyBird:ApplicationLibrary! + * @subpackage Events + * + * @author Adam Kadlec + */ +class DbTransactionStarted extends EventDispatcher\Event +{ + +} diff --git a/src/Helpers/Database.php b/src/Helpers/Database.php index fb7aeab..06eede7 100644 --- a/src/Helpers/Database.php +++ b/src/Helpers/Database.php @@ -18,9 +18,11 @@ use Doctrine\DBAL; use Doctrine\ORM; use Doctrine\Persistence; +use FastyBird\Library\Application\Events; use FastyBird\Library\Application\Exceptions; use FastyBird\Library\Metadata\Types as MetadataTypes; use Nette; +use Psr\EventDispatcher; use Psr\Log; use Throwable; use function gc_collect_cycles; @@ -41,6 +43,7 @@ class Database public function __construct( private readonly Persistence\ManagerRegistry|null $managerRegistry = null, + private readonly EventDispatcher\EventDispatcherInterface|null $dispatcher = null, private readonly Log\LoggerInterface $logger = new Log\NullLogger(), ) { @@ -95,6 +98,8 @@ public function transaction(callable $callback) // Start transaction connection to the database $connection->beginTransaction(); + $this->dispatcher?->dispatch(new Events\DbTransactionStarted()); + $result = $callback(); if ($connection->isRollbackOnly()) { @@ -106,6 +111,8 @@ public function transaction(callable $callback) $connection->commit(); } + $this->dispatcher?->dispatch(new Events\DbTransactionFinished()); + return $result; } catch (Throwable $ex) { // Revert all changes when error occur @@ -137,6 +144,8 @@ public function beginTransaction(): void $this->pingAndReconnect(); $connection->beginTransaction(); + + $this->dispatcher?->dispatch(new Events\DbTransactionStarted()); } catch (Throwable $ex) { throw new Exceptions\InvalidState( 'An error occurred: ' . $ex->getMessage(), @@ -165,6 +174,8 @@ public function commitTransaction(): void } else { // Commit all changes into database $connection->commit(); + + $this->dispatcher?->dispatch(new Events\DbTransactionFinished()); } } catch (Throwable $ex) { // Revert all changes when error occur @@ -234,24 +245,6 @@ public function clear(): void continue; } - // Flushing and then clearing Doctrine's entity manager allows - // for more memory to be released by PHP - try { - if ($manager->isOpen()) { - $manager->flush(); - } - } catch (Throwable $ex) { - // Log caught exception - $this->logger->error( - 'An unhandled error occurred during flushing entity manager', - [ - 'source' => MetadataTypes\Sources\Module::NOT_SPECIFIED, - 'type' => 'helper', - 'exception' => Logger::buildException($ex), - ], - ); - } - try { $manager->getConnection()->close(); } catch (Throwable $ex) { diff --git a/src/Helpers/Logger.php b/src/Helpers/Logger.php index 4b84f14..4e5f59b 100644 --- a/src/Helpers/Logger.php +++ b/src/Helpers/Logger.php @@ -47,13 +47,15 @@ class Logger /** * @return array> */ - public static function buildException(Throwable $ex): array + public static function buildException(Throwable $ex, bool $render = true): array { - try { - $blueScreen = new Tracy\BlueScreen(); - $blueScreen->renderToFile($ex, self::getExceptionFile($ex)); - } catch (Throwable) { - // Blue scree could not be saved + if ($render) { + try { + $blueScreen = new Tracy\BlueScreen(); + $blueScreen->renderToFile($ex, self::getExceptionFile($ex)); + } catch (Throwable) { + // Blue scree could not be saved + } } return self::processAllExceptions($ex); diff --git a/src/Subscribers/EventLoop.php b/src/Subscribers/EventLoop.php new file mode 100644 index 0000000..909aff2 --- /dev/null +++ b/src/Subscribers/EventLoop.php @@ -0,0 +1,59 @@ + + * @package FastyBird:ApplicationLibrary! + * @subpackage Subscribers + * @since 1.0.0 + * + * @date 11.09.24 + */ + +namespace FastyBird\Library\Application\Subscribers; + +use FastyBird\Library\Application\Events; +use FastyBird\Library\Application\Utilities; +use Nette; +use Symfony\Component\EventDispatcher; + +/** + * Event loop events + * + * @package FastyBird:ApplicationLibrary! + * @subpackage Subscribers + * + * @author Adam Kadlec + */ +final class EventLoop implements EventDispatcher\EventSubscriberInterface +{ + + use Nette\SmartObject; + + public function __construct(private readonly Utilities\EventLoopStatus $eventLoopStatus) + { + } + + public static function getSubscribedEvents(): array + { + return [ + Events\EventLoopStarted::class => 'loopStarted', + Events\EventLoopStopped::class => 'loopStopped', + Events\EventLoopStopping::class => 'loopStopped', + ]; + } + + public function loopStarted(): void + { + $this->eventLoopStatus->setStatus(true); + } + + public function loopStopped(): void + { + $this->eventLoopStatus->setStatus(false); + } + +} diff --git a/src/Utilities/EventLoopStatus.php b/src/Utilities/EventLoopStatus.php new file mode 100644 index 0000000..7a80120 --- /dev/null +++ b/src/Utilities/EventLoopStatus.php @@ -0,0 +1,45 @@ + + * @package FastyBird:ApplicationLibrary! + * @subpackage Utilities + * @since 1.0.0 + * + * @date 12.09.24 + */ + +namespace FastyBird\Library\Application\Utilities; + +use Nette; + +/** + * Event loop status helper + * + * @package FastyBird:ApplicationLibrary! + * @subpackage Utilities + * + * @author Adam Kadlec + */ +final class EventLoopStatus +{ + + use Nette\SmartObject; + + private bool $status = false; + + public function setStatus(bool $status): void + { + $this->status = $status; + } + + public function isRunning(): bool + { + return $this->status; + } + +}