Completed
Pull Request — master (#266)
by
unknown
06:19 queued 02:53
created

CustomProcessTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
eloc 56
c 7
b 0
f 1
dl 0
loc 85
rs 10
wmc 19

1 Method

Rating   Name   Duplication   Size   Complexity  
F addCustomProcesses() 0 81 19
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Swoole\Process;
4
5
use Swoole\Http\Server;
6
use Swoole\Process;
7
8
trait CustomProcessTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait CustomProcessTrait
Loading history...
9
{
10
    private $customProcessPidFile = 'laravels-custom-processes.pid';
0 ignored issues
show
Coding Style introduced by
Private member variable "customProcessPidFile" must be prefixed with an underscore
Loading history...
11
12
    public function addCustomProcesses(Server $swoole, $processPrefix, array $processes, array $laravelConfig)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addCustomProcesses()
Loading history...
13
    {
14
        $pidfile = dirname($swoole->setting['pid_file']) . '/' . $this->customProcessPidFile;
15
        if (file_exists($pidfile)) {
16
            unlink($pidfile);
17
        }
18
19
        /**@var []CustomProcessInterface $processList */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
20
        $processList = [];
21
        foreach ($processes as $name => $item) {
22
            if (empty($item['class'])) {
23
                throw new \InvalidArgumentException(sprintf(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
24
                        'process class name must be specified'
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
25
                    )
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
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 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
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
                });
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
52
53
                if (method_exists($processClass, 'onStop')) {
54
                    Process::signal(SIGTERM, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) {
0 ignored issues
show
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...
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

54
                    Process::signal(SIGTERM, 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...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
55
                        $this->info(sprintf('Stopping %s process[PID=%d].', $name, $worker->pid));
56
                        $processClass::onStop($swoole, $worker);
57
                    });
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
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) {
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...
63
                    $coroutineRuntimeAvailable && \Swoole\Runtime::enableCoroutine();
64
                    $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

64
                    $this->/** @scrutinizer ignore-call */ 
65
                           callWithCatchException([$processClass, 'callback'], [$swoole, $worker]);
Loading history...
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
    }
94
}
95