Skip to content

Commit

Permalink
IcingaRedis: Use the new singleton instead
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Dec 17, 2024
1 parent aa319d2 commit 8af314a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
3 changes: 3 additions & 0 deletions doc/05-Upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ The following classes have been deprecated and will be removed in a future relea
* `\Icinga\Module\Icingadb\Command\Object\ScheduleHostDowntimeCommand`
* `\Icinga\Module\Icingadb\Command\Object\ScheduleServiceDowntimeCommand`

The following methods have been deprecated and will be removed in a future release:
* `\Icinga\Module\Icingadb\Common\IcingaRedis::instance()`: Use `\Icinga\Module\Icingadb\Common\Backend::getRedis()` instead.

## Upgrading to Icinga DB Web v1.1

**Breaking Changes**
Expand Down
30 changes: 11 additions & 19 deletions library/Icingadb/Common/IcingaRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

class IcingaRedis
{
/** @var static The singleton */
protected static $instance;

/** @var Redis Connection to the Icinga Redis */
private $redis;

Expand All @@ -24,35 +21,30 @@ 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();
}

/**
* Get whether Redis is unavailable
*
* @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;
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion library/Icingadb/Common/ObjectInspectionDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())];
Expand Down
3 changes: 2 additions & 1 deletion library/Icingadb/Redis/VolatileStateResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 8af314a

Please sign in to comment.