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

121 lines
2.6 KiB
PHP
Raw Normal View History

2021-03-26 01:01:21 +08:00
<?php
namespace Snowflake\Crontab;
use Exception;
2021-04-23 03:55:51 +08:00
use HttpServer\Server;
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-03-26 01:01:21 +08:00
use Snowflake\Process\Process;
use Snowflake\Snowflake;
2021-04-16 01:42:01 +08:00
use Swoole\Coroutine;
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
private int $workerNum = 0;
private mixed $server;
/**
* @param \Swoole\Process $process
*/
public function getProcessName(): string
{
$name = Config::get('id', 'system') . '[' . $this->pid . ']';
if (!empty($prefix)) {
$name .= '.Crontab zookeeper';
}
return $name;
}
/**
* @param \Swoole\Process $process
2021-04-23 12:03:24 +08:00
* @throws Exception
2021-04-23 03:55:51 +08:00
*/
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-23 12:25:21 +08:00
$ticker = Config::get('crontab.ticker', 100) / 1000;
2021-04-23 03:55:51 +08:00
$redis = Snowflake::app()->getRedis();
while (true) {
$range = $this->loadCarobTask($redis);
foreach ($range as $value) {
$this->dispatch($redis, $value);
}
2021-04-23 12:19:10 +08:00
Coroutine::sleep($ticker);
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-23 03:55:51 +08:00
private function dispatch(Redis|\Redis $redis, $value)
{
try {
$params['action'] = 'crontab';
if (empty($handler = $redis->get('crontab:' . $value))) {
return;
}
2021-04-24 20:06:21 +08:00
$params['handler'] = unserialize($handler);
2021-04-23 03:55:51 +08:00
$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);
}
/**
* @return array
* @throws Exception
*/
private function loadCarobTask(Redis|\Redis $redis): array
{
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)time());
$redis->zRem(Producer::CRONTAB_KEY, ...$range);
2021-04-23 03:56:50 +08:00
return $range;
2021-04-23 03:55:51 +08:00
}
2021-04-16 00:33:13 +08:00
2021-03-26 01:01:21 +08:00
}