Lae/app/Console/Commands/Cluster/Work.php

115 lines
2.6 KiB
PHP
Raw Normal View History

2023-01-04 12:18:22 +00:00
<?php
namespace App\Console\Commands\Cluster;
2023-01-04 15:57:36 +00:00
use App\Support\Cluster;
2023-01-04 12:18:22 +00:00
use Illuminate\Console\Command;
2023-01-04 15:57:36 +00:00
use Illuminate\Support\Facades\Artisan;
2023-01-04 12:18:22 +00:00
use Symfony\Component\Console\Command\Command as CommandAlias;
class Work extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cluster:work';
/**
* The console command description.
*
* @var string
*/
protected $description = '开始集群协调任务。';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
2023-01-04 18:20:04 +00:00
if (!config('settings.node.ip')) {
$this->error('请先配置节点 IP。');
return CommandAlias::FAILURE;
}
2023-01-04 15:57:36 +00:00
$this->warn('正在初始化集群协调任务。');
Artisan::call('init');
Artisan::call('optimize');
$this->info('正在启动集群协调任务。');
$pid = pcntl_fork();
if ($pid === -1) {
$this->error('无法创建子进程。');
return CommandAlias::FAILURE;
} else if ($pid === 0) {
// 子进程
$this->report();
} else {
// 父进程
$this->work();
}
2023-01-04 12:18:22 +00:00
return CommandAlias::SUCCESS;
}
2023-01-04 15:57:36 +00:00
private function work(): void
{
$this->info('正在监听任务。');
Cluster::publish('node.online');
Cluster::listen('*', function ($event, $message) {
$this->dispatchEvent($event, $message);
}, false);
}
private function dispatchEvent($event, $message = []): void
{
$events = [
'config.updated' => function () {
$this->info('正在更新配置文件。');
Artisan::call('cluster:sync', [
'--force' => 'true',
]);
$this->info('配置文件更新完成。');
}
];
if (isset($events[$event])) {
$this->warn("正在处理 {$event} 事件。");
$events[$event]($message);
}
}
private function report(): void
{
$this->info('正在报告此系统,请保持此命令一直运行。');
$cpu = $this->getCpuUsage();
while (1) {
Cluster::publish('system_usage', [
'cpu' => $cpu,
]);
sleep(1);
}
}
private function getCpuUsage(): float
{
// 获取 CPU 使用率
$cpu = sys_getloadavg();
return $cpu[0];
}
2023-01-04 12:18:22 +00:00
}