改名
This commit is contained in:
@@ -9,7 +9,6 @@ use HttpServer\Abstracts\Callback;
|
|||||||
use Kafka\Struct;
|
use Kafka\Struct;
|
||||||
use Snowflake\Crontab\Crontab;
|
use Snowflake\Crontab\Crontab;
|
||||||
use Snowflake\Event;
|
use Snowflake\Event;
|
||||||
use Snowflake\Exception\ComponentException;
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,20 +24,20 @@ class OnPipeMessage extends Callback
|
|||||||
* @param $swollen_universalize
|
* @param $swollen_universalize
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onHandler(Server $server, int $src_worker_id, $swollen_universalize)
|
public function onHandler(Server $server, int $src_worker_id, $swollen_universalize)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
match ($swollen_universalize['action'] ?? null) {
|
match ($swollen_universalize['action'] ?? null) {
|
||||||
'kafka' => $this->onKafkaWorker($swollen_universalize),
|
'kafka' => $this->onKafkaWorker($swollen_universalize),
|
||||||
'crontab' => $this->onCrontabWorker($swollen_universalize),
|
'crontab' => $this->onCrontabWorker($swollen_universalize),
|
||||||
default => $this->onMessageWorker($server, $src_worker_id, $swollen_universalize)
|
default => $this->onMessageWorker($server, $src_worker_id, $swollen_universalize)
|
||||||
};
|
};
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$this->addError($exception);
|
$this->addError($exception);
|
||||||
} finally {
|
} finally {
|
||||||
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -46,13 +45,17 @@ class OnPipeMessage extends Callback
|
|||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function onCrontabWorker(array $message): string
|
private function onCrontabWorker(array $message): string
|
||||||
{
|
{
|
||||||
/** @var Crontab $crontab */
|
/** @var Crontab $crontab */
|
||||||
$crontab = $message['handler'] ?? null;
|
$crontab = $message['handler'] ?? null;
|
||||||
$crontab->increment()->execute();
|
if (!isset($message['handler'])) {
|
||||||
return 'success';
|
throw new Exception('unknown handler');
|
||||||
}
|
}
|
||||||
|
$crontab = swoole_unserialize($crontab['handler']);
|
||||||
|
$crontab->increment()->execute();
|
||||||
|
return 'success';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -62,26 +65,26 @@ class OnPipeMessage extends Callback
|
|||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function onMessageWorker($server, $src_worker_id, $message): string
|
private function onMessageWorker($server, $src_worker_id, $message): string
|
||||||
{
|
{
|
||||||
fire(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]);
|
fire(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]);
|
||||||
|
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $message
|
* @param array $message
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function onKafkaWorker(array $message): string
|
private function onKafkaWorker(array $message): string
|
||||||
{
|
{
|
||||||
[$topic, $rdMessage] = $message['body'];
|
[$topic, $rdMessage] = $message['body'];
|
||||||
|
|
||||||
call_user_func($message['handler'], new Struct($topic, $rdMessage));
|
call_user_func($message['handler'], new Struct($topic, $rdMessage));
|
||||||
|
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ namespace Snowflake\Crontab;
|
|||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Snowflake\Cache\Redis;
|
||||||
use Snowflake\Process\Process;
|
use Snowflake\Process\Process;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
use Swoole\Coroutine\WaitGroup;
|
|
||||||
use Swoole\Coroutine\Channel;
|
use Swoole\Coroutine\Channel;
|
||||||
|
use Swoole\Coroutine\WaitGroup;
|
||||||
use Swoole\Timer;
|
use Swoole\Timer;
|
||||||
use Snowflake\Cache\Redis;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Zookeeper
|
* Class Zookeeper
|
||||||
@@ -20,40 +20,40 @@ class Zookeeper extends Process
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private Channel $channel;
|
private Channel $channel;
|
||||||
private WaitGroup $waitGroup;
|
private WaitGroup $waitGroup;
|
||||||
|
|
||||||
|
|
||||||
/** @var Crontab[] $names */
|
/** @var Crontab[] $names */
|
||||||
public array $names = [];
|
public array $names = [];
|
||||||
|
|
||||||
|
|
||||||
public array $scores = [];
|
public array $scores = [];
|
||||||
public array $timers = [];
|
public array $timers = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Process $process
|
* @param \Swoole\Process $process
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onHandler(\Swoole\Process $process): void
|
public function onHandler(\Swoole\Process $process): void
|
||||||
{
|
{
|
||||||
$crontab = Snowflake::app()->get('crontab');
|
$crontab = Snowflake::app()->get('crontab');
|
||||||
$crontab->clearAll();
|
$crontab->clearAll();
|
||||||
if (Snowflake::getPlatform()->isLinux()) {
|
if (Snowflake::getPlatform()->isLinux()) {
|
||||||
name($this->pid, 'Crontab zookeeper.');
|
name($this->pid, 'Crontab zookeeper.');
|
||||||
}
|
}
|
||||||
Timer::tick(1000, function () {
|
Timer::tick(1000, function () {
|
||||||
[$range, $redis] = $this->loadCarobTask();
|
[$range, $redis] = $this->loadCarobTask();
|
||||||
|
|
||||||
$server = Snowflake::app()->getSwoole();
|
$server = Snowflake::app()->getSwoole();
|
||||||
$setting = $server->setting['worker_num'];
|
$setting = $server->setting['worker_num'];
|
||||||
foreach ($range as $value) {
|
foreach ($range as $value) {
|
||||||
$this->dispatch($server, $redis, $setting, $value);
|
$this->dispatch($server, $redis, $setting, $value);
|
||||||
}
|
}
|
||||||
$redis->release();
|
$redis->release();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,47 +63,52 @@ class Zookeeper extends Process
|
|||||||
* @param $value
|
* @param $value
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function dispatch($server, Redis|\Redis $redis, int $setting, $value)
|
private function dispatch($server, Redis|\Redis $redis, int $setting, $value)
|
||||||
{
|
{
|
||||||
$server->sendMessage([
|
try {
|
||||||
'action' => 'crontab', 'handler' => swoole_unserialize($redis->get('crontab:' . $value))
|
$params['action'] = 'crontab';
|
||||||
], random_int(0, $setting - 1));
|
$params['handler'] = $redis->get('crontab:' . $value);
|
||||||
}
|
$server->sendMessage($params, random_int(0, $setting - 1));
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
logger()->addError($exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function loadCarobTask(): array
|
private function loadCarobTask(): array
|
||||||
{
|
{
|
||||||
$redis = Snowflake::app()->getRedis();
|
$redis = Snowflake::app()->getRedis();
|
||||||
|
|
||||||
$startTime = time();
|
$startTime = time();
|
||||||
|
|
||||||
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
|
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
|
||||||
$redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
|
$redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
|
||||||
|
|
||||||
return [$range, $redis];
|
return [$range, $redis];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*/
|
*/
|
||||||
public function clear(string $name)
|
public function clear(string $name)
|
||||||
{
|
{
|
||||||
if (!isset($this->names[$name])) {
|
if (!isset($this->names[$name])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$timers = $this->timers[$name];
|
$timers = $this->timers[$name];
|
||||||
|
|
||||||
$search = array_search($name, $this->scores[$timers]);
|
$search = array_search($name, $this->scores[$timers]);
|
||||||
if ($search !== false) {
|
if ($search !== false) {
|
||||||
unset($this->scores[$timers][$search]);
|
unset($this->scores[$timers][$search]);
|
||||||
}
|
}
|
||||||
unset($this->timers[$name], $this->names[$name]);
|
unset($this->timers[$name], $this->names[$name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user