diff --git a/app/Console/Commands/Cluster/Monitor.php b/app/Console/Commands/Cluster/Monitor.php index d1fcb98..25a14ce 100644 --- a/app/Console/Commands/Cluster/Monitor.php +++ b/app/Console/Commands/Cluster/Monitor.php @@ -52,11 +52,11 @@ private function format(string $event, array $message = []) { $status = $this->switch($event, $message['data']); - if (!$status) { + if (! $status) { return; } - $message = "[{$message['node']['type']}] {$message['node']['id']}:$event: " . $status; + $message = "[{$message['node']['type']}] {$message['node']['id']}:$event: ".$status; $this->info($message); } @@ -66,7 +66,7 @@ public function switch($event, $message = []): string|null $events = [ 'monitor.started' => '我正在监听集群消息。', 'node.ok' => '此节点初始化成功,并且已经加入集群。', - 'node.online' => fn($message) => '此节点已经上线,启动权重为:' . $message['weight'], + 'node.online' => fn ($message) => '此节点已经上线,启动权重为:'.$message['weight'], 'node.offline' => '将不再处理任何任务。', 'cluster_ready.ok' => 'ClusterSupport Ready 就绪了,已经可以处理请求了。', 'config.updated' => '集群配置文件已经更新,请所有 slave 节点下载。', @@ -74,17 +74,17 @@ public function switch($event, $message = []): string|null 'config.synced' => '我已下载配置文件。', 'edge.deployed' => '已成功根据集群节点生成配置文件并应用。', 'edge.launched' => '边缘节点成功启动。', - 'edge.error' => fn($message) => $message['message'] ?? '未知错误', + 'edge.error' => fn ($message) => $message['message'] ?? '未知错误', 'cluster.restart.web' => '正在重启 web 服务。', 'cluster.restart.all' => '正在重启 整个 服务。', 'cluster.restarted.web' => 'Web 重启好了。', 'cluster.restarted.all' => '整个 重启好了。', 'cluster.deployed' => '集群配置文件已经部署。', - 'cluster.deployed.error' => fn($message) => $message['message'] ?? '未知错误', + 'cluster.deployed.error' => fn ($message) => $message['message'] ?? '未知错误', 'cluster.deployed.ok' => '集群配置文件部署成功。', - 'http.incoming' => fn($message) => $this->handleIncomingRequest($message), - 'http.outgoing' => fn($message) => $this->handleOutgoingRequest($message), - 'weight.updated' => fn($message) => $this->handleWeightUpdated($message), + 'http.incoming' => fn ($message) => $this->handleIncomingRequest($message), + 'http.outgoing' => fn ($message) => $this->handleOutgoingRequest($message), + 'weight.updated' => fn ($message) => $this->handleWeightUpdated($message), ]; $resp = $events[$event] ?? null; @@ -118,10 +118,10 @@ private function handleOutgoingRequest(array $message): string private function handleWeightUpdated(array $message): string { $msg = ''; - if ($message['weight'] === "0") { + if ($message['weight'] === '0') { $msg .= '我不参与调度了。'; } else { - $msg .= '我的权重是:' . $message['weight'] . '。'; + $msg .= '我的权重是:'.$message['weight'].'。'; } return $msg; diff --git a/app/Console/Commands/Cluster/Work.php b/app/Console/Commands/Cluster/Work.php index 2dab2d5..dd8d789 100644 --- a/app/Console/Commands/Cluster/Work.php +++ b/app/Console/Commands/Cluster/Work.php @@ -24,7 +24,7 @@ class Work extends Command */ protected $description = '启动此应用程序。'; - private string $weight = "1"; + private string $weight = '1'; /** * Execute the console command. @@ -34,7 +34,7 @@ class Work extends Command public function handle(): int { // 检测目录下是否有 rr - if (!file_exists(base_path('rr'))) { + if (! file_exists(base_path('rr'))) { $this->warn('未找到 rr 文件,将自动下载。'); // 获取操作系统是 darwin 还是 linux @@ -79,7 +79,7 @@ public function handle(): int $node_ip = config('settings.node.ip'); - if (!$node_ip) { + if (! $node_ip) { $this->error('请先配置节点 IP。'); return CommandAlias::FAILURE; @@ -111,17 +111,17 @@ public function handle(): int $this->error('无法创建子进程。'); return CommandAlias::FAILURE; - } else if ($pid === 0) { + } elseif ($pid === 0) { // 再打开一个,负责 octane $pid = pcntl_fork(); if ($pid === -1) { $this->error('无法创建子进程。'); return CommandAlias::FAILURE; - } else if ($pid === 0) { + } elseif ($pid === 0) { // 子进程 $this->info('正在启动 Web。'); - $this->info('以权重 ' . $this->argument('weight') . ' 启动。'); + $this->info('以权重 '.$this->argument('weight').' 启动。'); $rpc_port = config('settings.node.rpc_port'); diff --git a/app/Support/ClusterSupport.php b/app/Support/ClusterSupport.php index a8997cc..041a8df 100644 --- a/app/Support/ClusterSupport.php +++ b/app/Support/ClusterSupport.php @@ -45,7 +45,7 @@ public static function publish($event, $data = [], $register = false): void public static function registerThisNode($report = true, $data = []): void { $node_id = config('settings.node.id'); - + $node = [ 'type' => config('settings.node.type'), 'id' => $node_id, @@ -66,7 +66,7 @@ public static function updateThisNode($data = []): void { $node_id = config('settings.node.id'); - $node = self::hget('nodes', $node_id, "[]"); + $node = self::hget('nodes', $node_id, '[]'); $node = json_decode($node, true); $node = array_merge($node, $data); @@ -74,18 +74,16 @@ public static function updateThisNode($data = []): void ClusterSupport::hset('nodes', $node_id, $node); } - public static function hset($key, $value, $data = []): void { /** @noinspection PhpUndefinedMethodInspection */ - Redis::hset(self::$prefix . $key, $value, json_encode($data)); + Redis::hset(self::$prefix.$key, $value, json_encode($data)); } /** - * @param string|array $events 事件名称 - * @param $callback callable 回调函数,接收一个参数,为事件数据。 - * @param $ignore_self bool 是否忽略此节点的消息。 - * + * @param string|array $events 事件名称 + * @param $callback callable 回调函数,接收一个参数,为事件数据。 + * @param $ignore_self bool 是否忽略此节点的消息。 * @return void */ public static function listen(string|array $events, callable $callback, bool $ignore_self = true): void @@ -116,13 +114,13 @@ public static function listen(string|array $events, callable $callback, bool $ig public static function get($key, $default = null): string|array|null { /** @noinspection PhpUndefinedMethodInspection */ - return Redis::get(self::$prefix . $key, $default); + return Redis::get(self::$prefix.$key, $default); } public static function forget($key): void { /** @noinspection PhpUndefinedMethodInspection */ - Redis::forget(self::$prefix . $key); + Redis::forget(self::$prefix.$key); } // forever @@ -134,13 +132,13 @@ public static function forever($key, $value): void public static function set($key, $value, $ttl = null): void { /** @noinspection PhpUndefinedMethodInspection */ - Redis::set(self::$prefix . $key, $value, $ttl); + Redis::set(self::$prefix.$key, $value, $ttl); } public static function hget($key, $hashKey, $default = []): string|array|null { /** @noinspection PhpUndefinedMethodInspection */ - $value = Redis::hget(self::$prefix . $key, $hashKey); + $value = Redis::hget(self::$prefix.$key, $hashKey); return $value ?: $default; } @@ -167,7 +165,7 @@ public static function nodes($hide_ip = false): array public static function hgetAll($hashKey, $default = []): array { /** @noinspection PhpUndefinedMethodInspection */ - $value = Redis::hgetall(self::$prefix . $hashKey); + $value = Redis::hgetall(self::$prefix.$hashKey); return $value ?: $default; } diff --git a/deploy.php b/deploy.php index 25cc4bc..a80331d 100644 --- a/deploy.php +++ b/deploy.php @@ -1,4 +1,5 @@