Completed
Push — master ( f0c027...250233 )
by Biao
13:36
created

TimerTrait::addTimerProcess()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
cc 7
eloc 24
c 5
b 1
f 1
nc 3
nop 3
dl 0
loc 44
rs 8.6026
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Swoole\Timer;
4
5
use Swoole\Http\Server;
6
use Swoole\Process;
7
use Swoole\Timer;
8
9
trait TimerTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait TimerTrait
Loading history...
10
{
11
    private $timerPidFile = 'laravels-timer-process.pid';
0 ignored issues
show
Coding Style introduced by
Private member variable "timerPidFile" must be prefixed with an underscore
Loading history...
12
13
    public function addTimerProcess(Server $swoole, array $config, array $laravelConfig)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addTimerProcess()
Loading history...
14
    {
15
        if (empty($config['enable']) || empty($config['jobs'])) {
16
            return false;
17
        }
18
19
        // Add backup cron job.
20
        $config['jobs'][] = BackupCronJob::class;
21
        if (!empty($config['global'])) {
22
            // Add auxiliary jobs for global timer.
23
            $config['jobs'][] = RenewGlobalTimerLockCronJob::class;
24
            $config['jobs'][] = CheckGlobalTimerAliveCronJob::class;
25
        }
26
27
        $callback = function (Process $process) use ($swoole, $config, $laravelConfig) {
28
            $pidfile = dirname($swoole->setting['pid_file']) . '/' . $this->timerPidFile;
29
            file_put_contents($pidfile, $process->pid);
30
            $this->setProcessTitle(sprintf('%s laravels: timer process', $config['process_prefix']));
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

30
            $this->/** @scrutinizer ignore-call */ 
31
                   setProcessTitle(sprintf('%s laravels: timer process', $config['process_prefix']));
Loading history...
31
            $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

31
            $this->/** @scrutinizer ignore-call */ 
32
                   initLaravel($laravelConfig, $swoole);
Loading history...
32
33
            // Implement global timer by Cache lock.
34
            if (!empty($config['global'])) {
35
                CronJob::setGlobalTimerLockKey($config['lock_key']);
36
                // Close all cron job if not get the lock
37
                CronJob::setEnable(CronJob::getGlobalTimerLock());
38
            }
39
40
            $timerIds = $this->registerTimers($config['jobs']);
41
42
            Process::signal(SIGUSR1, function ($signo) use ($config, $timerIds, $process) {
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

42
            Process::signal(SIGUSR1, function (/** @scrutinizer ignore-unused */ $signo) use ($config, $timerIds, $process) {

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...
43
                foreach ($timerIds as $timerId) {
44
                    if (Timer::exists($timerId)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of Swoole\Timer::exists($timerId) targeting Swoole\Timer::exists() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
                        Timer::clear($timerId);
46
                    }
47
                }
48
                Timer::after($config['max_wait_time'] * 1000, function () use ($process) {
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...
49
                    $process->exit(0);
50
                });
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...
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
54
        $process = new Process($callback, false, 0);
55
        $swoole->addProcess($process);
56
        return $process;
57
    }
58
59
    public function registerTimers(array $jobs)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function registerTimers()
Loading history...
60
    {
61
        $timerIds = [];
62
        foreach ($jobs as $jobClass) {
63
            if (is_array($jobClass) && isset($jobClass[0])) {
64
                $job = new $jobClass[0](isset($jobClass[1]) ? $jobClass[1] : []);
65
            } else {
66
                $job = new $jobClass();
67
            }
68
            if (!($job instanceof CronJob)) {
69
                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...
70
                        '%s must extend the abstract class %s',
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...
71
                        get_class($job),
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...
72
                        CronJob::class
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...
73
                    )
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...
74
                );
75
            }
76
            if (empty($job->interval())) {
77
                throw new \InvalidArgumentException(sprintf('The interval of %s cannot be empty', get_class($job)));
78
            }
79
            $runJob = function () use ($job) {
80
                $runCallback = function () use ($job) {
81
                    $this->callWithCatchException(function () use ($job) {
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

81
                    $this->/** @scrutinizer ignore-call */ 
82
                           callWithCatchException(function () use ($job) {
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...
82
                        if (($job instanceof CheckGlobalTimerAliveCronJob) || $job::isEnable()) {
83
                            $job->run();
84
                        }
85
                    });
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...
86
                };
87
                class_exists('Swoole\Coroutine') ? \Swoole\Coroutine::create($runCallback) : $runCallback();
88
            };
89
90
            $timerId = Timer::tick($job->interval(), $runJob);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $timerId is correct as Swoole\Timer::tick($job->interval(), $runJob) targeting Swoole\Timer::tick() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
            $timerIds[] = $timerId;
92
            $job->setTimerId($timerId);
93
            if ($job->isImmediate()) {
94
                Timer::after(1, $runJob);
95
            }
96
        }
97
        return $timerIds;
98
    }
99
}