-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathDbCommand.php
More file actions
69 lines (63 loc) · 2.26 KB
/
Copy pathDbCommand.php
File metadata and controls
69 lines (63 loc) · 2.26 KB
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
<?php
// SPDX-FileCopyrightText: 2021 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Vspheredb\Clicommands;
use Exception;
use gipfl\Cli\Process;
use gipfl\Log\IcingaWeb\IcingaLogger;
use gipfl\Log\Logger;
use gipfl\Log\Writer\JsonRpcConnectionWriter;
use gipfl\Protocol\JsonRpc\Handler\NamespacedPacketHandler;
use gipfl\Protocol\JsonRpc\JsonRpcConnection;
use gipfl\Protocol\NetString\StreamWrapper;
use Icinga\Module\Vspheredb\Daemon\RpcNamespace\DbRunner;
use React\EventLoop\LoopInterface;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;
/**
* vSphereDB child process, our (synchronous) DB connection
*/
class DbCommand extends Command
{
public function runAction()
{
if (!$this->isRpc()) {
$this->fail('This is an internal command and should not be called directly');
}
$this->loop()->futureTick(function () {
$this->logger = $this->prepareLogger();
try {
Process::setTitle('Icinga::vSphereDB::DB::idle');
$handler = new NamespacedPacketHandler();
$handler->registerNamespace('db', new DbRunner($this->logger, $this->loop()));
$rpc = $this->prepareJsonRpc($this->loop(), $handler);
$this->logger->addWriter(new JsonRpcConnectionWriter($rpc));
} catch (Exception $e) {
$this->logger->error($e->getMessage());
// This allows to flush streams, especially pending log messages
$this->loop()->addTimer(0.1, function () {
$this->stopMainLoop();
exit(1);
});
}
});
$this->loop()->run();
}
protected function prepareLogger()
{
$logger = new Logger();
$this->eventuallyFilterLog($logger);
IcingaLogger::replace($logger);
return $logger;
}
/**
* Prepares a JSON-RPC Connection on STDIN/STDOUT
*/
protected function prepareJsonRpc(LoopInterface $loop, $handler)
{
return new JsonRpcConnection(new StreamWrapper(
new ReadableResourceStream(STDIN, $loop),
new WritableResourceStream(STDOUT, $loop)
), $handler);
}
}