Files
portal-api/app/Jobs/VirtualMachine/Snapshot/CreateSnapshot.php
T
pbao9 bb73d6c5a2 Refactor vCenter snapshot handling and VM operations
- Introduced a new method `resolveVCenterFromProduct` in `SnapshotService` to streamline vCenter resolution from product data.
- Updated `CreateSnapshot`, `RemoveSnapshot`, and `RevertSnapshot` jobs to utilize the new vCenter resolution method.
- Enhanced the `VmController` to support UUID-based service identification for VM operations.
- Added new actions for changing VM IP and RDP port, encapsulating the logic for these operations.
- Improved error handling and logging for VM operations, including detailed responses for API consumers.
- Removed unnecessary relationships in model queries to optimize performance.
2026-04-15 23:38:38 +07:00

115 lines
3.4 KiB
PHP

<?php
namespace App\Jobs\VirtualMachine\Snapshot;
use App\Events\NotificationStatus;
use App\Http\Services\Telegram;
use App\Http\Services\vCenter as ServiceVCenter;
use App\Models\ModuleSnapshot;
use App\Models\ModuleSnapshotLimit;
use App\Models\ModuleVmware;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class CreateSnapshot implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
public int $snapshotId
) {}
public function handle(ServiceVCenter $serviceVCenter)
{
$snapshot = ModuleSnapshot::with('vm')
->find($this->snapshotId);
if (!$snapshot) {
Log::error('Snapshot not found', ['snapshot_id' => $this->snapshotId]);
return;
}
$snapshot->update(['status' => 'processing']);
$product = $snapshot->vm;
// Resolve vCenter using helper method
$vcenter = $serviceVCenter->resolveVCenterFromProduct($product);
if (!$vcenter) {
$snapshot->update([
'status' => 'failed',
'error_message' => 'Không tìm thấy thông tin vCenter',
]);
throw new \Exception('Không tìm thấy thông tin vCenter cho VM');
}
try {
// Config vCenter
$serviceVCenter->token = $vcenter->token;
$serviceVCenter->apiUri = $vcenter->host;
// Call vCenter with snapshot name from DB
$result = $serviceVCenter->CreateSnapShot($product, $snapshot->name);
if ($result === 'Login_Again') {
$token = $serviceVCenter->loginServer($vcenter);
if (!$token) {
throw new \Exception('Không thể login lại vCenter');
}
$vcenter->update(['token' => $token]);
$serviceVCenter->token = $token;
$result = $serviceVCenter->CreateSnapShot($product, $snapshot->name);
}
if (is_array($result) && ($result['error'] ?? false)) {
throw new \Exception($result['message'] ?? 'Lỗi tạo snapshot');
}
// Update DB SUCCESS
$snapshot->update([
'status' => 'success',
'snapshot_ref' => $result['data'] ?? $snapshot->name,
]);
// Increment quota CHỈ khi success
ModuleSnapshotLimit::where('customer_id', $snapshot->customer_id)
->increment('used');
event(new NotificationStatus(
"Snapshot {$snapshot->name} tạo thành công",
true
));
} catch (\Throwable $e) {
$snapshot->update([
'status' => 'failed',
'error_message' => $e->getMessage(),
]);
throw $e;
}
}
public function failed(\Throwable $exception)
{
$snapshot = ModuleSnapshot::find($this->snapshotId);
if ($snapshot) {
$snapshot->update([
'status' => 'failed',
'error_message' => $exception->getMessage(),
]);
}
event(new NotificationStatus(
"Snapshot thất bại: {$exception->getMessage()}",
false
));
}
}