From c9c65aaafe74fa4618b75b43ce5a0d9bbc7996bb Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Thu, 5 Jan 2023 02:29:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20Init.php=20=E6=95=B4?= =?UTF-8?q?=E5=90=88=E5=88=B0=20Work.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Cluster/Work.php | 38 +++++++++++++-- app/Console/Commands/Init.php | 70 --------------------------- 2 files changed, 33 insertions(+), 75 deletions(-) delete mode 100644 app/Console/Commands/Init.php diff --git a/app/Console/Commands/Cluster/Work.php b/app/Console/Commands/Cluster/Work.php index 7118144..ad716c3 100644 --- a/app/Console/Commands/Cluster/Work.php +++ b/app/Console/Commands/Cluster/Work.php @@ -5,6 +5,7 @@ use App\Support\Cluster; use Illuminate\Console\Command; use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Str; use Symfony\Component\Console\Command\Command as CommandAlias; class Work extends Command @@ -30,18 +31,43 @@ class Work extends Command */ public function handle(): int { + Artisan::call('config:cache'); + if (!config('settings.node.ip')) { $this->error('请先配置节点 IP。'); return CommandAlias::FAILURE; } - $this->warn('正在初始化集群协调任务。'); - Artisan::call('init'); - Artisan::call('optimize'); + // if not node_id + if (!config('settings.node.id')) { + // 重写 .env 文件中的 NODE_ID + $this->info('正在重写 .env 文件中的 NODE_ID。'); + + $node_id = Str::random(8); + + if (config('settings.node.type') === 'master') { + $node_id = 'master'; + } + + $env = file_get_contents(base_path('.env')); + + $env = preg_replace('/^NODE_ID=.*$/m', 'NODE_ID=' . $node_id, $env); + + file_put_contents(base_path('.env'), $env); + } + + + // 刷新配置缓存 + $this->info('正在刷新配置缓存。'); + Artisan::call('config:cache'); + + // redis 创建一个 hash + $this->info('正在注册节点。'); + Cluster::registerThisNode(); + + $this->info('初始化完成。'); $this->info('正在启动集群协调任务。'); - - $pid = pcntl_fork(); if ($pid === -1) { $this->error('无法创建子进程。'); @@ -94,6 +120,8 @@ private function report(): void { $this->info('正在报告此系统,请保持此命令一直运行。'); + Artisan::call('config:cache'); + $cpu = $this->getCpuUsage(); while (1) { diff --git a/app/Console/Commands/Init.php b/app/Console/Commands/Init.php deleted file mode 100644 index 9a1443d..0000000 --- a/app/Console/Commands/Init.php +++ /dev/null @@ -1,70 +0,0 @@ -error('请先配置节点 IP。'); - return CommandAlias::FAILURE; - } - - // 重写 .env 文件中的 NODE_ID - $this->info('正在重写 .env 文件中的 NODE_ID。'); - - $node_id = Str::random(8); - - if (config('settings.node.type') === 'master') { - $node_id = 'master'; - } - - $env = file_get_contents(base_path('.env')); - - $env = preg_replace('/^NODE_ID=.*$/m', 'NODE_ID=' . $node_id, $env); - - file_put_contents(base_path('.env'), $env); - - - // 刷新配置缓存 - $this->info('正在刷新配置缓存。'); - Artisan::call('config:cache'); - - // redis 创建一个 hash - $this->info('正在注册节点。'); - Cluster::registerThisNode(); - - $this->info('初始化完成。'); - - return CommandAlias::SUCCESS; - } -}