feat(vm): hoàn tiền khi cấp VM lỗi, theo dõi job queue, uuid dịch vụ cho ticket, chuẩn hoá RAM addon

- VmProvisionException/VmProvisionRefund xử lý lỗi cấp phát VM
- QueueJobInspector + QueueMonitorController xem chi tiết job
- Thêm service uuid cho ticket, thời lượng cho activity_logs
- Migration chuẩn hoá RAM addon sang MB, trạng thái deleting cho snapshot

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
baodev0
2026-10-30 11:54:05 +07:00
co-authored by Claude Sonnet 5.5
parent 7136bec265
commit 62596d9063
32 changed files with 514 additions and 107 deletions
@@ -14,8 +14,8 @@ use RuntimeException;
*
* active vCenter → active Datacenter → active Host → active Datastore + Network
*
* Product/OS host binding is informational only — it no longer drives VM
* creation. Datastore, network, resource pool and folder are exceptions: if
* The OS host (and its datacenter) overrides the active host when set, because the
* OS template, datastore, network, resource pool and folder belong to that host. Datastore, network, resource pool and folder are exceptions: if
* the OS has datastore_id / module_network_id / resource_pool_id / folder_id
* set, they override the active chain / global default (an OS can target a
* different datastore/IP pool/resource pool/folder than whatever happens to
@@ -32,7 +32,7 @@ class VmInfrastructureResolver
public function resolve(OrderItem $item, $product): VmInfrastructureContext
{
$os = Os::with(['osGroup', 'network', 'datastore', 'resourcePool', 'folder'])->find($item->os_id);
$os = Os::with(['osGroup', 'host.datacenter', 'network', 'datastore', 'resourcePool', 'folder'])->find($item->os_id);
if (!$os) {
throw new RuntimeException(
@@ -41,6 +41,8 @@ class VmInfrastructureResolver
}
$chain = $this->activeProvider->resolveChain();
$host = $os->host ?? $chain['host'];
$datacenter = $os->host?->datacenter ?? $chain['datacenter'];
$network = $os->network ?? $chain['network'];
$datastore = $os->datastore ?? $chain['datastore'];
$folderId = $os->folder?->folder ?? $this->providerSettings->defaultFolder();
@@ -50,7 +52,8 @@ class VmInfrastructureResolver
'order_item_id' => $item->id,
'vcenter_id' => $chain['vcenter']->id,
'datacenter_id' => $chain['datacenter']->id,
'host_id' => $chain['host']->id,
'host_id' => $host->id,
'host_from_os' => $os->host !== null,
'datastore_id' => $datastore->id,
'datastore_from_os' => $os->datastore !== null,
'network_id' => $network->id,
@@ -66,8 +69,8 @@ class VmInfrastructureResolver
return new VmInfrastructureContext(
os: $os,
host: $chain['host'],
datacenter: $chain['datacenter'],
host: $host,
datacenter: $datacenter,
vcenter: $chain['vcenter'],
template: $os->template_id,
isWindows: $isWindows,
@@ -18,6 +18,11 @@ use Illuminate\Support\Facades\Log;
*/
class VmIpReservationService
{
public function hasFreeIp(): bool
{
return $this->availableIps()->exists();
}
/**
* @return array{reservation:ModuleIpAddress,ip_pool_id:int,gateway:string,cidr:int}|null
*/
@@ -25,8 +30,7 @@ class VmIpReservationService
{
return DB::transaction(function () use ($networkId, $hostId, $dns) {
// ip_pools.status is the single source of truth for availability.
$ipPool = IpPool::where('module_network_id', $networkId)
->whereIn('status', ['free', 'reserved'])
$ipPool = $this->availableIps()
->lockForUpdate()
->inRandomOrder()
->first();
@@ -44,7 +48,6 @@ class VmIpReservationService
// module_ip_address mirrors ip_pools — update the existing synced row
// (created when the IP was added to ip_pools) instead of inserting a new one.
$reservation = ModuleIpAddress::where('ip', $ipPool->ip_address)
->where('module_network_id', $networkId)
->lockForUpdate()
->first();
@@ -101,6 +104,13 @@ class VmIpReservationService
});
}
private function availableIps()
{
return IpPool::whereIn('status', ['free', 'reserved'])
->whereNotNull('gateway')
->where('gateway', '!=', '');
}
public function freeIpsInNetwork(int $networkId)
{
return IpPool::where('module_network_id', $networkId)
@@ -15,7 +15,7 @@ class VmModuleService
* Create ModuleVmware shell. IP and final password are filled in later
* by AssignIpToGuestJob → ChangePasswordViaVsphereJob.
*/
public function create(OrderItem $item, VmInfrastructureContext $infra, string $vmId): ModuleVmware
public function create(OrderItem $item, VmInfrastructureContext $infra, string $vmId, array $specs = []): ModuleVmware
{
$customer = $item->order->customer;
$serverName = $item->server_name ?? ('VM-' . $vmId);
@@ -43,6 +43,9 @@ class VmModuleService
'os_id' => $item->os_id ?? 1,
'customer_id' => $customer->id,
'status' => 1,
'cpu' => $specs['cpu'] ?? null,
'ram_mb' => $specs['ram_mb'] ?? null,
'disk_gb' => $specs['disk_gb'] ?? null,
]);
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Services\VirtualMachine;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Models\OrderItem;
use App\Services\InvoicePaymentService;
use Illuminate\Support\Facades\Log;
use Throwable;
class VmProvisionRefund
{
public function __construct(private readonly InvoicePaymentService $payments)
{
}
public function refund(OrderItem $item, string $reason): bool
{
$order = $item->order;
$invoice = $order?->invoice;
if (!$invoice || (int) $invoice->status !== InvoiceStatus::PAID->value || $order->orderItems()->count() !== 1) {
return false;
}
try {
$this->payments->refund($invoice, null, null, 'Tự động hoàn tiền do khởi tạo máy ảo thất bại: ' . $reason);
$order->update(['status' => OrderStatus::CANCELLED->value]);
return true;
} catch (Throwable $e) {
Log::error('VmProvisionRefund failed', ['order_item_id' => $item->id, 'error' => $e->getMessage()]);
return false;
}
}
}
@@ -14,6 +14,18 @@ class VmVcenterErrorTranslator
'TIMED_OUT' => 'Yêu cầu khởi tạo máy chủ hết thời gian chờ. Vui lòng thử lại sau.',
];
public function detail(array $result): string
{
$data = $result['data'] ?? [];
$messages = is_array($data) ? collect($data['messages'] ?? [])->pluck('default_message')->filter() : collect();
if ($messages->isNotEmpty()) {
return ($data['error_type'] ?? 'vCenter') . ': ' . $messages->implode(' | ');
}
return is_string($data) ? $data : (string) json_encode($data, JSON_UNESCAPED_UNICODE);
}
public function translate(array $result, ?string $serverName = null): string
{
$data = $result['data'] ?? [];