Files
kiri-core/System/Crontab/Zookeeper.php
T

111 lines
2.5 KiB
PHP
Raw Normal View History

2021-03-26 01:01:21 +08:00
<?php
namespace Snowflake\Crontab;
use Exception;
use Snowflake\Process\Process;
use Snowflake\Snowflake;
use Swoole\Coroutine\WaitGroup;
use Swoole\Coroutine\Channel;
use Swoole\Timer;
2021-04-12 11:05:57 +08:00
use Snowflake\Cache\Redis;
2021-03-26 01:01:21 +08:00
/**
2021-04-12 02:55:12 +08:00
* Class Zookeeper
2021-03-26 01:01:21 +08:00
* @package Snowflake\Process
*/
2021-04-12 02:55:12 +08:00
class Zookeeper extends Process
2021-03-26 01:01:21 +08:00
{
2021-04-11 15:38:23 +08:00
private Channel $channel;
private WaitGroup $waitGroup;
2021-03-29 10:40:47 +08:00
2021-04-11 15:38:23 +08:00
/** @var Crontab[] $names */
public array $names = [];
2021-03-29 10:40:47 +08:00
2021-04-11 15:38:23 +08:00
public array $scores = [];
public array $timers = [];
2021-03-29 10:40:47 +08:00
2021-04-11 15:38:23 +08:00
/**
* @param \Swoole\Process $process
* @throws Exception
*/
public function onHandler(\Swoole\Process $process): void
{
$crontab = Snowflake::app()->get('crontab');
$crontab->clearAll();
if (Snowflake::getPlatform()->isLinux()) {
name($this->pid, 'Crontab zookeeper.');
}
Timer::tick(1000, function () {
2021-04-12 11:05:57 +08:00
[$range, $redis] = $this->loadCarobTask();
2021-04-12 03:07:38 +08:00
2021-04-12 02:55:12 +08:00
$server = Snowflake::app()->getSwoole();
$setting = $server->setting['worker_num'];
foreach ($range as $value) {
$this->dispatch($server, $redis, $setting, $value);
}
$redis->release();
});
}
2021-04-12 11:05:57 +08:00
/**
* @param $server
* @param Redis|\Redis $redis
* @param int $setting
* @param $value
* @throws Exception
*/
private function dispatch($server, Redis|\Redis $redis, int $setting, $value)
2021-04-12 02:55:12 +08:00
{
$server->sendMessage(swoole_serialize([
2021-04-12 03:04:42 +08:00
'action' => 'crontab', 'handler' => swoole_unserialize($redis->get('crontab:' . $value))
2021-04-12 02:55:12 +08:00
]), random_int(0, $setting - 1));
2021-04-12 03:04:42 +08:00
$redis->del('crontab:' . $value);
2021-04-12 02:55:12 +08:00
}
2021-03-29 10:40:47 +08:00
2021-04-12 02:55:12 +08:00
/**
* @return array
2021-04-12 11:05:57 +08:00
* @throws Exception
2021-04-12 02:55:12 +08:00
*/
2021-04-12 11:05:57 +08:00
private function loadCarobTask(): array
2021-04-12 02:55:12 +08:00
{
$redis = Snowflake::app()->getRedis();
2021-04-11 04:07:35 +08:00
2021-04-12 02:55:12 +08:00
$startTime = time();
2021-03-29 10:40:47 +08:00
2021-04-12 03:09:00 +08:00
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
2021-04-12 02:55:12 +08:00
$redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
2021-04-11 15:38:23 +08:00
2021-04-12 02:55:12 +08:00
return [$range, $redis];
2021-04-11 15:38:23 +08:00
}
/**
* @param string $name
*/
public function clear(string $name)
{
if (!isset($this->names[$name])) {
return;
}
$timers = $this->timers[$name];
$search = array_search($name, $this->scores[$timers]);
if ($search !== false) {
unset($this->scores[$timers][$search]);
}
unset($this->timers[$name], $this->names[$name]);
}
2021-03-26 01:01:21 +08:00
}