Total Complexity | 59 |
Total Lines | 608 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Processes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Processes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Processes |
||
37 | { |
||
38 | /** |
||
39 | * Default timeout for background process execution in seconds |
||
40 | */ |
||
41 | private const DEFAULT_BG_TIMEOUT = 4; |
||
42 | |||
43 | /** |
||
44 | * Default number of attempts for safe daemon start |
||
45 | */ |
||
46 | private const DEFAULT_START_ATTEMPTS = 20; |
||
47 | |||
48 | /** |
||
49 | * Default timeout between attempts in microseconds |
||
50 | */ |
||
51 | private const DEFAULT_ATTEMPT_TIMEOUT = 1000000; |
||
52 | |||
53 | /** |
||
54 | * Default output file for process logs |
||
55 | */ |
||
56 | private const DEFAULT_OUTPUT_FILE = '/dev/null'; |
||
57 | |||
58 | /** |
||
59 | * Directory for temporary shell scripts |
||
60 | */ |
||
61 | private const TEMP_SCRIPTS_DIR = '/tmp'; |
||
62 | |||
63 | /** |
||
64 | * Valid process actions |
||
65 | */ |
||
66 | private const VALID_ACTIONS = ['start', 'stop', 'restart', 'status']; |
||
67 | |||
68 | /** |
||
69 | * Validates process action. |
||
70 | * |
||
71 | * @param string $action Action to validate |
||
72 | * @return bool |
||
73 | */ |
||
74 | private static function isValidAction(string $action): bool |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Checks if a process is running. |
||
81 | * |
||
82 | * @param string $processIdOrName Process ID or name |
||
83 | * @return bool |
||
84 | */ |
||
85 | public static function isProcessRunning(string $processIdOrName): bool |
||
86 | { |
||
87 | if (is_numeric($processIdOrName)) { |
||
88 | // Check by PID |
||
89 | return file_exists("/proc/$processIdOrName"); |
||
90 | } |
||
91 | // Check by process name |
||
92 | return self::getPidOfProcess($processIdOrName) !== ''; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Safely terminates a process with timeout. |
||
97 | * |
||
98 | * @param string $pid Process ID to terminate |
||
99 | * @param int $timeout Timeout in seconds |
||
100 | * @return bool Success status |
||
101 | */ |
||
102 | private static function safeTerminateProcess(string $pid, int $timeout = 10): bool |
||
103 | { |
||
104 | if (empty($pid)) { |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | $kill = Util::which('kill'); |
||
109 | |||
110 | // First try SIGTERM |
||
111 | self::mwExec("$kill -SIGTERM $pid"); |
||
112 | |||
113 | // Wait for process to terminate |
||
114 | $startTime = time(); |
||
115 | while (time() - $startTime < $timeout) { |
||
116 | if (!self::isProcessRunning($pid)) { |
||
117 | return true; |
||
118 | } |
||
119 | usleep(100000); // 100ms |
||
120 | } |
||
121 | |||
122 | // Force kill if still running |
||
123 | self::mwExec("$kill -9 $pid"); |
||
124 | return !self::isProcessRunning($pid); |
||
125 | } |
||
126 | |||
127 | |||
128 | /** |
||
129 | * Waits for a process to start. |
||
130 | * |
||
131 | * @param string $procName Process name |
||
132 | * @param string $excludeName Process name to exclude |
||
133 | * @param int $attempts Maximum number of attempts |
||
134 | * @param int $timeout Timeout between attempts |
||
135 | * @return string Process ID or empty string |
||
136 | */ |
||
137 | private static function waitForProcessStart( |
||
138 | string $procName, |
||
139 | string $excludeName, |
||
140 | int $attempts, |
||
141 | int $timeout |
||
142 | ): string { |
||
143 | $attempt = 1; |
||
144 | while ($attempt < $attempts) { |
||
145 | $pid = self::getPidOfProcess($procName, $excludeName); |
||
146 | if (!empty($pid)) { |
||
147 | return $pid; |
||
148 | } |
||
149 | usleep($timeout); |
||
150 | $attempt++; |
||
151 | } |
||
152 | return ''; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Kills a process/daemon by name. |
||
157 | * |
||
158 | * @param string $procName The name of the process/daemon to kill. |
||
159 | * @return int|null The return code of the execution. |
||
160 | */ |
||
161 | public static function killByName(string $procName): ?int |
||
162 | { |
||
163 | if (empty(trim($procName))) { |
||
164 | throw new InvalidArgumentException('Process name cannot be empty'); |
||
165 | } |
||
166 | |||
167 | $killallPath = Util::which('killall'); |
||
168 | return self::mwExec($killallPath . ' ' . escapeshellarg($procName)); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Executes a command using exec(). |
||
173 | * |
||
174 | * @param string $command The command to execute. |
||
175 | * @param array|null $outArr Reference to array for command output. |
||
176 | * @param int|null $retVal Reference for return value. |
||
177 | * @return int The return value of the execution. |
||
178 | * @throws RuntimeException If command is empty. |
||
179 | */ |
||
180 | public static function mwExec(string $command, ?array &$outArr = null, ?int &$retVal = null): int |
||
181 | { |
||
182 | if (empty(trim($command))) { |
||
183 | throw new RuntimeException('Empty command provided to mwExec'); |
||
184 | } |
||
185 | |||
186 | $retVal = 0; |
||
187 | $outArr = []; |
||
188 | $di = Di::getDefault(); |
||
189 | |||
190 | if ($di !== null && $di->getShared('config')->path('core.debugMode')) { |
||
191 | echo "mwExec(): $command\n"; |
||
192 | } else { |
||
193 | exec("$command 2>&1", $outArr, $retVal); |
||
194 | } |
||
195 | return $retVal; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Executes a command as a background process with timeout. |
||
200 | * |
||
201 | * @param string $command The command to execute. |
||
202 | * @param int $timeout The timeout value in seconds. |
||
203 | * @param string $logName The name of the log file. |
||
204 | */ |
||
205 | public static function mwExecBgWithTimeout( |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Executes multiple commands sequentially. |
||
224 | * |
||
225 | * @param array $arrCmds The array of commands to execute. |
||
226 | * @param array|null $out Reference to array for output. |
||
227 | * @param string $logname The log file name. |
||
228 | */ |
||
229 | public static function mwExecCommands(array $arrCmds, ?array &$out = [], string $logname = ''): void |
||
230 | { |
||
231 | $out = []; |
||
232 | foreach ($arrCmds as $cmd) { |
||
233 | $out[] = "$cmd;"; |
||
234 | $outCmd = []; |
||
235 | self::mwExec($cmd, $outCmd); |
||
236 | $out = array_merge($out, $outCmd); |
||
237 | } |
||
238 | |||
239 | if ($logname !== '') { |
||
240 | $result = implode("\n", $out); |
||
241 | file_put_contents( |
||
242 | self::TEMP_SCRIPTS_DIR . "/{$logname}_commands.log", |
||
243 | $result |
||
244 | ); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Restarts all workers in a separate process. |
||
250 | */ |
||
251 | public static function restartAllWorkers(): void |
||
252 | { |
||
253 | $workerSafeScriptsPath = Util::getFilePathByClassName(WorkerSafeScriptsCore::class); |
||
254 | $php = Util::which('php'); |
||
255 | $workerSafeScripts = "$php -f $workerSafeScriptsPath restart > /dev/null 2> /dev/null"; |
||
256 | self::mwExec($workerSafeScripts); |
||
257 | SystemMessages::sysLogMsg( |
||
258 | static::class, |
||
259 | "Service asked for WorkerSafeScriptsCore restart", |
||
260 | LOG_DEBUG |
||
261 | ); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Manages a PHP worker process. |
||
266 | * |
||
267 | * @param string $className The class name of the PHP worker. |
||
268 | * @param string $paramForPHPWorker The parameter for the PHP worker. |
||
269 | * @param string $action The action to perform. |
||
270 | */ |
||
271 | public static function processPHPWorker( |
||
272 | string $className, |
||
273 | string $paramForPHPWorker = 'start', |
||
274 | string $action = 'restart' |
||
275 | ): void { |
||
276 | if (!self::isValidAction($action)) { |
||
277 | throw new InvalidArgumentException("Invalid action: $action"); |
||
278 | } |
||
279 | |||
280 | SystemMessages::sysLogMsg( |
||
281 | __METHOD__, |
||
282 | "processPHPWorker $className action-$action", |
||
283 | LOG_DEBUG |
||
284 | ); |
||
285 | |||
286 | $workerPath = Util::getFilePathByClassName($className); |
||
287 | if (empty($workerPath) || !class_exists($className)) { |
||
288 | return; |
||
289 | } |
||
290 | |||
291 | $php = Util::which('php'); |
||
292 | $command = "$php -f $workerPath"; |
||
293 | $kill = Util::which('kill'); |
||
294 | |||
295 | $activeProcesses = self::getPidOfProcess($className); |
||
296 | $processes = array_filter(explode(' ', $activeProcesses)); |
||
297 | $currentProcCount = count($processes); |
||
298 | |||
299 | $workerObject = new $className(); |
||
300 | $neededProcCount = $workerObject->maxProc; |
||
301 | |||
302 | self::handleWorkerAction( |
||
303 | $action, |
||
304 | $activeProcesses, |
||
305 | $command, |
||
306 | $paramForPHPWorker, |
||
307 | $kill, |
||
308 | $currentProcCount, |
||
309 | $neededProcCount, |
||
310 | $processes |
||
311 | ); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Handles worker process actions. |
||
316 | * |
||
317 | * @param string $action Action to perform |
||
318 | * @param string $activeProcesses Active process IDs |
||
319 | * @param string $command Command to execute |
||
320 | * @param string $paramForPHPWorker Worker parameters |
||
321 | * @param string $kill Kill command path |
||
322 | * @param int $currentProcCount Current process count |
||
323 | * @param int $neededProcCount Needed process count |
||
324 | * @param array $processes Process IDs array |
||
325 | */ |
||
326 | private static function handleWorkerAction( |
||
327 | string $action, |
||
328 | string $activeProcesses, |
||
329 | string $command, |
||
330 | string $paramForPHPWorker, |
||
331 | string $kill, |
||
332 | int $currentProcCount, |
||
333 | int $neededProcCount, |
||
334 | array $processes |
||
335 | ): void { |
||
336 | switch ($action) { |
||
337 | case 'restart': |
||
338 | self::handleRestartAction( |
||
339 | $activeProcesses, |
||
340 | $kill, |
||
341 | $command, |
||
342 | $paramForPHPWorker, |
||
343 | $neededProcCount |
||
344 | ); |
||
345 | break; |
||
346 | case 'stop': |
||
347 | self::handleStopAction($activeProcesses, $kill); |
||
348 | break; |
||
349 | case 'start': |
||
350 | self::handleStartAction( |
||
351 | $currentProcCount, |
||
352 | $neededProcCount, |
||
353 | $command, |
||
354 | $paramForPHPWorker, |
||
355 | $processes, |
||
356 | $kill |
||
357 | ); |
||
358 | break; |
||
359 | } |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Handles the restart action for a worker. |
||
364 | * |
||
365 | * @param string $activeProcesses Active process IDs |
||
366 | * @param string $kill Kill command path |
||
367 | * @param string $command Command to execute |
||
368 | * @param string $paramForPHPWorker Worker parameters |
||
369 | * @param int $neededProcCount Needed process count |
||
370 | */ |
||
371 | private static function handleRestartAction( |
||
372 | string $activeProcesses, |
||
373 | string $kill, |
||
374 | string $command, |
||
375 | string $paramForPHPWorker, |
||
376 | int $neededProcCount |
||
377 | ): void { |
||
378 | if ($activeProcesses !== '') { |
||
379 | self::mwExec("$kill -SIGUSR1 $activeProcesses > /dev/null 2>&1 &"); |
||
380 | self::mwExecBg("$kill -SIGTERM $activeProcesses"); |
||
381 | } |
||
382 | |||
383 | for ($i = 0; $i < $neededProcCount; $i++) { |
||
384 | self::mwExecBg("$command $paramForPHPWorker"); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Handles the stop action for a worker. |
||
390 | * |
||
391 | * @param string $activeProcesses Active process IDs |
||
392 | * @param string $kill Kill command path |
||
393 | */ |
||
394 | private static function handleStopAction(string $activeProcesses, string $kill): void |
||
395 | { |
||
396 | if ($activeProcesses !== '') { |
||
397 | self::mwExec("$kill -SIGUSR2 $activeProcesses > /dev/null 2>&1 &"); |
||
398 | self::mwExecBg("$kill -SIGTERM $activeProcesses"); |
||
399 | } |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Handles the start action for a worker. |
||
404 | * |
||
405 | * @param int $currentProcCount Current process count |
||
406 | * @param int $neededProcCount Needed process count |
||
407 | * @param string $command Command to execute |
||
408 | * @param string $paramForPHPWorker Worker parameters |
||
409 | * @param array $processes Process IDs array |
||
410 | * @param string $kill Kill command path |
||
411 | */ |
||
412 | private static function handleStartAction( |
||
413 | int $currentProcCount, |
||
414 | int $neededProcCount, |
||
415 | string $command, |
||
416 | string $paramForPHPWorker, |
||
417 | array $processes, |
||
418 | string $kill |
||
419 | ): void { |
||
420 | if ($currentProcCount === $neededProcCount) { |
||
421 | return; |
||
422 | } |
||
423 | |||
424 | if ($neededProcCount > $currentProcCount) { |
||
425 | for ($i = $currentProcCount; $i < $neededProcCount; $i++) { |
||
426 | self::mwExecBg("$command $paramForPHPWorker"); |
||
427 | } |
||
428 | } elseif ($currentProcCount > $neededProcCount) { |
||
429 | $countProc4Kill = $currentProcCount - $neededProcCount; |
||
430 | for ($i = 0; $i < $countProc4Kill; $i++) { |
||
431 | if (!isset($processes[$i])) { |
||
432 | break; |
||
433 | } |
||
434 | self::mwExec("$kill -SIGUSR1 {$processes[$i]} > /dev/null 2>&1 &"); |
||
435 | self::mwExecBg("$kill -SIGTERM {$processes[$i]}"); |
||
436 | } |
||
437 | } |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Retrieves the PID of a process by its name. |
||
442 | * |
||
443 | * @param string $name Process name |
||
444 | * @param string $exclude Process name to exclude |
||
445 | * @return string Process IDs |
||
446 | */ |
||
447 | public static function getPidOfProcess(string $name, string $exclude = ''): string |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Executes a command as a background process. |
||
470 | * |
||
471 | * @param string $command Command to execute |
||
472 | * @param string $outFile Output file path |
||
473 | * @param int $sleepTime Sleep time in seconds |
||
474 | */ |
||
475 | public static function mwExecBg( |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Manages a daemon/worker process. |
||
500 | * |
||
501 | * @param string $cmd Command to execute |
||
502 | * @param string $param Command parameters |
||
503 | * @param string $procName Process name |
||
504 | * @param string $action Action to perform |
||
505 | * @param string $outFile Output file path |
||
506 | * |
||
507 | * @return array|bool Process status or operation result |
||
508 | * @throws InvalidArgumentException If action is invalid |
||
509 | */ |
||
510 | public static function processWorker( |
||
511 | string $cmd, |
||
512 | string $param, |
||
513 | string $procName, |
||
514 | string $action, |
||
515 | string $outFile = self::DEFAULT_OUTPUT_FILE |
||
516 | ): bool|array { |
||
517 | if (!self::isValidAction($action)) { |
||
518 | throw new InvalidArgumentException("Invalid action: $action"); |
||
519 | } |
||
520 | |||
521 | $kill = Util::which('kill'); |
||
522 | $nohup = Util::which('nohup'); |
||
523 | $workerPID = self::getPidOfProcess($procName); |
||
524 | |||
525 | return match ($action) { |
||
526 | 'status' => [ |
||
527 | 'status' => ($workerPID !== '') ? 'Started' : 'Stopped', |
||
528 | 'app' => $procName, |
||
529 | 'PID' => $workerPID |
||
530 | ], |
||
531 | 'restart' => self::handleWorkerRestart($kill, $workerPID, $nohup, $cmd, $param, $outFile), |
||
532 | 'stop' => self::handleWorkerStop($kill, $workerPID), |
||
533 | 'start' => self::handleWorkerStart($workerPID, $nohup, $cmd, $param, $outFile), |
||
534 | default => throw new InvalidArgumentException("Unsupported action: $action"), |
||
535 | }; |
||
536 | } |
||
537 | |||
538 | /** |
||
539 | * Handles worker restart operation. |
||
540 | * |
||
541 | * @param string $kill Kill command path |
||
542 | * @param string $workerPID Worker process ID |
||
543 | * @param string $nohup Nohup command path |
||
544 | * @param string $cmd Command to execute |
||
545 | * @param string $param Command parameters |
||
546 | * @param string $outFile Output file path |
||
547 | * @return bool |
||
548 | */ |
||
549 | private static function handleWorkerRestart( |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Handles worker stop operation. |
||
566 | * |
||
567 | * @param string $kill Kill command path |
||
568 | * @param string $workerPID Worker process ID |
||
569 | * @return bool |
||
570 | */ |
||
571 | private static function handleWorkerStop(string $kill, string $workerPID): bool |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * Handles worker start operation. |
||
581 | * |
||
582 | * @param string $workerPID Worker process ID |
||
583 | * @param string $nohup Nohup command path |
||
584 | * @param string $cmd Command to execute |
||
585 | * @param string $param Command parameters |
||
586 | * @param string $outFile Output file path |
||
587 | * @return bool |
||
588 | */ |
||
589 | private static function handleWorkerStart( |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * Starts a daemon process with failure control. |
||
604 | * |
||
605 | * @param string $procName Process name |
||
606 | * @param string $args Process arguments |
||
607 | * @param int $attemptsCount Number of attempts to start |
||
608 | * @param int $timeout Timeout between attempts in microseconds |
||
609 | * @param string $outFile Output file path |
||
610 | * @return bool Success status |
||
611 | */ |
||
612 | public static function safeStartDaemon( |
||
647 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.