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

129 lines
2.3 KiB
PHP
Raw Normal View History

2021-03-26 01:01:21 +08:00
<?php
namespace Snowflake\Crontab;
use Exception;
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\Process\Process;
use Snowflake\Snowflake;
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-04-12 02:55:12 +08:00
class Zookeeper extends Process
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
/**
* @return string
* @throws ConfigException
*/
2021-04-28 12:08:10 +08:00
public function getProcessName(): string
{
$name = Config::get('id', 'system') . '[' . $this->pid . ']';
if (!empty($prefix)) {
$name .= '.Crontab zookeeper';
}
return $name;
}
/**
* @param \Swoole\Process $process
* @throws Exception
*/
public function before(\Swoole\Process $process): void
{
/** @var Producer $crontab */
$crontab = Snowflake::app()->get('crontab');
$crontab->clearAll();
$this->server = $server = Snowflake::app()->getSwoole();
$this->workerNum = $server->setting['worker_num'] + $server->setting['task_worker_num'];
}
/**
* @param \Swoole\Process $process
* @throws Exception
*/
public function onHandler(\Swoole\Process $process): void
{
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
{
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)time());
2021-04-23 03:55:51 +08:00
2021-04-28 12:08:10 +08:00
$redis->zRem(Producer::CRONTAB_KEY, ...$range);
2021-04-23 03:55:51 +08:00
2021-04-28 12:08:10 +08:00
return $range;
}
2021-04-16 00:33:13 +08:00
2021-03-26 01:01:21 +08:00
}