From 39fd536baebe2365cbff58382c4a43576acc4b49 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Thu, 5 Jan 2023 13:20:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=BA=20pipe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Cluster/Work.php | 39 ++++++++++++++------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/app/Console/Commands/Cluster/Work.php b/app/Console/Commands/Cluster/Work.php index 621e6f0..573a5ff 100644 --- a/app/Console/Commands/Cluster/Work.php +++ b/app/Console/Commands/Cluster/Work.php @@ -5,7 +5,6 @@ use App\Support\Cluster; use Illuminate\Console\Command; use Illuminate\Support\Facades\Artisan; -use Illuminate\Support\Facades\Redis; use Illuminate\Support\Str; use Symfony\Component\Console\Command\Command as CommandAlias; @@ -123,21 +122,7 @@ public function handle(): int $this->info('正在启动 Web。'); $command = 'php artisan octane:start --host=0.0.0.0 --rpc-port=6001 --port=8000'; - // proc_open - $descriptor_spec = [ - 0 => ['pipe', 'r'], - 1 => ['pipe', 'w'], - 2 => ['pipe', 'w'], - ]; - - $process = proc_open($command, $descriptor_spec, $pipes); - - if (is_resource($process)) { - while ($s = fgets($pipes[1])) { - echo $s; - } - } - + $this->pipeCommand($command); } else { // 子进程 @@ -180,7 +165,8 @@ private function dispatchEvent($event, $message = []): void 'cluster.restart.web' => function () { $this->info('正在重启 Web。'); - exec('php artisan octane:reload'); + $this->pipeCommand('php artisan octane:reload'); + Cluster::publish('cluster.restarted.web'); @@ -189,7 +175,7 @@ private function dispatchEvent($event, $message = []): void 'cluster.restart.all' => function () { $this->info('正在重启整个莱云。'); - exec('supervisorctl restart all'); + $this->pipeCommand('supervisorctl restart all'); Cluster::publish('cluster.restarted.all'); @@ -226,4 +212,21 @@ private function getCpuUsage(): float $cpu = sys_getloadavg(); return $cpu[0]; } + + private function pipeCommand($command): void + { + $descriptor_spec = [ + 0 => ['pipe', 'r'], + 1 => ['pipe', 'w'], + 2 => ['pipe', 'w'], + ]; + + $process = proc_open($command, $descriptor_spec, $pipes); + + if (is_resource($process)) { + while ($s = fgets($pipes[1])) { + echo $s; + } + } + } }