productId = $productId; $this->userId = $userId; } /** * Execute the job. */ public function handle(vCenter $vCenterService) { Log::info("Job StartVmJob started for Product ID: {$this->productId}"); $product = ModuleVmware::with('ipAddress.host.datacenter.vCenter')->find($this->productId); if (!$product) { Log::error("StartVmJob: Product not found ID: {$this->productId}"); return; } try { // Call vCenter Service $result = $vCenterService->StartVirtualMachine($product, $product->vmware_id); // Check for Login_Again if ($result === 'Login_Again') { $vCenterService->loginServer($product->ipAddress->host->datacenter->vCenter); $result = $vCenterService->StartVirtualMachine($product, $product->vmware_id); } if (is_array($result) && isset($result['error']) && $result['error'] === false) { Log::info("StartVmJob: Command sent successfully for VM {$product->vmware_id}"); // Clear VM info cache \Illuminate\Support\Facades\Cache::forget('vm_info_' . $product->vmware_id); // Dispatch Event to Frontend \App\Events\VirtualMachineStatusChanged::dispatch( $this->userId, $product->vmware_id, 'running', __('Máy ảo đã được khởi động thành công.') ); // Update status to 'processing' (e.g., 3) if needed, or leave it to WaitStatus $product->update(['status' => 3]); // 3 = Processing/Pending // Dispatch wait status job // Note: WaitStatusVirtualMachine expects ($vmware_id, $service, $user, $status_to_wait_for?) // Looking at existing code: // $status = 2; // Suspend // $jobs = new WaitStatusVirtualMachine($product->vmware_id,$serviceVCenter,$product->customer_id,true); (true = wait for operations_ready?) // For Start: // $jobs = new WaitStatusVirtualMachine($product->vmware_id, $serviceVCenter, $product->customer_id, false); dispatch(new WaitStatusVirtualMachine( $product->vmware_id, $vCenterService, $this->userId, false // Expect operations_ready = false initially? Logic in WaitStatus is weird. ))->delay(now()->addSeconds(5)); } else { $msg = is_array($result) ? ($result['data'] ?? 'Unknown error') : $result; Log::error("StartVmJob: Failed for VM {$product->vmware_id}. Error: {$msg}"); // Optionally notify user via Pusher/Telegram here } } catch (\Exception $e) { Log::error("StartVmJob: Exception for VM {$product->vmware_id}: " . $e->getMessage()); $this->fail($e); } } }