Passed
Push — develop ( 020590...6f8a0b )
by Портнов
05:37
created

RedisConf::generateSyslogConf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 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
use MikoPBX\Common\Providers\ConfigProvider;
23
use MikoPBX\Core\System\Processes;
24
use MikoPBX\Core\System\Util;
25
use Phalcon\Di\Injectable;
26
27
/**
28
 * Class RedisConf
29
 *
30
 * Represents the Redis configuration.
31
 *
32
 * @package MikoPBX\Core\System\Configs
33
 */
34
class RedisConf extends Injectable
35
{
36
    public const PROC_NAME = 'redis-server';
37
38
    public const CONF_FILE = '/etc/redis.conf';
39
40
    public string $port = '';
41
42
    /**
43
     * Restarts the Redis server.
44
     *
45
     * @return void
46
     */
47
    public function reStart(): void
48
    {
49
        $mainRunner = 'safe-'.self::PROC_NAME;
50
        Processes::killByName($mainRunner);
51
        Processes::killByName(self::PROC_NAME);
52
53
        $ch = 0;
54
        do{
55
            $ch++;
56
            // Wait for Redis to finish its work
57
            sleep(1);
58
            $pid1 = Processes::getPidOfProcess($mainRunner);
59
            $pid2 = Processes::getPidOfProcess(self::PROC_NAME);
60
        }while(!empty($pid1.$pid2) && $ch < 30);
61
62
        $this->configure();
63
        Processes::safeStartDaemon(self::PROC_NAME, self::CONF_FILE);
64
65
        $redisCli = Util::which('redis-cli');
66
        for ($i=1; $i <= 60; $i++){
67
            if(Processes::mwExec("$redisCli -p $this->port info") === 0){
68
                break;
69
            }
70
            sleep(1);
71
        }
72
    }
73
74
    /**
75
     * Sets up the Redis daemon conf file.
76
     *
77
     * @return void
78
     */
79
    private function configure(): void
80
    {
81
        $config = $this->getDI()->get(ConfigProvider::SERVICE_NAME)->redis;
82
        $this->port = $config->port;
83
        $conf   = "bind {$config->host}" . PHP_EOL;
84
        $conf  .= "port {$config->port}" . PHP_EOL;
85
        $conf  .= "dir /var/tmp" . PHP_EOL;
86
        $conf  .= "loglevel warning" . PHP_EOL;
87
        $conf  .= "syslog-enabled yes" . PHP_EOL;
88
        $conf  .= "syslog-ident redis" . PHP_EOL;
89
        file_put_contents(self::CONF_FILE, $conf);
90
    }
91
92
    /**
93
     * Generate additional syslog rules.
94
     * @return void
95
     */
96
    public static function generateSyslogConf():void
97
    {
98
        Util::mwMkdir('/etc/rsyslog.d');
99
        $log_fileRedis       = SyslogConf::getSyslogFile('redis');
100
        $pathScriptRedis     = SyslogConf::createRotateScript('redis');
101
        $confSyslogD = '$outchannel log_redis,'.$log_fileRedis.',10485760,'.$pathScriptRedis.PHP_EOL.
102
            'if $programname == "redis-server" then :omfile:$log_redis'.PHP_EOL.
103
            'if $programname == "redis-server" then stop'.PHP_EOL;
104
        file_put_contents('/etc/rsyslog.d/redis.conf', $confSyslogD);
105
    }
106
}