|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) MIKO LLC - All Rights Reserved |
|
4
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited |
|
5
|
|
|
* Proprietary and confidential |
|
6
|
|
|
* Written by Nikolay Beketov, 7 2020 |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace MikoPBX\Core\System\Configs; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use MikoPBX\Core\System\MikoPBXConfig; |
|
14
|
|
|
use MikoPBX\Core\System\System; |
|
15
|
|
|
use MikoPBX\Core\System\Util; |
|
16
|
|
|
use Phalcon\Di; |
|
17
|
|
|
use Phalcon\Di\Injectable; |
|
18
|
|
|
|
|
19
|
|
|
class NatsConf extends Injectable |
|
20
|
|
|
{ |
|
21
|
|
|
private MikoPBXConfig $mikoPBXConfig; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* NatsConf constructor. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->mikoPBXConfig = new MikoPBXConfig(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Rotates gnats logs |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function logRotate(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$log_dir = System::getLogDir() . '/nats'; |
|
37
|
|
|
$gnatsdPath = Util::which('gnatsd'); |
|
38
|
|
|
$pid = Util::getPidOfProcess($gnatsdPath, 'custom_modules'); |
|
39
|
|
|
$max_size = 1; |
|
40
|
|
|
if (empty($pid)) { |
|
41
|
|
|
$natsConf = new self(); |
|
42
|
|
|
$natsConf->reStart(); |
|
43
|
|
|
sleep(1); |
|
44
|
|
|
} |
|
45
|
|
|
$text_config = "{$log_dir}/gnatsd.log { |
|
46
|
|
|
start 0 |
|
47
|
|
|
rotate 9 |
|
48
|
|
|
size {$max_size}M |
|
49
|
|
|
maxsize 1M |
|
50
|
|
|
daily |
|
51
|
|
|
missingok |
|
52
|
|
|
notifempty |
|
53
|
|
|
sharedscripts |
|
54
|
|
|
postrotate |
|
55
|
|
|
{$gnatsdPath} -sl reopen=$pid > /dev/null 2> /dev/null |
|
56
|
|
|
endscript |
|
57
|
|
|
}"; |
|
58
|
|
|
|
|
59
|
|
|
$mb10 = $max_size * 1024 * 1024; |
|
60
|
|
|
|
|
61
|
|
|
$options = ''; |
|
62
|
|
|
if (Util::mFileSize("{$log_dir}/gnatsd.log") > $mb10) { |
|
63
|
|
|
$options = '-f'; |
|
64
|
|
|
} |
|
65
|
|
|
$di = Di::getDefault(); |
|
66
|
|
|
if ($di !== null){ |
|
67
|
|
|
$varEtcPath = $di->getConfig()->path('core.varEtcPath'); |
|
68
|
|
|
} else { |
|
69
|
|
|
$varEtcPath = '/var/etc'; |
|
70
|
|
|
} |
|
71
|
|
|
$path_conf = $varEtcPath . '/gnatsd_logrotate.conf'; |
|
72
|
|
|
file_put_contents($path_conf, $text_config); |
|
73
|
|
|
if (file_exists("{$log_dir}/gnatsd.log")) { |
|
74
|
|
|
$logrotatePath = Util::which('logrotate'); |
|
75
|
|
|
Util::mwExecBg("{$logrotatePath} $options '{$path_conf}' > /dev/null 2> /dev/null"); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Restarts gnats server |
|
81
|
|
|
*/ |
|
82
|
|
|
public function reStart(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$confdir = '/etc/nats'; |
|
85
|
|
|
Util::mwMkdir($confdir); |
|
86
|
|
|
$logdir = System::getLogDir() . '/nats'; |
|
87
|
|
|
Util::mwMkdir($logdir); |
|
88
|
|
|
|
|
89
|
|
|
$pid_file = '/var/run/gnatsd.pid'; |
|
90
|
|
|
$settings = [ |
|
91
|
|
|
'port' => '4223', |
|
92
|
|
|
'http_port' => '8223', |
|
93
|
|
|
'debug' => 'false', |
|
94
|
|
|
'trace' => 'false', |
|
95
|
|
|
'logtime' => 'true', |
|
96
|
|
|
'pid_file' => $pid_file, |
|
97
|
|
|
'max_connections' => '1000', |
|
98
|
|
|
'max_payload' => '1000000', |
|
99
|
|
|
'max_control_line' => '512', |
|
100
|
|
|
'sessions_path' => $logdir, |
|
101
|
|
|
'log_file' => "{$logdir}/gnatsd.log", |
|
102
|
|
|
]; |
|
103
|
|
|
$config = ''; |
|
104
|
|
|
foreach ($settings as $key => $val) { |
|
105
|
|
|
$config .= "{$key}: {$val} \n"; |
|
106
|
|
|
} |
|
107
|
|
|
$conf_file = "{$confdir}/natsd.conf"; |
|
108
|
|
|
Util::fileWriteContent($conf_file, $config); |
|
109
|
|
|
|
|
110
|
|
|
$lic = $this->mikoPBXConfig->getGeneralSettings('PBXLicense'); |
|
111
|
|
|
file_put_contents($logdir . '/license.key', $lic); |
|
112
|
|
|
|
|
113
|
|
|
if (file_exists($pid_file)) { |
|
114
|
|
|
$killPath = Util::which('kill'); |
|
115
|
|
|
$catPath = Util::which('kill'); |
|
116
|
|
|
Util::mwExec("{$killPath} $({$catPath} {$pid_file})"); |
|
117
|
|
|
} |
|
118
|
|
|
$gnatsdPath = Util::which('gnatsd'); |
|
119
|
|
|
Util::mwExecBg("{$gnatsdPath} --config {$conf_file}", "{$logdir}/gnats_process.log"); |
|
120
|
|
|
} |
|
121
|
|
|
} |