| Conditions | 19 |
| Paths | 166 |
| Total Lines | 81 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| 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 []CustomProcessInterface $processList */ |
||
| 20 | $processList = []; |
||
| 21 | foreach ($processes as $name => $item) { |
||
| 22 | if (empty($item['class'])) { |
||
| 23 | throw new \InvalidArgumentException(sprintf( |
||
| 24 | 'process class name must be specified' |
||
| 25 | ) |
||
| 26 | ); |
||
| 27 | } |
||
| 28 | if (isset($item['enable']) && !$item['enable']) { |
||
| 29 | continue; |
||
| 30 | } |
||
| 31 | $processClass = $item['class']; |
||
| 32 | $restartInterval = isset($item['restart_interval']) ? (int)$item['restart_interval'] : 5; |
||
| 33 | $callback = function (Process $worker) use ($pidfile, $swoole, $processPrefix, $processClass, $restartInterval, $name, $laravelConfig) { |
||
| 34 | file_put_contents($pidfile, $worker->pid . "\n", FILE_APPEND | LOCK_EX); |
||
| 35 | $this->initLaravel($laravelConfig, $swoole); |
||
| 36 | if (!isset(class_implements($processClass)[CustomProcessInterface::class])) { |
||
| 37 | throw new \InvalidArgumentException( |
||
| 38 | sprintf( |
||
| 39 | '%s must implement the interface %s', |
||
| 40 | $processClass, |
||
| 41 | CustomProcessInterface::class |
||
| 42 | ) |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | /**@var CustomProcessInterface $processClass */ |
||
| 46 | $this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name)); |
||
| 47 | |||
| 48 | Process::signal(SIGUSR1, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) { |
||
| 49 | $this->info(sprintf('Reloading %s process[PID=%d].', $name, $worker->pid)); |
||
| 50 | $processClass::onReload($swoole, $worker); |
||
| 51 | }); |
||
| 52 | |||
| 53 | if (method_exists($processClass, 'onStop')) { |
||
| 54 | Process::signal(SIGTERM, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) { |
||
| 55 | $this->info(sprintf('Stopping %s process[PID=%d].', $name, $worker->pid)); |
||
| 56 | $processClass::onStop($swoole, $worker); |
||
| 57 | }); |
||
| 58 | } |
||
| 59 | |||
| 60 | $coroutineAvailable = class_exists('Swoole\Coroutine'); |
||
| 61 | $coroutineRuntimeAvailable = class_exists('Swoole\Runtime'); |
||
| 62 | $runProcess = function () use ($name, $processClass, $restartInterval, $swoole, $worker, $coroutineAvailable, $coroutineRuntimeAvailable) { |
||
| 63 | $coroutineRuntimeAvailable && \Swoole\Runtime::enableCoroutine(); |
||
| 64 | $this->callWithCatchException([$processClass, 'callback'], [$swoole, $worker]); |
||
| 65 | // Avoid frequent process creation |
||
| 66 | if ($coroutineAvailable) { |
||
| 67 | \Swoole\Coroutine::sleep($restartInterval); |
||
| 68 | swoole_event_exit(); |
||
| 69 | } else { |
||
| 70 | sleep($restartInterval); |
||
| 71 | } |
||
| 72 | }; |
||
| 73 | $coroutineAvailable ? \Swoole\Coroutine::create($runProcess) : $runProcess(); |
||
| 74 | }; |
||
| 75 | |||
| 76 | $redirect = isset($item['redirect']) ? $item['redirect'] : false; |
||
| 77 | $pipe = isset($item['pipe']) ? $item['pipe'] : 0; |
||
| 78 | $process = new Process($callback, $redirect, $pipe); |
||
| 79 | if (isset($item['queue'])) { |
||
| 80 | if (empty($item['queue'])) { |
||
| 81 | $process->useQueue(); |
||
| 82 | } else { |
||
| 83 | $msgKey = isset($item['msg_key']) ? $item['msg_key'] : 0; |
||
| 84 | $mode = isset($item['mode']) ? $item['mode'] : 2; |
||
| 85 | $capacity = isset($item['capacity']) ? $item['capacity'] : -1; |
||
| 86 | $process->useQueue($msgKey, $mode, $capacity); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | $swoole->addProcess($process); |
||
| 90 | $processList[$name] = $process; |
||
| 91 | } |
||
| 92 | return $processList; |
||
| 93 | } |
||
| 95 |