TimerTrait::addTimerProcess()   B
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 1 Features 1
Metric Value
cc 7
eloc 25
c 8
b 1
f 1
nc 3
nop 3
dl 0
loc 46
rs 8.5866
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\Event;
6
use Swoole\Http\Server;
7
use Swoole\Process;
8
use Swoole\Timer;
9
10
trait TimerTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait TimerTrait
Loading history...
11
{
12
    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...
13
14
    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...
15
    {
16
        if (empty($config['enable']) || empty($config['jobs'])) {
17
            return false;
18
        }
19
20
        // Add backup cron job.
21
        $config['jobs'][] = BackupCronJob::class;
22
        if (!empty($config['global_lock'])) {
23
            // Add auxiliary jobs for global timer.
24
            $config['jobs'][] = RenewGlobalTimerLockCronJob::class;
25
            $config['jobs'][] = CheckGlobalTimerAliveCronJob::class;
26
        }
27
28
        $callback = function (Process $process) use ($swoole, $config, $laravelConfig) {
29
            $pidfile = dirname($swoole->setting['pid_file']) . '/' . $this->timerPidFile;
30
            file_put_contents($pidfile, $process->pid);
31
            $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

31
            $this->/** @scrutinizer ignore-call */ 
32
                   setProcessTitle(sprintf('%s laravels: timer process', $config['process_prefix']));
Loading history...
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
34
            // Implement global timer by Cache lock.
35
            if (!empty($config['global_lock'])) {
36
                CronJob::setGlobalTimerLockKey($config['global_lock_key']);
37
                CronJob::checkSetEnable();
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)) {
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
            // For Swoole 4.6.x
53
            // Deprecated: Swoole\Event::rshutdown(): Event::wait() in shutdown function is deprecated in Unknown on line 0
54
            Event::wait();
55
        };
56
57
        $process = new Process($callback, false, 0);
58
        $swoole->addProcess($process);
59
        return $process;
60
    }
61
62
    public function registerTimers(array $jobs)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function registerTimers()
Loading history...
63
    {
64
        $timerIds = [];
65
        foreach ($jobs as $jobClass) {
66
            if (is_array($jobClass) && isset($jobClass[0])) {
67
                $job = new $jobClass[0](isset($jobClass[1]) ? $jobClass[1] : []);
68
            } else {
69
                $job = new $jobClass();
70
            }
71
            if (!($job instanceof CronJob)) {
72
                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...
73
                        '%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...
74
                        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...
75
                        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...
76
                    )
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...
77
                );
78
            }
79
            if (empty($job->interval())) {
80
                throw new \InvalidArgumentException(sprintf('The interval of %s cannot be empty', get_class($job)));
81
            }
82
            $runJob = function () use ($job) {
83
                $runCallback = function () use ($job) {
84
                    $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

84
                    $this->/** @scrutinizer ignore-call */ 
85
                           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...
85
                        if (($job instanceof CheckGlobalTimerAliveCronJob) || $job::isEnable()) {
86
                            $job->run();
87
                        }
88
                    });
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...
89
                };
90
                class_exists('Swoole\Coroutine') ? \Swoole\Coroutine::create($runCallback) : $runCallback();
91
            };
92
93
            $timerId = Timer::tick($job->interval(), $runJob);
94
            $timerIds[] = $timerId;
95
            $job->setTimerId($timerId);
96
            if ($job->isImmediate()) {
97
                Timer::after(1, $runJob);
98
            }
99
        }
100
        return $timerIds;
101
    }
102
}