|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Data\Collector\Collector; |
|
6
|
|
|
use Startwind\Inventorio\Data\Reporter\InventorioCloudReporter; |
|
7
|
|
|
use Startwind\Inventorio\Remote\RemoteConnect; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class DaemonCommand extends InventorioCommand |
|
12
|
|
|
{ |
|
13
|
|
|
protected static $defaultName = 'daemon'; |
|
14
|
|
|
protected static $defaultDescription = 'Start long running daemon'; |
|
15
|
|
|
|
|
16
|
|
|
private array $intervals = [ |
|
17
|
|
|
'default' => 60 * 60, |
|
18
|
|
|
'remote' => 10, |
|
19
|
|
|
'collect' => 5 * 60 |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritDoc |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
26
|
|
|
{ |
|
27
|
|
|
$this->initConfiguration($input->getOption('configFile')); |
|
28
|
|
|
|
|
29
|
|
|
$lastRun = [ |
|
30
|
|
|
'default' => time() - 7200, |
|
31
|
|
|
'remote' => time() - 7200, |
|
32
|
|
|
'collect' => time() - 7200, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
$serverId = $this->getServerId(); |
|
36
|
|
|
|
|
37
|
|
|
$remoteConnect = new RemoteConnect( |
|
38
|
|
|
$this->config->getInventorioServer(), |
|
39
|
|
|
$serverId, |
|
40
|
|
|
$this->config->getCommands(), |
|
41
|
|
|
$this->config->getSecret() |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$remoteEnabled = $this->isRemoteEnabled(); |
|
45
|
|
|
$collectEnabled = $this->isCollectEnabled(); |
|
46
|
|
|
|
|
47
|
|
|
if ($collectEnabled) { |
|
48
|
|
|
$collectReporter = new InventorioCloudReporter(); |
|
49
|
|
|
$collectCollector = new Collector(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
var_dump($collectEnabled); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
while (true) { |
|
55
|
|
|
if ($lastRun['default'] < time() - $this->intervals['default']) { |
|
56
|
|
|
$this->getApplication()->find('collect')->run($input, $output); |
|
57
|
|
|
$lastRun['default'] = time(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($remoteEnabled) { |
|
61
|
|
|
if ($lastRun['remote'] < time() - $this->intervals['remote']) { |
|
62
|
|
|
$remoteConnect->run(); |
|
63
|
|
|
$lastRun['remote'] = time(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($collectEnabled) { |
|
68
|
|
|
if ($lastRun['collect'] < time() - $this->intervals['collect']) { |
|
69
|
|
|
$collectReporter->report($serverId, $collectCollector->collect()); |
|
|
|
|
|
|
70
|
|
|
$lastRun['collect'] = time(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
sleep($this->getInterval()); |
|
75
|
|
|
} |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function getInterval(): int |
|
79
|
|
|
{ |
|
80
|
|
|
if ($this->isRemoteEnabled()) { |
|
81
|
|
|
return $this->intervals['remote']; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($this->isCollectEnabled()) { |
|
85
|
|
|
return $this->intervals['collect']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->intervals['default']; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|