Completed
Push — master ( 17cb81...69f72e )
by Biao
06:46 queued 02:50
created

CustomProcessTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 52
c 5
b 0
f 1
dl 0
loc 78
rs 10
wmc 18

1 Method

Rating   Name   Duplication   Size   Complexity  
D addCustomProcesses() 0 74 18
1
<?php
2
3
namespace Hhxsv5\LaravelS\Swoole\Process;
4
5
use Swoole\Http\Server;
6
use Swoole\Process;
7
8
trait CustomProcessTrait
9
{
10
    private $customProcessPidFile = 'laravels-custom-processes.pid';
11
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);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

35
                $this->/** @scrutinizer ignore-call */ 
36
                       initLaravel($laravelConfig, $swoole);
Loading history...
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));
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

46
                $this->/** @scrutinizer ignore-call */ 
47
                       setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name));
Loading history...
47
48
                Process::signal(SIGUSR1, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) {
0 ignored issues
show
Unused Code introduced by
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 ignore-unused  annotation

48
                Process::signal(SIGUSR1, function (/** @scrutinizer ignore-unused */ $signo) use ($name, $processClass, $worker, $pidfile, $swoole) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The import $pidfile is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
49
                    $this->info(sprintf('Reloading %s process[PID=%d].', $name, $worker->pid));
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

49
                    $this->/** @scrutinizer ignore-call */ 
50
                           info(sprintf('Reloading %s process[PID=%d].', $name, $worker->pid));
Loading history...
50
                    $processClass::onReload($swoole, $worker);
51
                });
52
53
                $coroutineAvailable = class_exists('Swoole\Coroutine');
54
                $coroutineRuntimeAvailable = class_exists('Swoole\Runtime');
55
                $runProcess = function () use ($name, $processClass, $restartInterval, $swoole, $worker, $coroutineAvailable, $coroutineRuntimeAvailable) {
0 ignored issues
show
Unused Code introduced by
The import $name is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
56
                    $coroutineRuntimeAvailable && \Swoole\Runtime::enableCoroutine();
57
                    $this->callWithCatchException([$processClass, 'callback'], [$swoole, $worker]);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

57
                    $this->/** @scrutinizer ignore-call */ 
58
                           callWithCatchException([$processClass, 'callback'], [$swoole, $worker]);
Loading history...
58
                    // Avoid frequent process creation
59
                    if ($coroutineAvailable) {
60
                        \Swoole\Coroutine::sleep($restartInterval);
61
                        swoole_event_exit();
62
                    } else {
63
                        sleep($restartInterval);
64
                    }
65
                };
66
                $coroutineAvailable ? \Swoole\Coroutine::create($runProcess) : $runProcess();
67
            };
68
69
            $redirect = isset($item['redirect']) ? $item['redirect'] : false;
70
            $pipe = isset($item['pipe']) ? $item['pipe'] : 0;
71
            $process = new Process($callback, $redirect, $pipe);
0 ignored issues
show
Bug introduced by
It seems like $pipe can also be of type integer; however, parameter $create_pipe of Swoole\Process::__construct() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            $process = new Process($callback, $redirect, /** @scrutinizer ignore-type */ $pipe);
Loading history...
72
            if (isset($item['queue'])) {
73
                if (empty($item['queue'])) {
74
                    $process->useQueue();
75
                } else {
76
                    $msgKey = isset($item['msg_key']) ? $item['msg_key'] : 0;
77
                    $mode = isset($item['mode']) ? $item['mode'] : 2;
78
                    $capacity = isset($item['capacity']) ? $item['capacity'] : -1;
79
                    $process->useQueue($msgKey, $mode, $capacity);
0 ignored issues
show
Unused Code introduced by
The call to Swoole\Process::useQueue() has too many arguments starting with $capacity. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
                    $process->/** @scrutinizer ignore-call */ 
80
                              useQueue($msgKey, $mode, $capacity);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
80
                }
81
            }
82
            $swoole->addProcess($process);
83
            $processList[$name] = $process;
84
        }
85
        return $processList;
86
    }
87
}
88