|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\SelfDiagnosis\Checks; |
|
4
|
|
|
|
|
5
|
|
|
use BeyondCode\SelfDiagnosis\SystemFunctions; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
|
|
8
|
|
|
class SupervisorProgramsAreRunning implements Check |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var Collection */ |
|
11
|
|
|
protected $notRunningPrograms; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string|null */ |
|
14
|
|
|
protected $message; |
|
15
|
|
|
|
|
16
|
|
|
/** @var SystemFunctions */ |
|
17
|
|
|
protected $systemFunctions; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected const REGEX_SUPERVISORCTL_STATUS = '/^(\S+)\s+RUNNING\s+pid\s+(\d+),\s+uptime\s+(\d+):(\d+):(\d+)$/'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* SupervisorProgramsAreRunning constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param SystemFunctions $systemFunctions |
|
26
|
|
|
*/ |
|
27
|
32 |
|
public function __construct(SystemFunctions $systemFunctions) |
|
28
|
|
|
{ |
|
29
|
32 |
|
$this->systemFunctions = $systemFunctions; |
|
30
|
32 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The name of the check. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $config |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public function name(array $config): string |
|
39
|
|
|
{ |
|
40
|
|
|
return trans('self-diagnosis::checks.supervisor_programs_are_running.name'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Perform the actual verification of this check. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $config |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
32 |
|
public function check(array $config): bool |
|
50
|
|
|
{ |
|
51
|
32 |
|
$this->notRunningPrograms = new Collection(array_get($config, 'programs', [])); |
|
|
|
|
|
|
52
|
32 |
|
if ($this->notRunningPrograms->isEmpty()) { |
|
53
|
4 |
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
28 |
|
if (!$this->systemFunctions->isFunctionAvailable('shell_exec')) { |
|
57
|
4 |
|
$this->message = trans('self-diagnosis::checks.supervisor_programs_are_running.message.shell_exec_not_available'); |
|
|
|
|
|
|
58
|
4 |
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
24 |
|
if ($this->systemFunctions->isWindowsOperatingSystem()) { |
|
62
|
4 |
|
$this->message = trans('self-diagnosis::checks.supervisor_programs_are_running.message.cannot_run_on_windows'); |
|
|
|
|
|
|
63
|
4 |
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
20 |
|
$programs = $this->systemFunctions->callShellExec('supervisorctl status'); |
|
67
|
20 |
|
if ($programs === null || $programs === '') { |
|
68
|
4 |
|
$this->message = trans('self-diagnosis::checks.supervisor_programs_are_running.message.supervisor_command_not_available'); |
|
|
|
|
|
|
69
|
4 |
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
16 |
|
$restartedWithin = array_get($config, 'restarted_within', 0); |
|
|
|
|
|
|
73
|
16 |
|
$programs = explode("\n" , $programs); |
|
74
|
16 |
|
foreach ($programs as $program) { |
|
75
|
|
|
/* |
|
76
|
|
|
* Capture groups of regex: |
|
77
|
|
|
* (program name) (process id) (uptime hours) (minutes) (seconds) |
|
78
|
|
|
*/ |
|
79
|
16 |
|
$isMatch = preg_match(self::REGEX_SUPERVISORCTL_STATUS, trim($program), $matches); |
|
80
|
16 |
|
if ($isMatch) { |
|
81
|
16 |
|
if ($restartedWithin > 0) { |
|
82
|
8 |
|
$totalSeconds = $matches[3] * 3600 + $matches[4] * 60 + $matches[5]; |
|
83
|
8 |
|
if ($totalSeconds > $restartedWithin) { |
|
84
|
4 |
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->notRunningPrograms = $this->notRunningPrograms->reject(function ($item) use ($matches) { |
|
89
|
12 |
|
return $item === $matches[1]; |
|
90
|
12 |
|
}); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
16 |
|
return $this->notRunningPrograms->isEmpty(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* The error message to display in case the check does not pass. |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $config |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
20 |
|
public function message(array $config): string |
|
104
|
|
|
{ |
|
105
|
20 |
|
if ($this->message) { |
|
|
|
|
|
|
106
|
12 |
|
return $this->message; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
8 |
|
return trans('self-diagnosis::checks.supervisor_programs_are_running.message.not_running_programs', [ |
|
110
|
8 |
|
'programs' => $this->notRunningPrograms->implode(PHP_EOL), |
|
111
|
|
|
]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.