1 | <?php |
||||||
2 | |||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||||
3 | namespace Hhxsv5\LaravelS\Swoole\Process; |
||||||
4 | |||||||
5 | use Swoole\Http\Server; |
||||||
6 | use Swoole\Process; |
||||||
7 | |||||||
8 | trait CustomProcessTrait |
||||||
0 ignored issues
–
show
|
|||||||
9 | { |
||||||
10 | private $customProcessPidFile = 'laravels-custom-processes.pid'; |
||||||
0 ignored issues
–
show
|
|||||||
11 | |||||||
12 | public function addCustomProcesses(Server $swoole, $processPrefix, array $processes, array $laravelConfig) |
||||||
0 ignored issues
–
show
|
|||||||
13 | { |
||||||
14 | $pidfile = dirname($swoole->setting['pid_file']) . '/' . $this->customProcessPidFile; |
||||||
15 | if (file_exists($pidfile)) { |
||||||
16 | unlink($pidfile); |
||||||
17 | } |
||||||
18 | |||||||
19 | /**@var []Process $processList */ |
||||||
0 ignored issues
–
show
|
|||||||
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); |
||||||
0 ignored issues
–
show
It seems like
initLaravel() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
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 */ |
||||||
0 ignored issues
–
show
|
|||||||
43 | $this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name)); |
||||||
0 ignored issues
–
show
It seems like
setProcessTitle() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
44 | |||||||
45 | Process::signal(SIGUSR1, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) { |
||||||
0 ignored issues
–
show
The parameter
$signo is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
46 | $this->info(sprintf('Reloading %s process[PID=%d].', $name, $worker->pid)); |
||||||
0 ignored issues
–
show
It seems like
info() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
47 | $processClass::onReload($swoole, $worker); |
||||||
48 | }); |
||||||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||||||
49 | |||||||
50 | if (method_exists($processClass, 'onStop')) { |
||||||
51 | Process::signal(SIGTERM, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) { |
||||||
0 ignored issues
–
show
The parameter
$signo is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
52 | $this->info(sprintf('Stopping %s process[PID=%d].', $name, $worker->pid)); |
||||||
53 | $processClass::onStop($swoole, $worker); |
||||||
54 | }); |
||||||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||||||
55 | } |
||||||
56 | |||||||
57 | if (class_exists('Swoole\Runtime')) { |
||||||
58 | \Swoole\Runtime::enableCoroutine(); |
||||||
59 | } |
||||||
60 | |||||||
61 | $this->callWithCatchException([$processClass, 'callback'], [$swoole, $worker]); |
||||||
0 ignored issues
–
show
It seems like
callWithCatchException() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
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 | } |
||||||
85 | |||||||
86 | /** |
||||||
0 ignored issues
–
show
|
|||||||
87 | * @param callable $callback |
||||||
0 ignored issues
–
show
|
|||||||
88 | * @param array $config |
||||||
0 ignored issues
–
show
|
|||||||
89 | * @return Process |
||||||
0 ignored issues
–
show
|
|||||||
90 | */ |
||||||
91 | public function makeProcess(callable $callback, array $config) |
||||||
92 | { |
||||||
93 | $redirect = isset($config['redirect']) ? $config['redirect'] : false; |
||||||
94 | $pipe = isset($config['pipe']) ? $config['pipe'] : 0; |
||||||
95 | $process = version_compare(SWOOLE_VERSION, '4.3.0', '>=') |
||||||
96 | ? new Process($callback, $redirect, $pipe, class_exists('Swoole\Coroutine')) |
||||||
97 | : new Process($callback, $redirect, $pipe); |
||||||
98 | if (isset($config['queue'])) { |
||||||
99 | if (empty($config['queue'])) { |
||||||
100 | $process->useQueue(); |
||||||
101 | } else { |
||||||
102 | $msgKey = isset($config['queue']['msg_key']) ? $config['queue']['msg_key'] : 0; |
||||||
103 | $mode = isset($config['queue']['mode']) ? $config['queue']['mode'] : 2; |
||||||
104 | $capacity = isset($config['queue']['capacity']) ? $config['queue']['capacity'] : -1; |
||||||
105 | $process->useQueue($msgKey, $mode, $capacity); |
||||||
106 | } |
||||||
107 | } |
||||||
108 | |||||||
109 | return $process; |
||||||
110 | } |
||||||
111 | } |
||||||
112 |