CustomProcessTrait   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 13
Bugs 1 Features 1
Metric Value
eloc 61
c 13
b 1
f 1
dl 0
loc 102
rs 10
wmc 23

2 Methods

Rating   Name   Duplication   Size   Complexity  
C addCustomProcesses() 0 72 14
B makeProcess() 0 19 9
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 []Process $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('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
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

32
                $this->/** @scrutinizer ignore-call */ 
33
                       initLaravel($laravelConfig, $swoole);
Loading history...
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
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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...
43
                $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

43
                $this->/** @scrutinizer ignore-call */ 
44
                       setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name));
Loading history...
44
45
                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

45
                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...
46
                    $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

46
                    $this->/** @scrutinizer ignore-call */ 
47
                           info(sprintf('Reloading %s process[PID=%d].', $name, $worker->pid));
Loading history...
47
                    $processClass::onReload($swoole, $worker);
48
                });
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...
49
50
                if (method_exists($processClass, 'onStop')) {
51
                    Process::signal(SIGTERM, 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

51
                    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...
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...
52
                        $this->info(sprintf('Stopping %s process[PID=%d].', $name, $worker->pid));
53
                        $processClass::onStop($swoole, $worker);
54
                    });
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...
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
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

61
                $this->/** @scrutinizer ignore-call */ 
62
                       callWithCatchException([$processClass, 'callback'], [$swoole, $worker]);
Loading history...
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
Coding Style introduced by
Missing short description in doc comment
Loading history...
87
     * @param callable $callback
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
88
     * @param array $config
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
89
     * @return Process
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
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