This commit is contained in:
as2252258@163.com
2021-04-23 03:55:51 +08:00
parent 93ca2318bd
commit 60b30d2557
3 changed files with 108 additions and 66 deletions
+91 -59
View File
@@ -5,6 +5,7 @@ namespace Snowflake\Crontab;
use Exception; use Exception;
use HttpServer\Server;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Cache\Redis; use Snowflake\Cache\Redis;
use Snowflake\Process\Process; use Snowflake\Process\Process;
@@ -19,72 +20,103 @@ use Throwable;
class Zookeeper extends Process class Zookeeper extends Process
{ {
/**
* @param \Swoole\Process $process
* @throws Exception
*/
public function onHandler(\Swoole\Process $process): void
{
/** @var Producer $crontab */
$crontab = Snowflake::app()->get('crontab');
$crontab->clearAll();
if (Snowflake::getPlatform()->isLinux()) {
name($this->pid, 'Crontab zookeeper.');
}
$ticker = Config::get('crontab.ticker', 50) / 1000; private int $workerNum = 0;
$server = Snowflake::app()->getSwoole();
$setting = $server->setting['worker_num'] + $server->setting['task_worker_num'];
while (true) {
[$range, $redis] = $this->loadCarobTask();
foreach ($range as $value) {
$this->dispatch($server, $redis, $setting, $value);
}
$redis->release();
Coroutine::sleep($ticker);
}
}
/** private mixed $server;
* @param $server
* @param Redis|\Redis $redis
* @param int $setting
* @param $value
* @throws Exception
*/
private function dispatch($server, Redis|\Redis $redis, int $setting, $value)
{
try {
$params['action'] = 'crontab';
if (empty($handler = $redis->get('crontab:' . $value))) {
return;
}
$params['handler'] = swoole_unserialize($handler);
$server->sendMessage($params, random_int(0, $setting - 1));
} catch (Throwable $exception) {
logger()->addError($exception);
}
}
/** /**
* @return array * @param \Swoole\Process $process
* @throws Exception */
*/ public function getProcessName(): string
private function loadCarobTask(): array {
{ $name = Config::get('id', 'system') . '[' . $this->pid . ']';
$redis = Snowflake::app()->getRedis(); if (!empty($prefix)) {
$name .= '.Crontab zookeeper';
}
return $name;
}
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)time());
$redis->zRem(Producer::CRONTAB_KEY, ...$range); /**
* @param \Swoole\Process $process
* @throws \Snowflake\Exception\ConfigException
*/
public function before(\Swoole\Process $process): void
{
/** @var Producer $crontab */
$crontab = Snowflake::app()->get('crontab');
$crontab->clearAll();
return [$range, $redis]; $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
{
$ticker = Config::get('crontab.ticker', 50) / 1000;
$redis = Snowflake::app()->getRedis();
while (true) {
$range = $this->loadCarobTask($redis);
foreach ($range as $value) {
$this->dispatch($redis, $value);
}
Coroutine::sleep($ticker);
}
}
/**
* @param $server
* @param Redis|\Redis $redis
* @param int $setting
* @param $value
* @throws Exception
*/
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);
}
/**
* @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);
return [$range, $redis];
}
} }
+8 -3
View File
@@ -7,11 +7,8 @@ namespace Snowflake\Process;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Snowflake\Application;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine\System;
/** /**
* Class Process * Class Process
@@ -46,6 +43,14 @@ abstract class Process extends \Swoole\Process implements SProcess
if (Snowflake::getPlatform()->isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
name($process->pid, $this->getPrefix()); name($process->pid, $this->getPrefix());
} }
if (method_exists($this, 'before')) {
$this->before($process);
}
if (!Snowflake::getPlatform()->isMac()) {
if (method_exists($this, 'getProcessName')) {
swoole_set_process_name($this->getProcessName());
}
}
$this->onHandler($process); $this->onHandler($process);
} }
+9 -4
View File
@@ -8,9 +8,14 @@ interface SProcess
{ {
/** // public function getProcessName(): string;
* @param \Swoole\Process $process //
*/ //
public function onHandler(\Swoole\Process $process): void; // public function before(\Swoole\Process $process): void;
/**
* @param \Swoole\Process $process
*/
public function onHandler(\Swoole\Process $process): void;
} }