These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PragmaRX\Health\Checkers; |
||
4 | |||
5 | use DomainException; |
||
6 | use PragmaRX\Health\Support\Result; |
||
7 | |||
8 | class Process extends Base |
||
9 | { |
||
10 | const METHOD_PROCESS_COUNT = 'process_count'; |
||
11 | |||
12 | const METHOD_PID_FILE = 'pid_file'; |
||
13 | |||
14 | /** |
||
15 | * Check target. |
||
16 | * |
||
17 | * @return Result |
||
18 | */ |
||
19 | public function check() |
||
20 | { |
||
21 | $message = $this->checkMinMax($this->getProcessesRunningCount()); |
||
22 | |||
23 | if (!empty($message)) { |
||
24 | return $this->makeResult(false, $message); |
||
25 | } |
||
26 | |||
27 | return $this->makeHealthyResult(); |
||
28 | } |
||
29 | |||
30 | private function checkMinMax($processes) |
||
31 | { |
||
32 | return $this->buildMessage('minimum', $processes) |
||
33 | ?: $this->buildMessage('maximum', $processes); |
||
34 | } |
||
35 | |||
36 | private function buildMessage($type, $processes) |
||
37 | { |
||
38 | $instances = $this->target->instances; |
||
0 ignored issues
–
show
|
|||
39 | |||
40 | if (!$count = (int) $instances[$type]['count']) { |
||
41 | return ''; |
||
42 | } |
||
43 | |||
44 | if ($type == 'minimum') { |
||
45 | $diff = $processes - $count; |
||
46 | } else { |
||
47 | $diff = $count - $processes; |
||
48 | } |
||
49 | |||
50 | if ($diff < 0) { |
||
51 | return sprintf( |
||
52 | $instances[$type]['message'], |
||
53 | $this->target->processName, |
||
0 ignored issues
–
show
The property
processName does not seem to exist in PragmaRX\Health\Support\Target .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
54 | $processes, |
||
55 | $count |
||
56 | ); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | private function checkPidFile() |
||
61 | { |
||
62 | return $this->processPidFileIsLocked() ? 1 : 0; |
||
63 | } |
||
64 | |||
65 | private function countRunningProcesses() |
||
66 | { |
||
67 | if ($command = $this->getCommand()) { |
||
68 | exec($command, $count); |
||
69 | |||
70 | return count($count); |
||
71 | } |
||
72 | |||
73 | return 0; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param $file |
||
78 | * @return bool |
||
79 | */ |
||
80 | private function checkPidFileExistence($file) |
||
81 | { |
||
82 | if (file_exists($file)) { |
||
83 | return 1; |
||
84 | } |
||
85 | |||
86 | throw new DomainException( |
||
87 | sprintf($this->target->pidFileMissingErrorMessage, $file) |
||
0 ignored issues
–
show
The property
pidFileMissingErrorMessage does not seem to exist in PragmaRX\Health\Support\Target .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
88 | ); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param $file |
||
93 | * @return bool |
||
94 | */ |
||
95 | private function checkPidFileLockState($file) |
||
96 | { |
||
97 | try { |
||
98 | $locked = flock($filePointer = fopen($file, 'r+'), LOCK_EX); |
||
99 | |||
100 | flock($filePointer, LOCK_UN); |
||
101 | |||
102 | fclose($filePointer); |
||
103 | |||
104 | if (!$locked) { |
||
105 | throw new DomainException( |
||
106 | sprintf($this->target->pid_file_missing_not_locked, $file) |
||
0 ignored issues
–
show
The property
pid_file_missing_not_locked does not seem to exist in PragmaRX\Health\Support\Target .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
107 | ); |
||
108 | } |
||
109 | } catch (\Exception $exception) { |
||
110 | report($exception); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return bool |
||
116 | */ |
||
117 | private function processPidFileIsLocked() |
||
118 | { |
||
119 | $file = $this->target->pidFile; |
||
0 ignored issues
–
show
The property
pidFile does not seem to exist in PragmaRX\Health\Support\Target .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
120 | |||
121 | $this->checkPidFileExistence($file); |
||
122 | |||
123 | $this->checkPidFileLockState($file); |
||
124 | |||
125 | return 1; |
||
126 | } |
||
127 | |||
128 | private function getCommand() |
||
129 | { |
||
130 | $command = $this->target->command; |
||
0 ignored issues
–
show
The property
command does not seem to exist in PragmaRX\Health\Support\Target .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
131 | |||
132 | $name = $this->target->processName; |
||
0 ignored issues
–
show
The property
processName does not seem to exist in PragmaRX\Health\Support\Target .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
133 | |||
134 | if ($command && $name) { |
||
135 | return sprintf($command, $name); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | private function getProcessesRunningCount() |
||
140 | { |
||
141 | if ($this->target->method == static::METHOD_PROCESS_COUNT) { |
||
0 ignored issues
–
show
The property
method does not seem to exist in PragmaRX\Health\Support\Target .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
142 | return $this->countRunningProcesses(); |
||
143 | } |
||
144 | |||
145 | if ($this->target->method == static::METHOD_PID_FILE) { |
||
146 | return $this->checkPidFile(); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.