From 5fd0272a6deaab98a4dab0b69965d1e2b371bd68 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 17 Dec 2024 10:42:57 +0100 Subject: [PATCH] IcingaRedis: Use the new singleton instead --- library/Icingadb/Common/IcingaRedis.php | 30 +++++++------------ .../Common/ObjectInspectionDetail.php | 2 +- .../Icingadb/Redis/VolatileStateResults.php | 3 +- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/library/Icingadb/Common/IcingaRedis.php b/library/Icingadb/Common/IcingaRedis.php index a22a0f03b..0926814fd 100644 --- a/library/Icingadb/Common/IcingaRedis.php +++ b/library/Icingadb/Common/IcingaRedis.php @@ -12,9 +12,6 @@ class IcingaRedis { - /** @var static The singleton */ - protected static $instance; - /** @var Redis Connection to the Icinga Redis */ private $redis; @@ -24,15 +21,12 @@ class IcingaRedis /** * Get the singleton * + * @deprecated Use {@see Backend::getRedis()} instead * @return static */ public static function instance(): self { - if (self::$instance === null) { - self::$instance = new static(); - } - - return self::$instance; + return Backend::getRedis(); } /** @@ -40,19 +34,17 @@ public static function instance(): self * * @return bool */ - public static function isUnavailable(): bool + public function isUnavailable(): bool { - $self = self::instance(); - - if (! $self->redisUnavailable && $self->redis === null) { + if (! $this->redisUnavailable && $this->redis === null) { try { - $self->getConnection(); + $this->getConnection(); } catch (Exception $_) { // getConnection already logs the error } } - return $self->redisUnavailable; + return $this->redisUnavailable; } /** @@ -126,7 +118,7 @@ public function getConnection(): Redis */ public static function fetchHostState(array $ids, array $columns): Generator { - return self::fetchState('icinga:host:state', $ids, $columns); + return Backend::getRedis()->fetchState('icinga:host:state', $ids, $columns); } /** @@ -139,7 +131,7 @@ public static function fetchHostState(array $ids, array $columns): Generator */ public static function fetchServiceState(array $ids, array $columns): Generator { - return self::fetchState('icinga:service:state', $ids, $columns); + return Backend::getRedis()->fetchState('icinga:service:state', $ids, $columns); } /** @@ -151,10 +143,10 @@ public static function fetchServiceState(array $ids, array $columns): Generator * * @return Generator */ - protected static function fetchState(string $key, array $ids, array $columns): Generator + protected function fetchState(string $key, array $ids, array $columns): Generator { try { - $results = self::instance()->getConnection()->hmget($key, $ids); + $results = $this->getConnection()->hmget($key, $ids); } catch (Exception $_) { // The error has already been logged elsewhere return; @@ -192,7 +184,7 @@ protected static function fetchState(string $key, array $ids, array $columns): G public static function getLastIcingaHeartbeat(Redis $redis = null) { if ($redis === null) { - $redis = self::instance()->getConnection(); + $redis = Backend::getRedis()->getConnection(); } // Predis doesn't support streams (yet). diff --git a/library/Icingadb/Common/ObjectInspectionDetail.php b/library/Icingadb/Common/ObjectInspectionDetail.php index b30797b4e..db408ae92 100644 --- a/library/Icingadb/Common/ObjectInspectionDetail.php +++ b/library/Icingadb/Common/ObjectInspectionDetail.php @@ -120,7 +120,7 @@ protected function createRedisInfo(): array $title = new HtmlElement('h2', null, Text::create(t('Volatile State Details'))); try { - $json = IcingaRedis::instance()->getConnection() + $json = Backend::getRedis()->getConnection() ->hGet("icinga:{$this->object->getTableName()}:state", bin2hex($this->object->id)); } catch (Exception $e) { return [$title, sprintf('Failed to load redis data: %s', $e->getMessage())]; diff --git a/library/Icingadb/Redis/VolatileStateResults.php b/library/Icingadb/Redis/VolatileStateResults.php index 43446d681..f3459e2b9 100644 --- a/library/Icingadb/Redis/VolatileStateResults.php +++ b/library/Icingadb/Redis/VolatileStateResults.php @@ -6,6 +6,7 @@ use Icinga\Application\Benchmark; use Icinga\Module\Icingadb\Common\Auth; +use Icinga\Module\Icingadb\Common\Backend; use Icinga\Module\Icingadb\Common\IcingaRedis; use Icinga\Module\Icingadb\Model\DependencyNode; use Icinga\Module\Icingadb\Model\Host; @@ -39,7 +40,7 @@ public static function fromQuery(Query $query) { $self = parent::fromQuery($query); $self->resolver = $query->getResolver(); - $self->redisUnavailable = IcingaRedis::isUnavailable(); + $self->redisUnavailable = Backend::getRedis()->isUnavailable(); return $self; }