删除 Init.php

整合到 Work.php
This commit is contained in:
iVampireSP.com 2023-01-05 02:29:35 +08:00
parent df5b9a2209
commit c9c65aaafe
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 33 additions and 75 deletions

View File

@ -5,6 +5,7 @@
use App\Support\Cluster; use App\Support\Cluster;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
use Symfony\Component\Console\Command\Command as CommandAlias; use Symfony\Component\Console\Command\Command as CommandAlias;
class Work extends Command class Work extends Command
@ -30,18 +31,43 @@ class Work extends Command
*/ */
public function handle(): int public function handle(): int
{ {
Artisan::call('config:cache');
if (!config('settings.node.ip')) { if (!config('settings.node.ip')) {
$this->error('请先配置节点 IP。'); $this->error('请先配置节点 IP。');
return CommandAlias::FAILURE; return CommandAlias::FAILURE;
} }
$this->warn('正在初始化集群协调任务。'); // if not node_id
Artisan::call('init'); if (!config('settings.node.id')) {
Artisan::call('optimize'); // 重写 .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('正在启动集群协调任务。'); $this->info('正在启动集群协调任务。');
$pid = pcntl_fork(); $pid = pcntl_fork();
if ($pid === -1) { if ($pid === -1) {
$this->error('无法创建子进程。'); $this->error('无法创建子进程。');
@ -94,6 +120,8 @@ private function report(): void
{ {
$this->info('正在报告此系统,请保持此命令一直运行。'); $this->info('正在报告此系统,请保持此命令一直运行。');
Artisan::call('config:cache');
$cpu = $this->getCpuUsage(); $cpu = $this->getCpuUsage();
while (1) { while (1) {

View File

@ -1,70 +0,0 @@
<?php
namespace App\Console\Commands;
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;
class Init extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'init';
/**
* The console command description.
*
* @var string
*/
protected $description = '注册此节点到集群中。';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
Artisan::call('optimize');
if (!config('settings.node.ip')) {
$this->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;
}
}