From ea741c8e3e4b57bcc1f5965326c9cceae0d99a92 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Thu, 5 Jan 2023 03:17:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E9=9B=86=E7=BE=A4?= =?UTF-8?q?=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 3 ++ app/Console/Commands/Cluster/Work.php | 57 ++++++++++++++++++++++++++- config/settings.php | 5 ++- supervisor/lae-cluster.conf | 13 ------ supervisor/lae-web.conf | 4 +- 5 files changed, 64 insertions(+), 18 deletions(-) delete mode 100644 supervisor/lae-cluster.conf diff --git a/.env.example b/.env.example index ea202d3..b82560a 100644 --- a/.env.example +++ b/.env.example @@ -106,6 +106,9 @@ DASHBOARD_BIRTHDAY_PATH=/birthdays # 可信代理,用于获取真实 IP。多个 IP 用逗号分隔。 TRUSTED_PROXIES= +# Roadrunner 的版本,比如 2.12.1, 默认也是 2.12.1 +ROADRUNNER_VERSION= + NODE_TYPE=master NODE_ID= NODE_IP= diff --git a/app/Console/Commands/Cluster/Work.php b/app/Console/Commands/Cluster/Work.php index 24afd5a..94e4db8 100644 --- a/app/Console/Commands/Cluster/Work.php +++ b/app/Console/Commands/Cluster/Work.php @@ -31,6 +31,43 @@ class Work extends Command */ public function handle(): int { + + // 检测目录下是否有 rr + if (!file_exists(base_path('rr'))) { + $this->warn('未找到 rr 文件,将自动下载。'); + + // 获取操作系统是 darwin 还是 linux + $os = Str::contains(PHP_OS, 'Darwin') ? 'darwin' : 'linux'; + + // 获取操作系统是 arm64 还是 amd64 + $arch = Str::contains(php_uname('m'), ['aarch64', 'arm64']) ? 'arm64' : 'amd64'; + + // 下载 rr + $this->info('正在下载 rr。'); + + $version = config('settings.roadrunner.version'); + + $download_link = "https://github.sakurapuare.com/roadrunner-server/roadrunner/releases/download/v{$version}/roadrunner-{$version}-{$os}-{$arch}.tar.gz"; + + $save_name = 'rr_download.tar.gz'; + + // 下载(wget) + exec("wget {$download_link} -O {$save_name}"); + exec("tar -zxvf {$save_name}"); + + // 删除下载的压缩包 + exec("rm {$save_name}"); + + // 提取解压目录下的 rr 文件 + exec("mv roadrunner-{$version}-{$os}-$arch/rr rr"); + + // 删除解压目录 + exec("rm -rf roadrunner-{$version}-{$os}-$arch"); + + // 设置 rr 可执行权限 + exec("chmod +x rr"); + } + Artisan::call('config:cache'); if (!config('settings.node.ip')) { @@ -66,13 +103,29 @@ public function handle(): int $this->info('初始化完成。'); $this->info('正在启动集群协调任务。'); + $pid = pcntl_fork(); if ($pid === -1) { $this->error('无法创建子进程。'); return CommandAlias::FAILURE; } else if ($pid === 0) { - // 子进程 - $this->report(); + // 再打开一个,负责 octane + $pid = pcntl_fork(); + if ($pid === -1) { + $this->error('无法创建子进程。'); + return CommandAlias::FAILURE; + } else if ($pid === 0) { + // 子进程 + $this->info('正在启动 Web。'); + + $command = 'php artisan octane:start --host=0.0.0.0 --rpc-port=6001 --port=8000'; + exec($command); + + } else { + // 子进程 + $this->report(); + } + } else { // 父进程 $this->work(); diff --git a/config/settings.php b/config/settings.php index 99ac69f..ba98e5c 100644 --- a/config/settings.php +++ b/config/settings.php @@ -19,5 +19,8 @@ 'type' => env('NODE_TYPE', 'master'), 'id' => env('NODE_ID'), 'ip' => env('NODE_IP'), - ] + ], + 'roadrunner' => [ + 'version' => env('ROADRUNNER_VERSION', '2.12.1'), + ], ]; diff --git a/supervisor/lae-cluster.conf b/supervisor/lae-cluster.conf deleted file mode 100644 index 63da143..0000000 --- a/supervisor/lae-cluster.conf +++ /dev/null @@ -1,13 +0,0 @@ -[program:lae-worker] -process_name=%(program_name)s_%(process_num)02d -directory=/opt/lae -command=php artisan cluster:work -autostart=true -autorestart=true -stopasgroup=true -killasgroup=true -user=www -numprocs=1 -redirect_stderr=true -stdout_logfile=/var/log/lae-cluster.log -stopwaitsecs=3600 diff --git a/supervisor/lae-web.conf b/supervisor/lae-web.conf index 7eb6fb0..40c90af 100644 --- a/supervisor/lae-web.conf +++ b/supervisor/lae-web.conf @@ -1,12 +1,12 @@ [program:lae-web] process_name=%(program_name)s_%(process_num)02d directory=/opt/lae -command=php artisan octane:start --host=0.0.0.0 --rpc-port=6001 --port=8000 +command=php artisan cluster:work autostart=true autorestart=true stopasgroup=true killasgroup=true -user=www +user=root numprocs=1 redirect_stderr=true stdout_logfile=/var/log/lae-web.log