|
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\Util; |
|
15
|
|
|
use MikoPBX\Core\Workers\Cron\WorkerSafeScriptsCore; |
|
16
|
|
|
use Phalcon\Di\Injectable; |
|
17
|
|
|
|
|
18
|
|
|
use function MikoPBX\Common\Config\appPath; |
|
19
|
|
|
|
|
20
|
|
|
class CronConf extends Injectable |
|
21
|
|
|
{ |
|
22
|
|
|
private MikoPBXConfig $mikoPBXConfig; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* CronConf constructor. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->mikoPBXConfig = new MikoPBXConfig(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Generates crontab config |
|
34
|
|
|
* |
|
35
|
|
|
* @param bool $boot |
|
36
|
|
|
*/ |
|
37
|
|
|
private function generateConfig($boot = true): void |
|
38
|
|
|
{ |
|
39
|
|
|
$additionalModules = $this->di->getShared('pbxConfModules'); |
|
40
|
|
|
$mast_have = []; |
|
41
|
|
|
|
|
42
|
|
|
if (Util::isSystemctl()) { |
|
43
|
|
|
$mast_have[] = "SHELL=/bin/sh\n"; |
|
44
|
|
|
$mast_have[] = "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n\n"; |
|
45
|
|
|
$cron_filename = '/etc/cron.d/mikopbx'; |
|
46
|
|
|
$cron_user = 'root '; |
|
47
|
|
|
} else { |
|
48
|
|
|
$cron_filename = '/var/spool/cron/crontabs/root'; |
|
49
|
|
|
$cron_user = ''; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$workerSafeScriptsPath = Util::getFilePathByClassName(WorkerSafeScriptsCore::class); |
|
53
|
|
|
$phpPath = Util::which('php'); |
|
54
|
|
|
$WorkerSafeScripts = "{$phpPath} -f {$workerSafeScriptsPath} start > /dev/null 2> /dev/null"; |
|
55
|
|
|
|
|
56
|
|
|
$workersPath = appPath('src/Core/Workers'); |
|
57
|
|
|
|
|
58
|
|
|
$restart_night = $this->mikoPBXConfig->getGeneralSettings('RestartEveryNight'); |
|
59
|
|
|
$asteriskPath = Util::which('asterisk'); |
|
60
|
|
|
$ntpdPath = Util::which('ntpd'); |
|
61
|
|
|
$shPath = Util::which('sh'); |
|
62
|
|
|
if ($restart_night === '1') { |
|
63
|
|
|
$mast_have[] = '0 1 * * * ' . $cron_user . $asteriskPath . ' -rx"core restart now" > /dev/null 2> /dev/null' . "\n"; |
|
64
|
|
|
} |
|
65
|
|
|
$mast_have[] = '*/5 * * * * ' . $cron_user . $ntpdPath . ' -q > /dev/null 2> /dev/null' . "\n"; |
|
66
|
|
|
$mast_have[] = '*/6 * * * * ' . $cron_user . "{$shPath} {$workersPath}/Cron/cleaner_download_links.sh > /dev/null 2> /dev/null\n"; |
|
67
|
|
|
$mast_have[] = '*/1 * * * * ' . $cron_user . "{$WorkerSafeScripts}\n"; |
|
68
|
|
|
|
|
69
|
|
|
$tasks = []; |
|
70
|
|
|
|
|
71
|
|
|
// Add additional modules includes |
|
72
|
|
|
foreach ($additionalModules as $appClass) { |
|
73
|
|
|
/** @var \MikoPBX\Modules\Config\ConfigClass $appClass */ |
|
74
|
|
|
$appClass->createCronTasks($tasks); |
|
75
|
|
|
} |
|
76
|
|
|
$conf = implode('', array_merge($mast_have, $tasks)); |
|
77
|
|
|
|
|
78
|
|
|
if (Util::isSystemctl()) { |
|
79
|
|
|
// Convert rules to debian style |
|
80
|
|
|
$conf = str_replace(' * * * * /', ' * * * * root /', $conf); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($boot === true) { |
|
84
|
|
|
Util::mwExecBg($WorkerSafeScripts); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
Util::fileWriteContent($cron_filename, $conf); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Setups crond and restart it |
|
92
|
|
|
* |
|
93
|
|
|
* @return int |
|
94
|
|
|
*/ |
|
95
|
|
|
public function reStart(): int |
|
96
|
|
|
{ |
|
97
|
|
|
$this->generateConfig($this->di->registry->booting); |
|
|
|
|
|
|
98
|
|
|
if (Util::isSystemctl()) { |
|
99
|
|
|
$systemctlPath = Util::which('systemctl'); |
|
100
|
|
|
Util::mwExec("{$systemctlPath} restart cron"); |
|
101
|
|
|
} else { |
|
102
|
|
|
$crondPath = Util::which('crond'); |
|
103
|
|
|
Util::killByName($crondPath); |
|
104
|
|
|
Util::mwExec("{$crondPath} -L /dev/null -l 8"); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return 0; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
} |