productId = $productId; $this->userId = $userId; } /** * Execute the job. */ public function handle(vCenter $vCenterService) { Log::info("Job StopVmJob started for Product ID: {$this->productId}"); $product = ModuleVmware::with('ipAddress.host.datacenter.vCenter')->find($this->productId); if (!$product) { Log::error("StopVmJob: Product not found ID: {$this->productId}"); return; } try { // Call vCenter Service - Graceful Shutdown $result = $vCenterService->ShutdownGuestVirtualMachine($product, $product->vmware_id); // Check for Login_Again if ($result === 'Login_Again') { $vCenterService->loginServer($product->ipAddress->host->datacenter->vCenter); $result = $vCenterService->ShutdownGuestVirtualMachine($product, $product->vmware_id); } if (is_array($result) && isset($result['error']) && $result['error'] === false) { // Clear VM info cache \Illuminate\Support\Facades\Cache::forget(\App\Support\Cache\CacheKey::build('vm', 'info', ['id' => $product->vmware_id])); // Dispatch Event to Frontend \App\Events\VirtualMachineStatusChanged::dispatch( $this->userId, $product->vmware_id, 'stopped', __('Máy ảo đã được tắt thành công.') ); $product->update(['status' => 3]); // Processing dispatch(new WaitStatusVirtualMachine( $product->vmware_id, $vCenterService, $this->userId, true ))->delay(now()->addSeconds(5)); } else { $msg = is_array($result) ? ($result['data'] ?? 'Unknown error') : $result; Log::error("StopVmJob: Failed for VM {$product->vmware_id}. Error: {$msg}"); } } catch (\Exception $e) { Log::error("StopVmJob: Exception for VM {$product->vmware_id}: " . $e->getMessage()); $this->fail($e); } } }