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