2023-01-04 12:18:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands\Cluster;
|
|
|
|
|
2023-01-14 17:21:12 +00:00
|
|
|
use App\Support\ClusterSupport;
|
2023-01-04 12:18:22 +00:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Symfony\Component\Console\Command\Command as CommandAlias;
|
|
|
|
|
|
|
|
class Log extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'cluster:log';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = '监听集群消息。';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
2023-01-14 17:21:12 +00:00
|
|
|
ClusterSupport::listen('*', function ($event, $message) {
|
2023-01-04 15:57:36 +00:00
|
|
|
$this->format($event, $message);
|
|
|
|
}, false);
|
|
|
|
|
2023-01-04 12:18:22 +00:00
|
|
|
return CommandAlias::SUCCESS;
|
|
|
|
}
|
2023-01-04 15:57:36 +00:00
|
|
|
|
|
|
|
private function format(string $event, array $message = [])
|
|
|
|
{
|
2023-01-04 19:28:18 +00:00
|
|
|
$status = $this->switch($event, $message['data']);
|
2023-01-04 15:57:36 +00:00
|
|
|
|
2023-02-08 13:35:11 +00:00
|
|
|
if (! $status) {
|
2023-01-04 15:57:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-08 13:35:11 +00:00
|
|
|
$message = "[{$message['node']['type']}] {$message['node']['id']}:$event: ".$status;
|
2023-01-04 15:57:36 +00:00
|
|
|
|
|
|
|
$this->info($message);
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:28:18 +00:00
|
|
|
public function switch($event, $message = []): string|null
|
2023-01-04 15:57:36 +00:00
|
|
|
{
|
|
|
|
$events = [
|
|
|
|
'node.ok' => '此节点初始化成功,并且已经加入集群。',
|
|
|
|
'node.online' => '此节点已经上线。',
|
|
|
|
'node.offline' => '将不再处理任何任务。',
|
2023-01-14 17:21:12 +00:00
|
|
|
'cluster_ready.ok' => 'ClusterSupport Ready 就绪了,已经可以处理请求了。',
|
2023-01-04 15:57:36 +00:00
|
|
|
'config.updated' => '集群配置文件已经更新,请所有 slave 节点下载。',
|
2023-01-18 10:12:28 +00:00
|
|
|
'config.ssl.updated' => '边缘节点的证书已经更新。',
|
2023-01-04 15:57:36 +00:00
|
|
|
'config.synced' => '我已下载配置文件。',
|
2023-01-04 19:28:18 +00:00
|
|
|
'edge.deployed' => '已成功根据集群节点生成配置文件并应用。',
|
|
|
|
'edge.launched' => '边缘节点成功启动。',
|
2023-02-08 13:35:11 +00:00
|
|
|
'edge.error' => fn ($message) => $message['message'] ?? '未知错误',
|
2023-01-04 19:43:59 +00:00
|
|
|
'cluster.restart.web' => '正在重启 web 服务。',
|
|
|
|
'cluster.restart.all' => '正在重启 整个 服务。',
|
|
|
|
'cluster.restarted.web' => 'Web 重启好了。',
|
|
|
|
'cluster.restarted.all' => '整个 重启好了。',
|
2023-02-08 05:46:21 +00:00
|
|
|
'cluster.deployed' => '集群配置文件已经部署。',
|
2023-02-08 13:35:11 +00:00
|
|
|
'cluster.deployed.error' => fn ($message) => $message['message'] ?? '未知错误',
|
2023-02-08 05:46:21 +00:00
|
|
|
'cluster.deployed.ok' => '集群配置文件部署成功。',
|
2023-02-08 13:35:11 +00:00
|
|
|
'http.incoming' => fn ($message) => $this->handleIncomingRequest($message),
|
|
|
|
'http.outgoing' => fn ($message) => $this->handleOutgoingRequest($message),
|
2023-01-04 15:57:36 +00:00
|
|
|
];
|
|
|
|
|
2023-02-08 06:18:58 +00:00
|
|
|
$resp = $events[$event] ?? null;
|
2023-02-08 05:46:21 +00:00
|
|
|
|
|
|
|
// if resp is callable
|
|
|
|
if (is_callable($resp)) {
|
|
|
|
return $resp($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resp ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleIncomingRequest(array $message): string
|
|
|
|
{
|
|
|
|
$msg = $this->appendUser($message);
|
|
|
|
|
|
|
|
$msg .= "{$message['method']} {$message['path']}";
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleOutgoingRequest(array $message): string
|
|
|
|
{
|
|
|
|
$msg = $this->appendUser($message);
|
|
|
|
|
|
|
|
$msg .= "{$message['method']} {$message['path']} {$message['status']} {$message['time']}ms";
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function appendUser(array $message): string
|
|
|
|
{
|
|
|
|
$msg = '';
|
|
|
|
if ($message['user']) {
|
|
|
|
$msg .= "{$message['user']['name']}#{$message['user']['id']} ";
|
|
|
|
} else {
|
|
|
|
$msg .= 'Guest ';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $msg;
|
2023-01-04 15:57:36 +00:00
|
|
|
}
|
2023-01-04 12:18:22 +00:00
|
|
|
}
|