CheckNodeIp.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Jobs\Node;
  3. use App\Events\NodeActions;
  4. use App\Utils\NetworkDetection;
  5. use Exception;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Log;
  12. class CheckNodeIp implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. public function __construct(
  16. public int $nodeId,
  17. public array $ips,
  18. public int $port,
  19. public ?int $controllerNodeId = null
  20. ) {
  21. }
  22. public function handle(): void
  23. {
  24. foreach ($this->ips as $ip) {
  25. $ret = ['ip' => $ip, 'icmp' => 4, 'tcp' => 4, 'nodeId' => $this->nodeId];
  26. try {
  27. $status = NetworkDetection::networkStatus($ip, $this->port ?? 22);
  28. $ret['icmp'] = $status['icmp'];
  29. $ret['tcp'] = $status['tcp'];
  30. } catch (Exception $e) {
  31. Log::error("节点 [{$this->nodeId}] IP [$ip] 检测失败: ".$e->getMessage());
  32. }
  33. broadcast(new NodeActions('check', $ret, $this->controllerNodeId));
  34. }
  35. }
  36. }