1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Command; |
4
|
|
|
|
5
|
|
|
use Monolog\Handler\StreamHandler; |
6
|
|
|
use Monolog\Logger; |
7
|
|
|
use Startwind\Inventorio\Metrics\Collector\Collector; |
8
|
|
|
use Startwind\Inventorio\Metrics\Memory\Memory; |
9
|
|
|
use Startwind\Inventorio\Metrics\Reporter\InventorioCloudReporter; |
10
|
|
|
use Startwind\Inventorio\Remote\RemoteConnect; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class DaemonCommand extends InventorioCommand |
15
|
|
|
{ |
16
|
|
|
protected static $defaultName = 'daemon'; |
17
|
|
|
protected static $defaultDescription = 'Start long running daemon'; |
18
|
|
|
|
19
|
|
|
private array $intervals = [ |
20
|
|
|
'default' => 60 * 60 * 1, // 1 hour |
21
|
|
|
'remote' => 10, // 10 seconds |
22
|
|
|
'smartCare' => 10, // 10 seconds |
23
|
|
|
'collect' => 5 * 60 // 5 minutes |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
30
|
|
|
{ |
31
|
|
|
$this->initConfiguration($input->getOption('configFile')); |
32
|
|
|
|
33
|
|
|
$isDebug = $input->getOption('debug'); |
34
|
|
|
|
35
|
|
|
if ($isDebug) { |
36
|
|
|
$logger = new Logger('name'); |
37
|
|
|
$logger->pushHandler(new StreamHandler('daemon.log', Logger::DEBUG)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$lastRun = [ |
41
|
|
|
'default' => time() - (24 * 60 * 60), |
42
|
|
|
'remote' => time() - (24 * 60 * 60), |
43
|
|
|
'collect' => time() - (24 * 60 * 60), |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
$serverId = $this->getServerId(); |
47
|
|
|
$memory = Memory::getInstance(); |
48
|
|
|
|
49
|
|
|
$remoteConnect = new RemoteConnect( |
50
|
|
|
$this->config->getInventorioServer(), |
51
|
|
|
$serverId, |
52
|
|
|
$this->config->getCommands(), |
53
|
|
|
$this->config->getSecret() |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$remoteEnabled = $this->isRemoteEnabled(); |
57
|
|
|
$collectEnabled = $this->isCollectEnabled(); |
58
|
|
|
$smartCareEnabled = $this->isSmartCareEnabled(); |
59
|
|
|
|
60
|
|
|
if ($collectEnabled) { |
61
|
|
|
$collectReporter = new InventorioCloudReporter(); |
62
|
|
|
$collectCollector = new Collector(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
while (true) { |
66
|
|
|
if ($lastRun['default'] <= time() - $this->intervals['default']) { |
67
|
|
|
$this->getApplication()->find('collect')->run($input, $output); |
68
|
|
|
$lastRun['default'] = time(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($remoteEnabled || $smartCareEnabled) { |
72
|
|
|
if ($lastRun['remote'] <= time() - $this->intervals['remote']) { |
73
|
|
|
$result = $remoteConnect->run($remoteEnabled, $smartCareEnabled); |
74
|
|
|
if ($isDebug && $result) { |
75
|
|
|
$logger->debug('Running command: ' . $result); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
$lastRun['remote'] = time(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($collectEnabled) { |
82
|
|
|
if ($lastRun['collect'] <= time() - $this->intervals['collect']) { |
83
|
|
|
$dataset = $collectCollector->collect(); |
|
|
|
|
84
|
|
|
$memory->addDataSet($dataset); |
85
|
|
|
$collectReporter->report($serverId, $dataset); |
|
|
|
|
86
|
|
|
$lastRun['collect'] = time(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
sleep($this->getInterval()); |
91
|
|
|
} |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function getInterval(): int |
95
|
|
|
{ |
96
|
|
|
if ($this->isRemoteEnabled() || $this->isSmartCareEnabled()) { |
97
|
|
|
return $this->intervals['remote']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($this->isCollectEnabled()) { |
101
|
|
|
return $this->intervals['collect']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->intervals['default']; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|