Conditions | 14 |
Paths | 18 |
Total Lines | 79 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
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 | if (class_exists('Swoole\Runtime')) { |
||
61 | \Swoole\Runtime::enableCoroutine(); |
||
62 | } |
||
63 | |||
64 | $this->callWithCatchException([$processClass, 'callback'], [$swoole, $worker]); |
||
65 | |||
66 | // Avoid frequent process creation |
||
67 | if (class_exists('Swoole\Coroutine')) { |
||
68 | \Swoole\Coroutine::sleep($restartInterval); |
||
69 | } else { |
||
70 | sleep($restartInterval); |
||
71 | } |
||
72 | }; |
||
73 | |||
74 | // for multiple processes |
||
75 | if (isset($item['num']) && $item['num'] > 1) { |
||
76 | for ($i = 0; $i < $item['num']; $i++) { |
||
77 | $process = $this->makeProcess($callback, $item); |
||
78 | $swoole->addProcess($process); |
||
79 | $processList[$name . $i] = $process; |
||
80 | } |
||
81 | |||
82 | return $processList; |
||
83 | } |
||
84 | |||
85 | // for single process |
||
86 | $process = $this->makeProcess($callback, $item); |
||
87 | $swoole->addProcess($process); |
||
88 | $processList[$name] = $process; |
||
89 | } |
||
90 | return $processList; |
||
91 | } |
||
119 |