| Conditions | 16 |
| Paths | 27 |
| Total Lines | 71 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 120 | public static function processPHPWorker( |
||
| 121 | string $className, |
||
| 122 | string $paramForPHPWorker = 'start', |
||
| 123 | string $action = 'restart' |
||
| 124 | ): void { |
||
| 125 | $workerPath = Util::getFilePathByClassName($className); |
||
| 126 | if (empty($workerPath)) { |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | $command = "php -f {$workerPath}"; |
||
| 130 | $path_kill = Util::which('kill'); |
||
| 131 | $activeProcesses = self::getPidOfProcess($className); |
||
| 132 | $processes = explode(' ', $activeProcesses); |
||
| 133 | if (empty($processes[0])) { |
||
| 134 | array_shift($processes); |
||
| 135 | } |
||
| 136 | $currentProcCount = count($processes); |
||
| 137 | |||
| 138 | if ( ! class_exists($className)) { |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | $workerObject = new $className(); |
||
| 142 | $neededProcCount = $workerObject->maxProc; |
||
| 143 | |||
| 144 | switch ($action) { |
||
| 145 | case 'restart': |
||
| 146 | // Stop all old workers |
||
| 147 | if ($activeProcesses !== '') { |
||
| 148 | self::mwExec("{$path_kill} SIGUSR1 {$activeProcesses} > /dev/null 2>&1 &"); |
||
| 149 | self::mwExecBgWithTimeout("{$path_kill} SIGTERM {$activeProcesses}", 10); |
||
| 150 | $currentProcCount = 0; |
||
| 151 | } |
||
| 152 | |||
| 153 | // Start new processes |
||
| 154 | while ($currentProcCount < $neededProcCount) { |
||
| 155 | self::mwExecBg("{$command} {$paramForPHPWorker}"); |
||
| 156 | $currentProcCount++; |
||
| 157 | } |
||
| 158 | |||
| 159 | break; |
||
| 160 | case 'stop': |
||
| 161 | if ($activeProcesses !== '') { |
||
| 162 | self::mwExec("{$path_kill} SIGUSR1 {$activeProcesses} > /dev/null 2>&1 &"); |
||
| 163 | self::mwExecBgWithTimeout("{$path_kill} SIGTERM {$activeProcesses}", 10); |
||
| 164 | } |
||
| 165 | break; |
||
| 166 | case 'start': |
||
| 167 | if ($currentProcCount === $neededProcCount) { |
||
| 168 | return; |
||
| 169 | } elseif ($neededProcCount > $currentProcCount) { |
||
| 170 | // Start additional processes |
||
| 171 | while ($currentProcCount < $neededProcCount) { |
||
| 172 | self::mwExecBg("{$command} {$paramForPHPWorker}"); |
||
| 173 | $currentProcCount++; |
||
| 174 | } |
||
| 175 | } elseif ($currentProcCount > $neededProcCount) { |
||
| 176 | // Find redundant processes |
||
| 177 | $countProc4Kill = $neededProcCount - $currentProcCount; |
||
| 178 | // Send SIGUSR1 command to them |
||
| 179 | while ($countProc4Kill >= 0) { |
||
| 180 | if ( ! isset($processes[$countProc4Kill])) { |
||
| 181 | break; |
||
| 182 | } |
||
| 183 | // Kill old processes with timeout, maybe it is soft restart and worker die without any help |
||
| 184 | self::mwExec("{$path_kill} SIGUSR1 {$processes[$countProc4Kill]} > /dev/null 2>&1 &"); |
||
| 185 | self::mwExecBgWithTimeout("{$path_kill} SIGTERM {$processes[$countProc4Kill]}", 10); |
||
| 186 | $countProc4Kill--; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | break; |
||
| 190 | default: |
||
| 191 | } |
||
| 291 | } |