|
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\Models\PbxSettingsConstants; |
|
23
|
|
|
use MikoPBX\Common\Providers\PBXConfModulesProvider; |
|
24
|
|
|
use MikoPBX\Common\Providers\RegistryProvider; |
|
25
|
|
|
use MikoPBX\Core\System\MikoPBXConfig; |
|
26
|
|
|
use MikoPBX\Core\System\Processes; |
|
27
|
|
|
use MikoPBX\Core\System\Util; |
|
28
|
|
|
use MikoPBX\Core\Workers\Cron\WorkerSafeScriptsCore; |
|
29
|
|
|
use MikoPBX\Modules\Config\SystemConfigInterface; |
|
30
|
|
|
use Phalcon\Di\Injectable; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class CronConf |
|
34
|
|
|
* |
|
35
|
|
|
* Represents the Cron configuration. |
|
36
|
|
|
* |
|
37
|
|
|
* @package MikoPBX\Core\System\Configs |
|
38
|
|
|
*/ |
|
39
|
|
|
class CronConf extends Injectable |
|
40
|
|
|
{ |
|
41
|
|
|
public const PROC_NAME = 'crond'; |
|
42
|
|
|
|
|
43
|
|
|
private MikoPBXConfig $mikoPBXConfig; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* CronConf constructor. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->mikoPBXConfig = new MikoPBXConfig(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Setups crond and restarts it. |
|
55
|
|
|
* |
|
56
|
|
|
* @return int Returns 0 on success. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function reStart(): int |
|
59
|
|
|
{ |
|
60
|
|
|
$booting = $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting??false; |
|
61
|
|
|
$this->generateConfig($booting); |
|
62
|
|
|
if (Util::isSystemctl()) { |
|
63
|
|
|
$systemctl = Util::which('systemctl'); |
|
64
|
|
|
Processes::mwExec("$systemctl restart ".self::PROC_NAME); |
|
65
|
|
|
} else { |
|
66
|
|
|
// T2SDE or Docker |
|
67
|
|
|
$cronPath = Util::which(self::PROC_NAME); |
|
68
|
|
|
Processes::killByName(self::PROC_NAME); |
|
69
|
|
|
Processes::mwExec("$cronPath -S -l 0"); |
|
70
|
|
|
} |
|
71
|
|
|
return 0; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Generates crontab config. |
|
76
|
|
|
* |
|
77
|
|
|
* @param bool $boot Indicates whether the config is generated during boot. |
|
78
|
|
|
*/ |
|
79
|
|
|
private function generateConfig(bool $boot = true): void |
|
80
|
|
|
{ |
|
81
|
|
|
$mast_have = []; |
|
82
|
|
|
$cron_filename = '/var/spool/cron/crontabs/root'; |
|
83
|
|
|
|
|
84
|
|
|
$workerSafeScriptsPath = Util::getFilePathByClassName(WorkerSafeScriptsCore::class); |
|
85
|
|
|
$phpPath = Util::which('php'); |
|
86
|
|
|
$WorkerSafeScripts = "$phpPath -f $workerSafeScriptsPath start > /dev/null 2> /dev/null"; |
|
87
|
|
|
|
|
88
|
|
|
$restart_night = $this->mikoPBXConfig->getGeneralSettings(PbxSettingsConstants::RESTART_EVERY_NIGHT); |
|
89
|
|
|
$asterisk = Util::which('asterisk'); |
|
90
|
|
|
$ntpd = Util::which('ntpd'); |
|
91
|
|
|
$dump = Util::which('dump-conf-db'); |
|
92
|
|
|
$checkIpPath = Util::which('check-out-ip'); |
|
93
|
|
|
$recordsCleaner= Util::which('records-cleaner'); |
|
94
|
|
|
$cleanerLinks = Util::which('cleaner-download-links'); |
|
95
|
|
|
|
|
96
|
|
|
// Restart every night if enabled |
|
97
|
|
|
if ($restart_night === '1') { |
|
98
|
|
|
$mast_have[] = '0 1 * * * ' . $asterisk . ' -rx"core restart now" > /dev/null 2> /dev/null'.PHP_EOL; |
|
99
|
|
|
} |
|
100
|
|
|
// Update NTP time every 5 minutes |
|
101
|
|
|
$mast_have[] = '*/5 * * * * ' . $ntpd . ' -q > /dev/null 2> /dev/null'.PHP_EOL; |
|
102
|
|
|
|
|
103
|
|
|
// Perform database dump every 5 minutes |
|
104
|
|
|
$mast_have[] = '*/5 * * * * ' . "$dump > /dev/null 2> /dev/null".PHP_EOL; |
|
105
|
|
|
// Clearing outdated conversation records |
|
106
|
|
|
$mast_have[] = '*/30 * * * * ' . "$recordsCleaner > /dev/null 2> /dev/null".PHP_EOL; |
|
107
|
|
|
|
|
108
|
|
|
// Check IP address every minute |
|
109
|
|
|
$mast_have[] = '*/1 * * * * ' . "$checkIpPath > /dev/null 2> /dev/null".PHP_EOL; |
|
110
|
|
|
|
|
111
|
|
|
// Clean download links every 6 minutes |
|
112
|
|
|
$mast_have[] = '*/6 * * * * ' . "$cleanerLinks > /dev/null 2> /dev/null".PHP_EOL; |
|
113
|
|
|
|
|
114
|
|
|
// Run WorkerSafeScripts every minute |
|
115
|
|
|
$mast_have[] = '*/1 * * * * ' . $WorkerSafeScripts.PHP_EOL; |
|
116
|
|
|
|
|
117
|
|
|
// Add additional modules includes |
|
118
|
|
|
$tasks = []; |
|
119
|
|
|
PBXConfModulesProvider::hookModulesMethod(SystemConfigInterface::CREATE_CRON_TASKS, [&$tasks]); |
|
120
|
|
|
$conf = implode('', array_merge($mast_have, $tasks)); |
|
121
|
|
|
|
|
122
|
|
|
// Execute WorkerSafeScripts during boot if enabled |
|
123
|
|
|
if ($boot === true) { |
|
124
|
|
|
Processes::mwExecBg($WorkerSafeScripts); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Write the generated config to the cron file |
|
128
|
|
|
Util::fileWriteContent($cron_filename, $conf); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Generate additional syslog rules. |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function generateSyslogConf():void |
|
136
|
|
|
{ |
|
137
|
|
|
Util::mwMkdir('/etc/rsyslog.d'); |
|
138
|
|
|
$log_fileRedis = SyslogConf::getSyslogFile(self::PROC_NAME); |
|
139
|
|
|
$pathScriptRedis = SyslogConf::createRotateScript(self::PROC_NAME); |
|
140
|
|
|
$confSyslogD = '$outchannel log_'.self::PROC_NAME.','.$log_fileRedis.',10485760,'.$pathScriptRedis.PHP_EOL. |
|
141
|
|
|
'if $programname == "'.self::PROC_NAME.'" then :omfile:$log_'.self::PROC_NAME.PHP_EOL. |
|
142
|
|
|
'if $programname == "'.self::PROC_NAME.'" then stop'.PHP_EOL; |
|
143
|
|
|
file_put_contents('/etc/rsyslog.d/'.self::PROC_NAME.'.conf', $confSyslogD); |
|
144
|
|
|
} |
|
145
|
|
|
} |