2023-01-04 12:18:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands\Cluster;
|
|
|
|
|
2023-01-04 15:57:36 +00:00
|
|
|
use App\Support\Cluster;
|
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-04 15:57:36 +00:00
|
|
|
Cluster::listen('*', function ($event, $message) {
|
|
|
|
$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 = [])
|
|
|
|
{
|
|
|
|
$status = $this->switch($event);
|
|
|
|
|
|
|
|
if (!$status) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = "[{$message['node']['type']}] {$message['node']['id']}:{$event}: " . $status;
|
|
|
|
|
|
|
|
$this->info($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function switch($event): string|null
|
|
|
|
{
|
|
|
|
$events = [
|
|
|
|
'node.ok' => '此节点初始化成功,并且已经加入集群。',
|
|
|
|
'node.online' => '此节点已经上线。',
|
|
|
|
'node.offline' => '将不再处理任何任务。',
|
|
|
|
'cluster_ready.ok' => 'Cluster Ready 就绪了,已经可以处理请求了。',
|
|
|
|
'config.updated' => '集群配置文件已经更新,请所有 slave 节点下载。',
|
|
|
|
'config.synced' => '我已下载配置文件。',
|
2023-01-04 18:25:14 +00:00
|
|
|
'edge.deployed' => '边缘节点已经部署,已经成功根据集群节点生成配置文件并应用。',
|
2023-01-04 15:57:36 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return $events[$event] ?? null;
|
|
|
|
}
|
2023-01-04 12:18:22 +00:00
|
|
|
}
|