data = vCenter::select('id','host','user','password','token')->get(); $this->vCenter = new ServiceVCenter; } /** * Execute the job. * * @return void */ public function handle() { foreach ($this->data as $item) { $this->vCenter->apiUri = $item->host; if (!$item->token) { $token = $this->vCenter->loginServer($item); if ($token) { vCenter::where('id', $item->id)->update(['token' => $token]); $this->vCenter->token = $token; } } $this->vCenter->token = $item->token; if (! $this->vCenter->token) { continue; } $dataCenter = ModuleDatacenter::where('vcenter_id', $item->id)->select('id', 'datacenter_id')->get(); foreach ($dataCenter as $_item) { $allHost = ModuleHost::where('module_datacenter_id', $_item->id) ->select('id', 'name', 'host_id') ->get(); if ($allHost) { foreach ($allHost as $hostItem) { $network = $this->vCenter->loadNetWork($item, $hostItem->host_id); if ($network === "Login_Again") { $token = $this->vCenter->loginServer($item); if ($token) { vCenter::where('id', $item->id)->update(['token' => $token]); $this->vCenter->token = $token; $network = $this->vCenter->loadNetWork($item, $hostItem->host_id); } } if ($network) { foreach ($network as $_subItem) { ModuleNetwork::updateOrCreate( [ 'network' => $_subItem['network'], 'module_host_id' => $hostItem->id ], [ 'name' => $_subItem['name'], 'type' => $_subItem['type'], ] ); } } } } } } // Notify success via Telegram (new \App\Http\Services\Telegram())->SendJobRunInfo('SyncNetwork', 'Success', [ 'message' => 'Sync network completed successfully', 'synced_vcenters' => $this->data->count(), ]); } }