modify
This commit is contained in:
@@ -5,6 +5,7 @@ namespace Snowflake\Crontab;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Server;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Cache\Redis;
|
||||
use Snowflake\Process\Process;
|
||||
@@ -19,30 +20,54 @@ use Throwable;
|
||||
class Zookeeper extends Process
|
||||
{
|
||||
|
||||
|
||||
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
|
||||
* @throws \Snowflake\Exception\ConfigException
|
||||
*/
|
||||
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
|
||||
{
|
||||
/** @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;
|
||||
|
||||
$server = Snowflake::app()->getSwoole();
|
||||
$setting = $server->setting['worker_num'] + $server->setting['task_worker_num'];
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
while (true) {
|
||||
[$range, $redis] = $this->loadCarobTask();
|
||||
$range = $this->loadCarobTask($redis);
|
||||
foreach ($range as $value) {
|
||||
$this->dispatch($server, $redis, $setting, $value);
|
||||
$this->dispatch($redis, $value);
|
||||
}
|
||||
$redis->release();
|
||||
|
||||
Coroutine::sleep($ticker);
|
||||
}
|
||||
}
|
||||
@@ -55,7 +80,7 @@ class Zookeeper extends Process
|
||||
* @param $value
|
||||
* @throws Exception
|
||||
*/
|
||||
private function dispatch($server, Redis|\Redis $redis, int $setting, $value)
|
||||
private function dispatch(Redis|\Redis $redis, $value)
|
||||
{
|
||||
try {
|
||||
$params['action'] = 'crontab';
|
||||
@@ -64,11 +89,20 @@ class Zookeeper extends Process
|
||||
}
|
||||
$params['handler'] = swoole_unserialize($handler);
|
||||
|
||||
$server->sendMessage($params, random_int(0, $setting - 1));
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
@@ -76,10 +110,8 @@ class Zookeeper extends Process
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
private function loadCarobTask(): array
|
||||
private function loadCarobTask(Redis|\Redis $redis): array
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)time());
|
||||
|
||||
$redis->zRem(Producer::CRONTAB_KEY, ...$range);
|
||||
|
||||
@@ -7,11 +7,8 @@ namespace Snowflake\Process;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Application;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine\System;
|
||||
|
||||
/**
|
||||
* Class Process
|
||||
@@ -46,6 +43,14 @@ abstract class Process extends \Swoole\Process implements SProcess
|
||||
if (Snowflake::getPlatform()->isLinux()) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,11 @@ interface SProcess
|
||||
{
|
||||
|
||||
|
||||
// public function getProcessName(): string;
|
||||
//
|
||||
//
|
||||
// public function before(\Swoole\Process $process): void;
|
||||
|
||||
/**
|
||||
* @param \Swoole\Process $process
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user