| Conditions | 15 |
| Paths | 26 |
| Total Lines | 84 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 11 | public function addCustomProcesses(Server $swoole, $processPrefix, array $processes, array $laravelConfig) |
||
| 12 | { |
||
| 13 | $pidfile = dirname($swoole->setting['pid_file']) . '/laravels-custom-processes.pid'; |
||
| 14 | if (file_exists($pidfile)) { |
||
| 15 | unlink($pidfile); |
||
| 16 | } |
||
| 17 | |||
| 18 | /**@var []CustomProcessInterface $processList */ |
||
| 19 | $processList = []; |
||
| 20 | foreach ($processes as $item) { |
||
| 21 | if (is_string($item)) { |
||
| 22 | // Backwards compatible |
||
| 23 | Laravel::autoload($laravelConfig['root_path']); |
||
| 24 | $process = $item; |
||
| 25 | $redirect = $process::isRedirectStdinStdout(); |
||
| 26 | $pipe = $process::getPipeType(); |
||
| 27 | } else { |
||
| 28 | if (empty($item['class'])) { |
||
| 29 | throw new \InvalidArgumentException(sprintf( |
||
| 30 | 'process class name must be specified' |
||
| 31 | ) |
||
| 32 | ); |
||
| 33 | } |
||
| 34 | if (isset($item['enable']) && !$item['enable']) { |
||
| 35 | continue; |
||
| 36 | } |
||
| 37 | $process = $item['class']; |
||
| 38 | $redirect = isset($item['redirect']) ? $item['redirect'] : false; |
||
| 39 | $pipe = isset($item['pipe']) ? $item['pipe'] : 0; |
||
| 40 | } |
||
| 41 | |||
| 42 | $processHandler = function (Process $worker) use ($pidfile, $swoole, $processPrefix, $process, $laravelConfig) { |
||
| 43 | file_put_contents($pidfile, $worker->pid . "\n", FILE_APPEND | LOCK_EX); |
||
| 44 | $this->initLaravel($laravelConfig, $swoole); |
||
|
|
|||
| 45 | if (!isset(class_implements($process)[CustomProcessInterface::class])) { |
||
| 46 | throw new \InvalidArgumentException( |
||
| 47 | sprintf( |
||
| 48 | '%s must implement the interface %s', |
||
| 49 | $process, |
||
| 50 | CustomProcessInterface::class |
||
| 51 | ) |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | $name = $process::getName() ?: 'custom'; |
||
| 55 | $this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name)); |
||
| 56 | |||
| 57 | Process::signal(SIGUSR1, function ($signo) use ($name, $process, $worker, $pidfile, $swoole) { |
||
| 58 | $this->info(sprintf('Reloading the process %s [pid=%d].', $name, $worker->pid)); |
||
| 59 | $process::onReload($swoole, $worker); |
||
| 60 | }); |
||
| 61 | |||
| 62 | $enableCoroutine = class_exists('Swoole\Coroutine'); |
||
| 63 | $runProcess = function () use ($name, $process, $swoole, $worker, $enableCoroutine) { |
||
| 64 | $maxTry = 10; |
||
| 65 | $i = 0; |
||
| 66 | do { |
||
| 67 | $this->callWithCatchException([$process, 'callback'], [$swoole, $worker]); |
||
| 68 | ++$i; |
||
| 69 | if ($enableCoroutine) { |
||
| 70 | \Swoole\Coroutine::sleep(1); |
||
| 71 | } else { |
||
| 72 | sleep(1); |
||
| 73 | } |
||
| 74 | } while ($i < $maxTry); |
||
| 75 | $this->error( |
||
| 76 | sprintf( |
||
| 77 | 'The custom process "%s" reaches the maximum number of retries %d times, and will be restarted by the manager process.', |
||
| 78 | $name, |
||
| 79 | $maxTry |
||
| 80 | ) |
||
| 81 | ); |
||
| 82 | }; |
||
| 83 | $enableCoroutine ? go($runProcess) : $runProcess(); |
||
| 84 | }; |
||
| 85 | $customProcess = new Process( |
||
| 86 | $processHandler, |
||
| 87 | $redirect, |
||
| 88 | $pipe |
||
| 89 | ); |
||
| 90 | if ($swoole->addProcess($customProcess)) { |
||
| 91 | $processList[] = $customProcess; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | return $processList; |
||
| 95 | } |
||
| 97 |