| Conditions | 14 |
| Paths | 14 |
| Total Lines | 72 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 11 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 12 | public function addCustomProcesses(Server $swoole, $processPrefix, array $processes, array $laravelConfig) |
||
| 13 | { |
||
| 14 | $pidfile = dirname($swoole->setting['pid_file']) . '/' . $this->customProcessPidFile; |
||
| 15 | if (file_exists($pidfile)) { |
||
| 16 | unlink($pidfile); |
||
| 17 | } |
||
| 18 | |||
| 19 | /**@var []Process $processList */ |
||
| 20 | $processList = []; |
||
| 21 | foreach ($processes as $name => $item) { |
||
| 22 | if (empty($item['class'])) { |
||
| 23 | throw new \InvalidArgumentException(sprintf('The class of process %s must be specified', $name)); |
||
| 24 | } |
||
| 25 | if (isset($item['enable']) && !$item['enable']) { |
||
| 26 | continue; |
||
| 27 | } |
||
| 28 | $processClass = $item['class']; |
||
| 29 | $restartInterval = isset($item['restart_interval']) ? (int)$item['restart_interval'] : 5; |
||
| 30 | $callback = function (Process $worker) use ($pidfile, $swoole, $processPrefix, $processClass, $restartInterval, $name, $laravelConfig) { |
||
| 31 | file_put_contents($pidfile, $worker->pid . "\n", FILE_APPEND | LOCK_EX); |
||
| 32 | $this->initLaravel($laravelConfig, $swoole); |
||
| 33 | if (!isset(class_implements($processClass)[CustomProcessInterface::class])) { |
||
| 34 | throw new \InvalidArgumentException( |
||
| 35 | sprintf( |
||
| 36 | '%s must implement the interface %s', |
||
| 37 | $processClass, |
||
| 38 | CustomProcessInterface::class |
||
| 39 | ) |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | /**@var CustomProcessInterface $processClass */ |
||
| 43 | $this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name)); |
||
| 44 | |||
| 45 | Process::signal(SIGUSR1, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) { |
||
| 46 | $this->info(sprintf('Reloading %s process[PID=%d].', $name, $worker->pid)); |
||
| 47 | $processClass::onReload($swoole, $worker); |
||
| 48 | }); |
||
| 49 | |||
| 50 | if (method_exists($processClass, 'onStop')) { |
||
| 51 | Process::signal(SIGTERM, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) { |
||
| 52 | $this->info(sprintf('Stopping %s process[PID=%d].', $name, $worker->pid)); |
||
| 53 | $processClass::onStop($swoole, $worker); |
||
| 54 | }); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (class_exists('Swoole\Runtime')) { |
||
| 58 | \Swoole\Runtime::enableCoroutine(); |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->callWithCatchException([$processClass, 'callback'], [$swoole, $worker]); |
||
| 62 | |||
| 63 | // Avoid frequent process creation |
||
| 64 | if (class_exists('Swoole\Coroutine')) { |
||
| 65 | \Swoole\Coroutine::sleep($restartInterval); |
||
| 66 | } else { |
||
| 67 | sleep($restartInterval); |
||
| 68 | } |
||
| 69 | }; |
||
| 70 | |||
| 71 | if (isset($item['num']) && $item['num'] > 1) { // For multiple processes |
||
| 72 | for ($i = 0; $i < $item['num']; $i++) { |
||
| 73 | $process = $this->makeProcess($callback, $item); |
||
| 74 | $swoole->addProcess($process); |
||
| 75 | $processList[$name . $i] = $process; |
||
| 76 | } |
||
| 77 | } else { // For single process |
||
| 78 | $process = $this->makeProcess($callback, $item); |
||
| 79 | $swoole->addProcess($process); |
||
| 80 | $processList[$name] = $process; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | return $processList; |
||
| 84 | } |
||
| 112 |