This commit is contained in:
as2252258@163.com
2021-04-16 00:33:13 +08:00
parent 5cb3296982
commit 331ba228a3
+73 -73
View File
@@ -20,99 +20,99 @@ class Zookeeper 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 Exception * @throws Exception
*/ */
public function onHandler(\Swoole\Process $process): void public function onHandler(\Swoole\Process $process): void
{ {
$crontab = Snowflake::app()->get('crontab'); $crontab = Snowflake::app()->get('crontab');
$crontab->clearAll(); $crontab->clearAll();
if (Snowflake::getPlatform()->isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
name($this->pid, 'Crontab zookeeper.'); name($this->pid, 'Crontab zookeeper.');
} }
Timer::tick(1000, function () { Timer::tick(1000, function () {
[$range, $redis] = $this->loadCarobTask(); [$range, $redis] = $this->loadCarobTask();
$server = Snowflake::app()->getSwoole(); $server = Snowflake::app()->getSwoole();
$setting = $server->setting['worker_num']; $setting = $server->setting['worker_num'];
foreach ($range as $value) { foreach ($range as $value) {
$this->dispatch($server, $redis, $setting, $value); $this->dispatch($server, $redis, $setting, $value);
} }
$redis->release(); $redis->release();
}); });
} }
/** /**
* @param $server * @param $server
* @param Redis|\Redis $redis * @param Redis|\Redis $redis
* @param int $setting * @param int $setting
* @param $value * @param $value
* @throws Exception * @throws Exception
*/ */
private function dispatch($server, Redis|\Redis $redis, int $setting, $value) private function dispatch($server, Redis|\Redis $redis, int $setting, $value)
{ {
try { try {
$params['action'] = 'crontab'; $params['action'] = 'crontab';
$params['handler'] = swoole_unserialize($redis->get('crontab:' . $value)); $params['handler'] = swoole_unserialize($redis->get('crontab:' . $value));
$result = $server->sendMessage($params, $workerId = random_int(0, $setting - 1)); $result = $server->sendMessage($params, $workerId = random_int(0, $setting - 1));
var_dump('send crontab to ' . $workerId . ' ' . intval($result)); var_dump('send crontab to ' . $workerId . ' ' . intval($result));
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
logger()->addError($exception); logger()->addError($exception);
} }
} }
/** /**
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
private function loadCarobTask(): array private function loadCarobTask(): array
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$startTime = time(); $startTime = time();
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime); $range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
var_dump($range); var_dump($startTime, $range);
$redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime); $redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
return [$range, $redis]; return [$range, $redis];
} }
/** /**
* @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]);
} }
} }