data = vCenter::select('id', 'host', 'user', 'password', 'token')->get(); $this->vCenter = new ServiceVCenter; $this->telegram = new Telegram(); } /** * Execute the job. * * @return void */ public function handle() { try { 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) { $host = $this->vCenter->getAllHost($_item['datacenter_id']); if ($host === "Login_Again") { $token = $this->vCenter->loginServer($item); if ($token) { vCenter::where('id', $item->id)->update(['token' => $token]); $this->vCenter->token = $token; } } $host = $this->vCenter->getAllHost($_item['datacenter_id']); $template = $this->vCenter->getAllTemplate(); if ($host) { foreach ($host as $_subItem) { if (!ModuleHost::where('host_id', $_subItem['host'])->first()) { ModuleHost::create([ 'name' => $_subItem['name'], 'host_id' => $_subItem['host'], 'module_datacenter_id' => $_item['id'], 'template' => $template[0] ?? null, ]); } } } } } // Notify success via Telegram $this->telegram->SendJobRunInfo('SyncHost', 'Success', [ 'message' => 'Sync host completed successfully', 'synced_vcenters' => $this->data->count(), ]); } catch (\Throwable $exception) { // Ghi log Laravel Log::error('SyncHost Job failed', [ 'error' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ]); // Gửi log lỗi qua Telegram $this->telegram->SendJobRunInfo('SyncHost', 'Failed', [ 'error' => $exception->getMessage(), 'file' => $exception->getFile(), 'line' => $exception->getLine(), ]); // Quăng lại lỗi để Laravel xử lý retry/failure throw $exception; } } public function failed(\Throwable $exception) { Log::error('SyncHost Job failed', [ 'error' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ]); } }