hhxsv5 /
laravel-s
| 1 | <?php |
||||||
| 2 | |||||||
|
0 ignored issues
–
show
Coding Style
introduced
by
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
|
|||||||
| 11 | { |
||||||
| 12 | private $timerPidFile = 'laravels-timer-process.pid'; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 13 | |||||||
| 14 | public function addTimerProcess(Server $swoole, array $config, array $laravelConfig) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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
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
Loading history...
|
|||||||
| 32 | $this->initLaravel($laravelConfig, $swoole); |
||||||
|
0 ignored issues
–
show
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
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
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
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...
|
|||||||
| 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
|
|||||||
| 49 | $process->exit(0); |
||||||
| 50 | }); |
||||||
|
0 ignored issues
–
show
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
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
|
|||||||
| 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
|
|||||||
| 73 | '%s must extend the abstract class %s', |
||||||
|
0 ignored issues
–
show
|
|||||||
| 74 | get_class($job), |
||||||
|
0 ignored issues
–
show
|
|||||||
| 75 | CronJob::class |
||||||
|
0 ignored issues
–
show
|
|||||||
| 76 | ) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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
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
Loading history...
|
|||||||
| 85 | if (($job instanceof CheckGlobalTimerAliveCronJob) || $job::isEnable()) { |
||||||
| 86 | $job->run(); |
||||||
| 87 | } |
||||||
| 88 | }); |
||||||
|
0 ignored issues
–
show
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 | } |