格式化代码
This commit is contained in:
parent
f852717191
commit
876f3dff04
@ -52,11 +52,11 @@ private function format(string $event, array $message = [])
|
|||||||
{
|
{
|
||||||
$status = $this->switch($event, $message['data']);
|
$status = $this->switch($event, $message['data']);
|
||||||
|
|
||||||
if (!$status) {
|
if (! $status) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = "[{$message['node']['type']}] {$message['node']['id']}:$event: " . $status;
|
$message = "[{$message['node']['type']}] {$message['node']['id']}:$event: ".$status;
|
||||||
|
|
||||||
$this->info($message);
|
$this->info($message);
|
||||||
}
|
}
|
||||||
@ -66,7 +66,7 @@ public function switch($event, $message = []): string|null
|
|||||||
$events = [
|
$events = [
|
||||||
'monitor.started' => '我正在监听集群消息。',
|
'monitor.started' => '我正在监听集群消息。',
|
||||||
'node.ok' => '此节点初始化成功,并且已经加入集群。',
|
'node.ok' => '此节点初始化成功,并且已经加入集群。',
|
||||||
'node.online' => fn($message) => '此节点已经上线,启动权重为:' . $message['weight'],
|
'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 节点下载。',
|
||||||
@ -74,17 +74,17 @@ public function switch($event, $message = []): string|null
|
|||||||
'config.synced' => '我已下载配置文件。',
|
'config.synced' => '我已下载配置文件。',
|
||||||
'edge.deployed' => '已成功根据集群节点生成配置文件并应用。',
|
'edge.deployed' => '已成功根据集群节点生成配置文件并应用。',
|
||||||
'edge.launched' => '边缘节点成功启动。',
|
'edge.launched' => '边缘节点成功启动。',
|
||||||
'edge.error' => fn($message) => $message['message'] ?? '未知错误',
|
'edge.error' => fn ($message) => $message['message'] ?? '未知错误',
|
||||||
'cluster.restart.web' => '正在重启 web 服务。',
|
'cluster.restart.web' => '正在重启 web 服务。',
|
||||||
'cluster.restart.all' => '正在重启 整个 服务。',
|
'cluster.restart.all' => '正在重启 整个 服务。',
|
||||||
'cluster.restarted.web' => 'Web 重启好了。',
|
'cluster.restarted.web' => 'Web 重启好了。',
|
||||||
'cluster.restarted.all' => '整个 重启好了。',
|
'cluster.restarted.all' => '整个 重启好了。',
|
||||||
'cluster.deployed' => '集群配置文件已经部署。',
|
'cluster.deployed' => '集群配置文件已经部署。',
|
||||||
'cluster.deployed.error' => fn($message) => $message['message'] ?? '未知错误',
|
'cluster.deployed.error' => fn ($message) => $message['message'] ?? '未知错误',
|
||||||
'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),
|
'weight.updated' => fn ($message) => $this->handleWeightUpdated($message),
|
||||||
];
|
];
|
||||||
|
|
||||||
$resp = $events[$event] ?? null;
|
$resp = $events[$event] ?? null;
|
||||||
@ -118,10 +118,10 @@ private function handleOutgoingRequest(array $message): string
|
|||||||
private function handleWeightUpdated(array $message): string
|
private function handleWeightUpdated(array $message): string
|
||||||
{
|
{
|
||||||
$msg = '';
|
$msg = '';
|
||||||
if ($message['weight'] === "0") {
|
if ($message['weight'] === '0') {
|
||||||
$msg .= '我不参与调度了。';
|
$msg .= '我不参与调度了。';
|
||||||
} else {
|
} else {
|
||||||
$msg .= '我的权重是:' . $message['weight'] . '。';
|
$msg .= '我的权重是:'.$message['weight'].'。';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $msg;
|
return $msg;
|
||||||
|
@ -24,7 +24,7 @@ class Work extends Command
|
|||||||
*/
|
*/
|
||||||
protected $description = '启动此应用程序。';
|
protected $description = '启动此应用程序。';
|
||||||
|
|
||||||
private string $weight = "1";
|
private string $weight = '1';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the console command.
|
* Execute the console command.
|
||||||
@ -34,7 +34,7 @@ class Work extends Command
|
|||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
// 检测目录下是否有 rr
|
// 检测目录下是否有 rr
|
||||||
if (!file_exists(base_path('rr'))) {
|
if (! file_exists(base_path('rr'))) {
|
||||||
$this->warn('未找到 rr 文件,将自动下载。');
|
$this->warn('未找到 rr 文件,将自动下载。');
|
||||||
|
|
||||||
// 获取操作系统是 darwin 还是 linux
|
// 获取操作系统是 darwin 还是 linux
|
||||||
@ -79,7 +79,7 @@ public function handle(): int
|
|||||||
|
|
||||||
$node_ip = config('settings.node.ip');
|
$node_ip = config('settings.node.ip');
|
||||||
|
|
||||||
if (!$node_ip) {
|
if (! $node_ip) {
|
||||||
$this->error('请先配置节点 IP。');
|
$this->error('请先配置节点 IP。');
|
||||||
|
|
||||||
return CommandAlias::FAILURE;
|
return CommandAlias::FAILURE;
|
||||||
@ -111,17 +111,17 @@ public function handle(): int
|
|||||||
$this->error('无法创建子进程。');
|
$this->error('无法创建子进程。');
|
||||||
|
|
||||||
return CommandAlias::FAILURE;
|
return CommandAlias::FAILURE;
|
||||||
} else if ($pid === 0) {
|
} elseif ($pid === 0) {
|
||||||
// 再打开一个,负责 octane
|
// 再打开一个,负责 octane
|
||||||
$pid = pcntl_fork();
|
$pid = pcntl_fork();
|
||||||
if ($pid === -1) {
|
if ($pid === -1) {
|
||||||
$this->error('无法创建子进程。');
|
$this->error('无法创建子进程。');
|
||||||
|
|
||||||
return CommandAlias::FAILURE;
|
return CommandAlias::FAILURE;
|
||||||
} else if ($pid === 0) {
|
} elseif ($pid === 0) {
|
||||||
// 子进程
|
// 子进程
|
||||||
$this->info('正在启动 Web。');
|
$this->info('正在启动 Web。');
|
||||||
$this->info('以权重 ' . $this->argument('weight') . ' 启动。');
|
$this->info('以权重 '.$this->argument('weight').' 启动。');
|
||||||
|
|
||||||
$rpc_port = config('settings.node.rpc_port');
|
$rpc_port = config('settings.node.rpc_port');
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public static function publish($event, $data = [], $register = false): void
|
|||||||
public static function registerThisNode($report = true, $data = []): void
|
public static function registerThisNode($report = true, $data = []): void
|
||||||
{
|
{
|
||||||
$node_id = config('settings.node.id');
|
$node_id = config('settings.node.id');
|
||||||
|
|
||||||
$node = [
|
$node = [
|
||||||
'type' => config('settings.node.type'),
|
'type' => config('settings.node.type'),
|
||||||
'id' => $node_id,
|
'id' => $node_id,
|
||||||
@ -66,7 +66,7 @@ public static function updateThisNode($data = []): void
|
|||||||
{
|
{
|
||||||
$node_id = config('settings.node.id');
|
$node_id = config('settings.node.id');
|
||||||
|
|
||||||
$node = self::hget('nodes', $node_id, "[]");
|
$node = self::hget('nodes', $node_id, '[]');
|
||||||
$node = json_decode($node, true);
|
$node = json_decode($node, true);
|
||||||
|
|
||||||
$node = array_merge($node, $data);
|
$node = array_merge($node, $data);
|
||||||
@ -74,18 +74,16 @@ public static function updateThisNode($data = []): void
|
|||||||
ClusterSupport::hset('nodes', $node_id, $node);
|
ClusterSupport::hset('nodes', $node_id, $node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function hset($key, $value, $data = []): void
|
public static function hset($key, $value, $data = []): void
|
||||||
{
|
{
|
||||||
/** @noinspection PhpUndefinedMethodInspection */
|
/** @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 string|array $events 事件名称
|
||||||
* @param $callback callable 回调函数,接收一个参数,为事件数据。
|
* @param $callback callable 回调函数,接收一个参数,为事件数据。
|
||||||
* @param $ignore_self bool 是否忽略此节点的消息。
|
* @param $ignore_self bool 是否忽略此节点的消息。
|
||||||
*
|
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function listen(string|array $events, callable $callback, bool $ignore_self = true): 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
|
public static function get($key, $default = null): string|array|null
|
||||||
{
|
{
|
||||||
/** @noinspection PhpUndefinedMethodInspection */
|
/** @noinspection PhpUndefinedMethodInspection */
|
||||||
return Redis::get(self::$prefix . $key, $default);
|
return Redis::get(self::$prefix.$key, $default);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function forget($key): void
|
public static function forget($key): void
|
||||||
{
|
{
|
||||||
/** @noinspection PhpUndefinedMethodInspection */
|
/** @noinspection PhpUndefinedMethodInspection */
|
||||||
Redis::forget(self::$prefix . $key);
|
Redis::forget(self::$prefix.$key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// forever
|
// forever
|
||||||
@ -134,13 +132,13 @@ public static function forever($key, $value): void
|
|||||||
public static function set($key, $value, $ttl = null): void
|
public static function set($key, $value, $ttl = null): void
|
||||||
{
|
{
|
||||||
/** @noinspection PhpUndefinedMethodInspection */
|
/** @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
|
public static function hget($key, $hashKey, $default = []): string|array|null
|
||||||
{
|
{
|
||||||
/** @noinspection PhpUndefinedMethodInspection */
|
/** @noinspection PhpUndefinedMethodInspection */
|
||||||
$value = Redis::hget(self::$prefix . $key, $hashKey);
|
$value = Redis::hget(self::$prefix.$key, $hashKey);
|
||||||
|
|
||||||
return $value ?: $default;
|
return $value ?: $default;
|
||||||
}
|
}
|
||||||
@ -167,7 +165,7 @@ public static function nodes($hide_ip = false): array
|
|||||||
public static function hgetAll($hashKey, $default = []): array
|
public static function hgetAll($hashKey, $default = []): array
|
||||||
{
|
{
|
||||||
/** @noinspection PhpUndefinedMethodInspection */
|
/** @noinspection PhpUndefinedMethodInspection */
|
||||||
$value = Redis::hgetall(self::$prefix . $hashKey);
|
$value = Redis::hgetall(self::$prefix.$hashKey);
|
||||||
|
|
||||||
return $value ?: $default;
|
return $value ?: $default;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Deployer;
|
namespace Deployer;
|
||||||
|
|
||||||
require 'recipe/laravel.php';
|
require 'recipe/laravel.php';
|
||||||
|
Loading…
Reference in New Issue
Block a user