Passed
Push — develop ( 79bfea...587507 )
by Nikolay
05:31
created

NatsConf::logRotate()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 42
rs 9.2088
c 0
b 0
f 0
cc 5
nc 16
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Core\System\Configs;
21
22
23
use MikoPBX\Core\System\MikoPBXConfig;
24
use MikoPBX\Core\System\System;
25
use MikoPBX\Core\System\Util;
26
use MikoPBX\Core\System\Processes;
27
use Phalcon\Di;
28
use Phalcon\Di\Injectable;
29
30
class NatsConf extends Injectable
31
{
32
    private MikoPBXConfig $mikoPBXConfig;
33
34
    /**
35
     * NatsConf constructor.
36
     */
37
    public function __construct()
38
    {
39
        $this->mikoPBXConfig = new MikoPBXConfig();
40
    }
41
    /**
42
     * Restarts gnats server
43
     */
44
    public function reStart(): void
45
    {
46
        $confdir = '/etc/nats';
47
        Util::mwMkdir($confdir);
48
49
        $logdir = System::getLogDir() . '/nats';
50
        Util::mwMkdir($logdir);
51
52
        $tempDir = $this->di->getShared('config')->path('core.tempDir');
53
        $sessionsDir = "{$tempDir}/nats_cache";
54
        Util::mwMkdir($sessionsDir);
55
56
        $pid_file = '/var/run/gnatsd.pid';
57
        $settings = [
58
            'port'             => '4223',
59
            'http_port'        => '8223',
60
            'debug'            => 'false',
61
            'trace'            => 'false',
62
            'logtime'          => 'true',
63
            'pid_file'         => $pid_file,
64
            'max_connections'  => '1000',
65
            'max_payload'      => '1000000',
66
            'max_control_line' => '512',
67
            'sessions_path'    => $sessionsDir,
68
            'log_size_limit'   => 10485760, //10Mb
69
            'log_file'         => "{$logdir}/gnatsd.log",
70
        ];
71
        $config   = '';
72
        foreach ($settings as $key => $val) {
73
            $config .= "{$key}: {$val} \n";
74
        }
75
        $conf_file = "{$confdir}/natsd.conf";
76
        Util::fileWriteContent($conf_file, $config);
77
78
        $lic = $this->mikoPBXConfig->getGeneralSettings('PBXLicense');
79
        file_put_contents("{$sessionsDir}/license.key", $lic);
80
81
        if (file_exists($pid_file)) {
82
            $killPath = Util::which('kill');
83
            $catPath = Util::which('kill');
84
            Processes::mwExec("{$killPath} $({$catPath} {$pid_file})");
85
        }
86
87
        $gnatsdPath = Util::which('gnatsd');
88
        Processes::mwExecBg("{$gnatsdPath} --config {$conf_file}", "{$logdir}/gnats_process.log");
89
    }
90
}