From bf0d11ff37f091b5fc215e5a1bcf1dbd7439e730 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 28 Jun 2022 09:56:58 +0200 Subject: [PATCH] VolatileStateResults: Don't fail completely if redis is unavailable --- .../Icingadb/Redis/VolatileStateResults.php | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/library/Icingadb/Redis/VolatileStateResults.php b/library/Icingadb/Redis/VolatileStateResults.php index dd3695e9..abcf1f14 100644 --- a/library/Icingadb/Redis/VolatileStateResults.php +++ b/library/Icingadb/Redis/VolatileStateResults.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Redis; +use Exception; use Generator; use Icinga\Application\Benchmark; use Icinga\Module\Icingadb\Common\IcingaRedis; @@ -12,6 +13,7 @@ use Icinga\Module\Icingadb\Model\Service; use ipl\Orm\Query; use ipl\Orm\Resolver; use ipl\Orm\ResultSet; +use Predis\Client; use RuntimeException; class VolatileStateResults extends ResultSet @@ -19,6 +21,9 @@ class VolatileStateResults extends ResultSet /** @var Resolver */ private $resolver; + /** @var Client */ + private $redis; + /** @var bool Whether Redis updates were applied */ private $updatesApplied = false; @@ -27,12 +32,28 @@ class VolatileStateResults extends ResultSet $self = parent::fromQuery($query); $self->resolver = $query->getResolver(); + try { + $self->redis = IcingaRedis::instance()->getConnection(); + } catch (Exception $e) { + // The error has already been logged + } + return $self; } + /** + * Get whether Redis is unavailable + * + * @return bool + */ + public function isRedisUnavailable(): bool + { + return $this->redis === null; + } + public function current() { - if (! $this->updatesApplied && ! $this->isCacheDisabled) { + if ($this->redis && ! $this->updatesApplied && ! $this->isCacheDisabled) { $this->rewind(); } @@ -41,7 +62,7 @@ class VolatileStateResults extends ResultSet public function key(): int { - if (! $this->updatesApplied && ! $this->isCacheDisabled) { + if ($this->redis && ! $this->updatesApplied && ! $this->isCacheDisabled) { $this->rewind(); } @@ -50,7 +71,7 @@ class VolatileStateResults extends ResultSet public function rewind(): void { - if (! $this->updatesApplied && ! $this->isCacheDisabled) { + if ($this->redis && ! $this->updatesApplied && ! $this->isCacheDisabled) { $this->updatesApplied = true; $this->advance(); @@ -126,7 +147,7 @@ class VolatileStateResults extends ResultSet protected function fetchStates(string $key, array $ids, array $keys): Generator { - $results = IcingaRedis::instance()->getConnection()->hmget($key, $ids); + $results = $this->redis->hmget($key, $ids); foreach ($results as $i => $json) { if ($json !== null) { $data = json_decode($json, true);