From 342bd1d17dbf0a972f858910017ba6f17f528fe2 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Thu, 9 Feb 2023 18:56:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=9D=83=E9=87=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Cluster/Work.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/app/Console/Commands/Cluster/Work.php b/app/Console/Commands/Cluster/Work.php index 03fcaa7..2dab2d5 100644 --- a/app/Console/Commands/Cluster/Work.php +++ b/app/Console/Commands/Cluster/Work.php @@ -15,7 +15,7 @@ class Work extends Command * * @var string */ - protected $signature = 'works'; + protected $signature = 'works {weight=1}'; /** * The console command description. @@ -24,6 +24,8 @@ class Work extends Command */ protected $description = '启动此应用程序。'; + private string $weight = "1"; + /** * Execute the console command. * @@ -32,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 @@ -77,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; @@ -92,9 +94,13 @@ public function handle(): int $node_port = $node_ips[1]; } + $this->weight = $this->argument('weight'); + // redis 创建一个 hash $this->info('正在注册节点。'); - ClusterSupport::registerThisNode(); + ClusterSupport::registerThisNode(true, [ + 'weight' => $this->weight, + ]); $this->info('初始化完成。'); @@ -105,16 +111,17 @@ public function handle(): int $this->error('无法创建子进程。'); return CommandAlias::FAILURE; - } elseif ($pid === 0) { + } else if ($pid === 0) { // 再打开一个,负责 octane $pid = pcntl_fork(); if ($pid === -1) { $this->error('无法创建子进程。'); return CommandAlias::FAILURE; - } elseif ($pid === 0) { + } else if ($pid === 0) { // 子进程 $this->info('正在启动 Web。'); + $this->info('以权重 ' . $this->argument('weight') . ' 启动。'); $rpc_port = config('settings.node.rpc_port'); @@ -189,7 +196,10 @@ private function work(): void { $this->info('正在监听任务。'); - ClusterSupport::publish('node.online'); + ClusterSupport::publish('node.online', [ + 'weight' => $this->weight, + ]); + ClusterSupport::listen('*', function ($event, $message) { $this->dispatchEvent($event, $message); }, false);