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

121 lines
2.1 KiB
PHP
Raw Normal View History

2021-03-26 01:01:21 +08:00
<?php
namespace Snowflake\Crontab;
use Exception;
2021-07-20 11:28:19 +08:00
use Server\SInterface\CustomProcess;
2021-04-20 18:38:41 +08:00
use Snowflake\Abstracts\Config;
2021-04-15 18:18:41 +08:00
use Snowflake\Cache\Redis;
2021-04-26 17:34:33 +08:00
use Snowflake\Exception\ConfigException;
2021-03-26 01:01:21 +08:00
use Snowflake\Snowflake;
2021-07-20 11:28:19 +08:00
use Swoole\Process;
2021-04-28 12:08:10 +08:00
use Swoole\Timer;
2021-04-16 01:17:37 +08:00
use Throwable;
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-07-20 11:28:19 +08:00
class Zookeeper implements CustomProcess
2021-03-26 01:01:21 +08:00
{
2021-04-23 03:55:51 +08:00
2021-04-28 12:08:10 +08:00
private int $workerNum = 0;
2021-04-23 03:55:51 +08:00
2021-04-28 12:08:10 +08:00
private mixed $server;
2021-04-23 03:55:51 +08:00
2021-04-26 17:34:33 +08:00
/**
2021-07-20 11:28:19 +08:00
* @param Process $process
2021-04-26 17:34:33 +08:00
* @return string
* @throws ConfigException
*/
2021-07-20 11:28:19 +08:00
public function getProcessName(Process $process): string
2021-04-28 12:08:10 +08:00
{
2021-07-20 11:28:19 +08:00
$name = Config::get('id', 'system') . '[' . $process->pid . ']';
2021-04-28 12:08:10 +08:00
if (!empty($prefix)) {
$name .= '.Crontab zookeeper';
}
return $name;
}
/**
2021-07-20 11:28:19 +08:00
* @param Process $process
2021-04-28 12:08:10 +08:00
* @throws Exception
*/
2021-07-20 11:28:19 +08:00
public function onHandler(Process $process): void
2021-04-28 12:08:10 +08:00
{
2021-04-28 12:08:57 +08:00
Timer::tick(100, [$this, 'loop']);
2021-04-28 12:08:10 +08:00
}
/**
* @throws ConfigException
* @throws Exception
*/
public function loop()
{
$redis = Snowflake::app()->getRedis();
defer(fn() => $redis->release());
$range = $this->loadCarobTask($redis);
foreach ($range as $value) {
$this->dispatch($redis, $value);
}
}
2021-04-23 03:55:51 +08:00
2021-04-23 12:03:24 +08:00
/**
* @param Redis|\Redis $redis
* @param $value
* @throws Exception
*/
2021-04-28 12:08:10 +08:00
private function dispatch(Redis|\Redis $redis, $value)
{
try {
$params['action'] = 'crontab';
if (empty($handler = $redis->get('crontab:' . $value))) {
return;
}
$params['handler'] = swoole_unserialize($handler);
$this->server->sendMessage($params, $this->getWorker());
} catch (Throwable $exception) {
logger()->addError($exception);
}
}
/**
* @return int
* @throws \Exception
*/
private function getWorker(): int
{
return random_int(0, $this->workerNum - 1);
}
2021-04-23 03:55:51 +08:00
2021-04-26 17:34:33 +08:00
/**
* @param Redis|\Redis $redis
* @return array
*/
2021-04-28 12:08:10 +08:00
private function loadCarobTask(Redis|\Redis $redis): array
{
2021-06-07 11:49:24 +08:00
$script = <<<SCRIPT
2021-06-07 12:03:37 +08:00
local _two = redis.call('zRangeByScore', KEYS[1], '0', ARGV[1])
2021-06-07 12:05:03 +08:00
if (table.getn(_two) > 0) then
redis.call('ZREM', KEYS[1], unpack(_two))
end
2021-06-07 12:03:37 +08:00
return _two
2021-06-07 11:49:24 +08:00
SCRIPT;
2021-06-07 12:05:39 +08:00
return $redis->eval($script, [Producer::CRONTAB_KEY, (string)time()], 1);
2021-04-28 12:08:10 +08:00
}
2021-04-16 00:33:13 +08:00
2021-03-26 01:01:21 +08:00
}