|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole\Timer; |
|
4
|
|
|
|
|
5
|
|
|
use Swoole\Http\Server; |
|
6
|
|
|
use Swoole\Process; |
|
7
|
|
|
use Swoole\Timer; |
|
8
|
|
|
|
|
9
|
|
|
trait TimerTrait |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
private $timerPidFile = 'laravels-timer-process.pid'; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
public function addTimerProcess(Server $swoole, array $config, array $laravelConfig) |
|
|
|
|
|
|
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'])); |
|
|
|
|
|
|
31
|
|
|
$this->initLaravel($laravelConfig, $swoole); |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
$process = new Process($callback, false, 0); |
|
55
|
|
|
$swoole->addProcess($process); |
|
56
|
|
|
return $process; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function registerTimers(array $jobs) |
|
|
|
|
|
|
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( |
|
|
|
|
|
|
70
|
|
|
'%s must extend the abstract class %s', |
|
|
|
|
|
|
71
|
|
|
get_class($job), |
|
|
|
|
|
|
72
|
|
|
CronJob::class |
|
|
|
|
|
|
73
|
|
|
) |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
82
|
|
|
if (($job instanceof CheckGlobalTimerAliveCronJob) || $job::isEnable()) { |
|
83
|
|
|
$job->run(); |
|
84
|
|
|
} |
|
85
|
|
|
}); |
|
|
|
|
|
|
86
|
|
|
}; |
|
87
|
|
|
class_exists('Swoole\Coroutine') ? \Swoole\Coroutine::create($runCallback) : $runCallback(); |
|
88
|
|
|
}; |
|
89
|
|
|
|
|
90
|
|
|
$timerId = Timer::tick($job->interval(), $runJob); |
|
|
|
|
|
|
91
|
|
|
$timerIds[] = $timerId; |
|
92
|
|
|
$job->setTimerId($timerId); |
|
93
|
|
|
if ($job->isImmediate()) { |
|
94
|
|
|
Timer::after(1, $runJob); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
return $timerIds; |
|
98
|
|
|
} |
|
99
|
|
|
} |