更改 命令

This commit is contained in:
iVampireSP.com 2023-02-09 18:56:26 +08:00
parent c6a10b962f
commit 51b7451de2
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132

View File

@ -6,14 +6,14 @@
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias; use Symfony\Component\Console\Command\Command as CommandAlias;
class Log extends Command class Monitor extends Command
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'cluster:log'; protected $signature = 'monitor {--ignore_event=}';
/** /**
* The console command description. * The console command description.
@ -29,7 +29,19 @@ class Log extends Command
*/ */
public function handle(): int public function handle(): int
{ {
ClusterSupport::listen('*', function ($event, $message) { $ignore_events = [];
if ($this->option('ignore_event')) {
$ignore_events = explode(',', $this->option('ignore_event'));
}
ClusterSupport::publish('monitor.started');
ClusterSupport::listen('*', function ($event, $message) use ($ignore_events) {
if (in_array($event, $ignore_events)) {
return;
}
$this->format($event, $message); $this->format($event, $message);
}, false); }, false);
@ -52,8 +64,9 @@ private function format(string $event, array $message = [])
public function switch($event, $message = []): string|null public function switch($event, $message = []): string|null
{ {
$events = [ $events = [
'monitor.started' => '我正在监听集群消息。',
'node.ok' => '此节点初始化成功,并且已经加入集群。', 'node.ok' => '此节点初始化成功,并且已经加入集群。',
'node.online' => '此节点已经上线。', 'node.online' => fn($message) => '此节点已经上线,启动权重为:' . $message['weight'],
'node.offline' => '将不再处理任何任务。', 'node.offline' => '将不再处理任何任务。',
'cluster_ready.ok' => 'ClusterSupport Ready 就绪了,已经可以处理请求了。', 'cluster_ready.ok' => 'ClusterSupport Ready 就绪了,已经可以处理请求了。',
'config.updated' => '集群配置文件已经更新,请所有 slave 节点下载。', 'config.updated' => '集群配置文件已经更新,请所有 slave 节点下载。',
@ -71,6 +84,7 @@ public function switch($event, $message = []): string|null
'cluster.deployed.ok' => '集群配置文件部署成功。', 'cluster.deployed.ok' => '集群配置文件部署成功。',
'http.incoming' => fn($message) => $this->handleIncomingRequest($message), 'http.incoming' => fn($message) => $this->handleIncomingRequest($message),
'http.outgoing' => fn($message) => $this->handleOutgoingRequest($message), 'http.outgoing' => fn($message) => $this->handleOutgoingRequest($message),
'weight.updated' => fn($message) => $this->handleWeightUpdated($message),
]; ];
$resp = $events[$event] ?? null; $resp = $events[$event] ?? null;
@ -101,6 +115,18 @@ private function handleOutgoingRequest(array $message): string
return $msg; return $msg;
} }
private function handleWeightUpdated(array $message): string
{
$msg = '';
if ($message['weight'] === "0") {
$msg .= '我不参与调度了。';
} else {
$msg .= '我的权重是:' . $message['weight'] . '。';
}
return $msg;
}
private function appendUser(array $message): string private function appendUser(array $message): string
{ {
$msg = ''; $msg = '';