Skip to content

Commit 1a23a6b

Browse files
committed
IcingaRedis: Use the new singleton instead
1 parent edbafbd commit 1a23a6b

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

doc/05-Upgrading.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ The following classes have been deprecated and will be removed in a future relea
1212
* `\Icinga\Module\Icingadb\Command\Object\ScheduleHostDowntimeCommand`
1313
* `\Icinga\Module\Icingadb\Command\Object\ScheduleServiceDowntimeCommand`
1414

15+
The following methods have been deprecated and will be removed in a future release:
16+
* `\Icinga\Module\Icingadb\Common\IcingaRedis::instance()`: Use `\Icinga\Module\Icingadb\Common\Backend::getRedis()` instead.
17+
1518
## Upgrading to Icinga DB Web v1.1
1619

1720
**Breaking Changes**

library/Icingadb/Common/IcingaRedis.php

+11-19
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212

1313
class IcingaRedis
1414
{
15-
/** @var static The singleton */
16-
protected static $instance;
17-
1815
/** @var Redis Connection to the Icinga Redis */
1916
private $redis;
2017

@@ -24,35 +21,30 @@ class IcingaRedis
2421
/**
2522
* Get the singleton
2623
*
24+
* @deprecated Use {@see Backend::getRedis()} instead
2725
* @return static
2826
*/
2927
public static function instance(): self
3028
{
31-
if (self::$instance === null) {
32-
self::$instance = new static();
33-
}
34-
35-
return self::$instance;
29+
return Backend::getRedis();
3630
}
3731

3832
/**
3933
* Get whether Redis is unavailable
4034
*
4135
* @return bool
4236
*/
43-
public static function isUnavailable(): bool
37+
public function isUnavailable(): bool
4438
{
45-
$self = self::instance();
46-
47-
if (! $self->redisUnavailable && $self->redis === null) {
39+
if (! $this->redisUnavailable && $this->redis === null) {
4840
try {
49-
$self->getConnection();
41+
$this->getConnection();
5042
} catch (Exception $_) {
5143
// getConnection already logs the error
5244
}
5345
}
5446

55-
return $self->redisUnavailable;
47+
return $this->redisUnavailable;
5648
}
5749

5850
/**
@@ -126,7 +118,7 @@ public function getConnection(): Redis
126118
*/
127119
public static function fetchHostState(array $ids, array $columns): Generator
128120
{
129-
return self::fetchState('icinga:host:state', $ids, $columns);
121+
return Backend::getRedis()->fetchState('icinga:host:state', $ids, $columns);
130122
}
131123

132124
/**
@@ -139,7 +131,7 @@ public static function fetchHostState(array $ids, array $columns): Generator
139131
*/
140132
public static function fetchServiceState(array $ids, array $columns): Generator
141133
{
142-
return self::fetchState('icinga:service:state', $ids, $columns);
134+
return Backend::getRedis()->fetchState('icinga:service:state', $ids, $columns);
143135
}
144136

145137
/**
@@ -151,10 +143,10 @@ public static function fetchServiceState(array $ids, array $columns): Generator
151143
*
152144
* @return Generator
153145
*/
154-
protected static function fetchState(string $key, array $ids, array $columns): Generator
146+
protected function fetchState(string $key, array $ids, array $columns): Generator
155147
{
156148
try {
157-
$results = self::instance()->getConnection()->hmget($key, $ids);
149+
$results = $this->getConnection()->hmget($key, $ids);
158150
} catch (Exception $_) {
159151
// The error has already been logged elsewhere
160152
return;
@@ -192,7 +184,7 @@ protected static function fetchState(string $key, array $ids, array $columns): G
192184
public static function getLastIcingaHeartbeat(Redis $redis = null)
193185
{
194186
if ($redis === null) {
195-
$redis = self::instance()->getConnection();
187+
$redis = Backend::getRedis()->getConnection();
196188
}
197189

198190
// Predis doesn't support streams (yet).

library/Icingadb/Common/ObjectInspectionDetail.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected function createRedisInfo(): array
120120
$title = new HtmlElement('h2', null, Text::create(t('Volatile State Details')));
121121

122122
try {
123-
$json = IcingaRedis::instance()->getConnection()
123+
$json = Backend::getRedis()->getConnection()
124124
->hGet("icinga:{$this->object->getTableName()}:state", bin2hex($this->object->id));
125125
} catch (Exception $e) {
126126
return [$title, sprintf('Failed to load redis data: %s', $e->getMessage())];

library/Icingadb/Redis/VolatileStateResults.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Icinga\Application\Benchmark;
88
use Icinga\Module\Icingadb\Common\Auth;
9+
use Icinga\Module\Icingadb\Common\Backend;
910
use Icinga\Module\Icingadb\Common\IcingaRedis;
1011
use Icinga\Module\Icingadb\Model\DependencyNode;
1112
use Icinga\Module\Icingadb\Model\Host;
@@ -39,7 +40,7 @@ public static function fromQuery(Query $query)
3940
{
4041
$self = parent::fromQuery($query);
4142
$self->resolver = $query->getResolver();
42-
$self->redisUnavailable = IcingaRedis::isUnavailable();
43+
$self->redisUnavailable = Backend::getRedis()->isUnavailable();
4344

4445
return $self;
4546
}

0 commit comments

Comments
 (0)