blob: 08b188f65545375516325adc899bf6f0bf149eef (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
<?php
namespace gipfl\IcingaCliDaemon;
use Icinga\Application\Config;
use InvalidArgumentException;
use React\EventLoop\LoopInterface;
/**
* DbResourceConfigWatch
*
* Checks every $interval = 3 seconds for changed DB resource configuration.
* Notifies registered callbacksin case this happens.
*/
class DbResourceConfigWatch
{
/** @var string */
protected $configFile;
/** @var string */
protected $resourceConfigFile;
/** @var string|null */
protected $dbResourceName;
/** @var array|null|false It's false on initialization to trigger */
protected $resourceConfig = false;
/** @var int|float */
protected $interval = 3;
/** @var callable[] */
protected $callbacks = [];
/**
* @param string $dbResourceName
* @return DbResourceConfigWatch
*/
public static function name($dbResourceName)
{
$self = new static();
$self->dbResourceName = $dbResourceName;
return $self;
}
/**
* @param string $moduleName
* @return DbResourceConfigWatch
*/
public static function module($moduleName)
{
$self = new static();
$self->configFile = Config::module($moduleName)->getConfigFile();
$self->resourceConfigFile = Config::app('resources')->getConfigFile();
return $self;
}
/**
* @param int|float $interval
* @return $this
*/
public function setInterval($interval)
{
if (! \is_int($interval) && ! \is_float($interval)) {
throw new InvalidArgumentException(
'$interval needs to be either int or float'
);
}
$this->interval = $interval;
return $this;
}
/**
* @param callable $callable
* @return $this
*/
public function notify($callable)
{
if (! \is_callable($callable)) {
throw new InvalidArgumentException('$callable needs to be callable');
}
$this->callbacks[] = $callable;
return $this;
}
/**
* @param LoopInterface $loop
*/
public function run(LoopInterface $loop)
{
$check = function () {
$this->checkForFreshConfig();
};
$loop->addPeriodicTimer($this->interval, $check);
$loop->futureTick($check);
}
protected function checkForFreshConfig()
{
if ($this->configHasBeenChanged()) {
$this->emitNewConfig($this->resourceConfig);
}
}
protected function emitNewConfig($config)
{
foreach ($this->callbacks as $callback) {
$callback($config);
}
}
protected function getResourceName()
{
if ($this->dbResourceName) {
return $this->dbResourceName;
} else {
return $this->loadDbResourceName();
}
}
protected function loadDbResourceName()
{
$parsed = @\parse_ini_file($this->configFile, true);
if (isset($parsed['db']['resource'])) {
return $parsed['db']['resource'];
} else {
return null;
}
}
protected function loadDbConfigFromDisk($name)
{
if ($name === null) {
return null;
}
$parsed = @\parse_ini_file($this->resourceConfigFile, true);
if (isset($parsed[$name])) {
$section = $parsed[$name];
\ksort($section);
return $section;
} else {
return null;
}
}
protected function configHasBeenChanged()
{
$resource = $this->loadDbConfigFromDisk($this->loadDbResourceName());
if ($resource !== $this->resourceConfig) {
$this->resourceConfig = $resource;
return true;
} else {
return false;
}
}
}
|