增加 清理心跳超时节点命令

This commit is contained in:
iVampireSP.com 2023-02-09 19:12:11 +08:00
parent 786cb2b076
commit 126a18fac5
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132

View File

@ -0,0 +1,44 @@
<?php
namespace App\Console\Commands\Cluster;
use App\Support\ClusterSupport;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;
class CleanOffline extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cluster:clean-offline';
/**
* The console command description.
*
* @var string
*/
protected $description = '清理心跳超过 30 秒的节点。';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$nodes = ClusterSupport::nodes();
foreach ($nodes as $node) {
$now = time();
if ($now - $node['last_heartbeat'] > 30) {
$this->info("节点 {$node['id']} 已离线,将被清理。");
ClusterSupport::removeNode($node['id']);
}
}
return CommandAlias::SUCCESS;
}
}