productId = $productId; $this->snapshotId = $snapshotId; $this->telegram = new Telegram(); } /** * Execute the job. * * @return void */ public function handle(ServiceVCenter $serviceVCenter) { try { // Load product and snapshot $product = ModuleVmware::find($this->productId); $snapshot = ModuleSnapshot::find($this->snapshotId); if (!$product || !$snapshot) { throw new \Exception("Không tìm thấy thông tin VPS hoặc snapshot"); } // Resolve vCenter using helper method $vcenter = $serviceVCenter->resolveVCenterFromProduct($product); if (!$vcenter) { throw new \Exception("VPS không có thông tin vCenter được cấu hình"); } // Set vCenter connection info $serviceVCenter->token = $vcenter->token; $serviceVCenter->apiUri = $vcenter->host; Log::info('🗑️ Bắt đầu xóa snapshot', [ 'product_id' => $product->id, 'snapshot_id' => $snapshot->id, 'snapshot_name' => $snapshot->name, 'vcenter_host' => $vcenter->host ]); // Attempt to remove snapshot $result = $serviceVCenter->RemoveSnapShot($product, $snapshot->snapshot_ref ?? $snapshot->name); // Handle re-login if needed if ($result === 'Login_Again') { Log::info('🔑 Cần đăng nhập lại vCenter'); $token = $serviceVCenter->loginServer($vcenter); if ($token) { $vcenter->update(['token' => $token]); $serviceVCenter->token = $token; $result = $serviceVCenter->RemoveSnapShot($product, $snapshot->snapshot_ref ?? $snapshot->name); } else { throw new \Exception("Không thể đăng nhập lại vào vCenter"); } } // Check for errors if (is_array($result) && isset($result['error']) && $result['error']) { $errorMessage = 'Lỗi xóa snapshot'; if (isset($result['message'])) { $errorMessage .= ': ' . (is_array($result['message']) ? json_encode($result['message']) : $result['message']); } throw new \Exception($errorMessage); } if (is_string($result) && $result !== 'Login_Again') { throw new \Exception("Lỗi xóa snapshot: {$result}"); } // Decrease snapshot limit counter ModuleSnapshotLimit::where('customer_id', $product->customer_id)->increment('used', -1); // Store snapshot name before deletion for notification $snapshotName = $snapshot->name; // Delete snapshot from database $snapshot->delete(); Log::info('✅ Xóa snapshot thành công', [ 'product_id' => $product->id, 'snapshot_name' => $snapshotName ]); // Send success notification Event(new NotificationStatus("Xóa snapshot thành công cho VPS {$product->name}", true)); $product->loadMissing('customer'); if ($product->customer?->email) { Mail::to($product->customer->email)->queue(new SnapshotActionMail([ 'action' => 'remove', 'fullname' => $product->customer->fullname, 'vm_name' => $product->name, 'snapshot_name' => $snapshotName, 'action_at' => now()->format('d/m/Y H:i:s'), ])); } // Send Telegram notification $this->telegram->SendMessage( "🗑️ SNAPSHOT REMOVED SUCCESSFULLY\n\n" . "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" . "🖥️ Server: `{$product->name}`\n" . "📸 Snapshot: `{$snapshotName}`\n" . "👤 Customer: `#{$product->customer_id}`\n" . "🆔 VM ID: `{$product->vmware_id}`\n" . "⏰ Removed: `" . now()->format('d/m/Y H:i:s') . "`\n" . "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" . "💡 Status: ✅ Completed\n" . "🔧 Action: Snapshot Removal\n" . "📊 Queue: snapshots" ); } catch (\Exception $e) { Log::error('❌ Lỗi xóa snapshot', [ 'product_id' => $this->productId, 'snapshot_id' => $this->snapshotId, 'error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine() ]); throw $e; // Re-throw để kích hoạt failed method } } public function failed(\Throwable $exception) { Log::error('❌ Job RemoveSnapshot failed', [ 'product_id' => $this->productId, 'snapshot_id' => $this->snapshotId, 'error' => $exception->getMessage(), 'trace' => $exception->getTraceAsString() ]); // Send failure notification Event(new NotificationStatus("Lỗi xóa snapshot: {$exception->getMessage()}", false)); // Send Telegram error notification $this->telegram->SendErrorMessage([ 'job_name' => 'RemoveSnapshot', 'product_id' => $this->productId, 'snapshot_id' => $this->snapshotId, 'error' => $exception->getMessage(), 'trace' => $exception->getTraceAsString() ]); } }