This commit is contained in:
2021-03-29 10:40:47 +08:00
parent 38748d1f8c
commit 4d69d4bfaf
+48 -45
View File
@@ -24,65 +24,68 @@ class ZookeeperProcess extends Process
{ {
private Channel $channel; private Channel $channel;
private WaitGroup $waitGroup; private WaitGroup $waitGroup;
/** @var Crontab[] $names */ /** @var Crontab[] $names */
public array $names = []; public array $names = [];
public array $scores = []; public array $scores = [];
public array $timers = []; public array $timers = [];
/** /**
* @param \Swoole\Process $process * @param \Swoole\Process $process
* @throws ReflectionException * @throws ReflectionException
* @throws ComponentException * @throws ComponentException
* @throws NotFindClassException * @throws NotFindClassException
*/ * @throws Exception
public function onHandler(\Swoole\Process $process): void */
{ public function onHandler(\Swoole\Process $process): void
$crontab = Snowflake::app()->get('crontab'); {
$crontab->clearAll(); $crontab = Snowflake::app()->get('crontab');
$crontab->clearAll();
$process->name('Crontab zookeeper.'); if (Snowflake::getPlatform()->isLinux()) {
Timer::tick(1000, function () { $process->name('Crontab zookeeper.');
$startTime = time(); }
Timer::tick(1000, function () {
$startTime = time();
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime); $range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
$redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime); $redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
/** @var Consumer $consumer */ /** @var Consumer $consumer */
$consumer = Snowflake::app()->get(Consumer::class); $consumer = Snowflake::app()->get(Consumer::class);
foreach ($range as $value) { foreach ($range as $value) {
$consumer->write('crontab:' . md5($value)); $consumer->write('crontab:' . md5($value));
} }
$redis->release(); $redis->release();
}); });
} }
/** /**
* @param string $name * @param string $name
*/ */
public function clear(string $name) public function clear(string $name)
{ {
if (!isset($this->names[$name])) { if (!isset($this->names[$name])) {
return; return;
} }
$timers = $this->timers[$name]; $timers = $this->timers[$name];
$search = array_search($name, $this->scores[$timers]); $search = array_search($name, $this->scores[$timers]);
if ($search !== false) { if ($search !== false) {
unset($this->scores[$timers][$search]); unset($this->scores[$timers][$search]);
} }
unset($this->timers[$name], $this->names[$name]); unset($this->timers[$name], $this->names[$name]);
} }
} }