Files
portal-api/app/Jobs/VirtualMachine/Rebuild/RebuildFinalizeJob.php
T

193 lines
7.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs\VirtualMachine\Rebuild;
use App\Http\Services\Telegram;
use App\Enums\Product\ProductBillingCycle;
use App\Jobs\Mail\Admin\AdminOrderNotificationJob;
use App\Jobs\Mail\Customer\VmRebuildFailedJob;
use App\Jobs\Mail\SendInformationVirtualMachine;
use App\Models\Invoice;
use App\Models\ModuleVmware;
use App\Models\ReinstallLog;
use App\Models\Setting;
use App\Services\VirtualMachine\Rebuild\VmRebuildService;
use App\Services\VirtualMachine\Rebuild\VmRebuildSwapService;
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;
use Throwable;
/**
* RebuildFinalizeJob
*
* Final validation + DB swap + credential email. Only after this step is the
* old VM safe to delete.
*
* [1] Health check the new VM in vCenter.
* [2] Swap module_vmware.vmware_id (+ virtual_machines.vm_id) atomically.
* [3] Send credentials email + admin notification.
* [4] Dispatch RebuildDeleteOldVmJob — deletion happens last.
*
* If health check fails: nothing is swapped, old VM stays attached to the
* service, and the operator gets a failed-job alert.
*/
final class RebuildFinalizeJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 3;
public int $timeout = 300;
public int $backoff = 30;
public function __construct(
public readonly int $moduleVmwareId,
public readonly int $newOsId,
public readonly string $newVmId,
public readonly int $reinstallLogId,
) {
}
public function handle(
VmRebuildService $rebuildService,
VmRebuildSwapService $swapService,
): void {
Log::info('RebuildFinalizeJob: start', [
'module_vmware_id' => $this->moduleVmwareId,
'new_vm_id' => $this->newVmId,
]);
$ctx = $rebuildService
->buildContext($this->moduleVmwareId, $this->newOsId, $this->newVmId);
// [1] Health check before touching DB.
$swapService->healthCheck($ctx);
$oldVmId = $ctx->oldVmId;
// [2] Atomic DB swap.
$swapService->swapDatabaseReferences($ctx);
// [3] Notify customer with new credentials.
$this->sendCredentialsEmail($this->moduleVmwareId);
ReinstallLog::where('id', $this->reinstallLogId)->update([
'status' => ReinstallLog::STATUS_COMPLETED,
'new_vmware_id' => $ctx->newVmId,
'note' => 'Rebuild OK',
'completed_at' => now(),
'updated_at' => now(),
]);
// [4] Schedule old-VM deletion — last step, after everything is committed.
RebuildDeleteOldVmJob::dispatch(
$this->moduleVmwareId,
$oldVmId,
$this->reinstallLogId,
)->onQueue('default');
Log::info('RebuildFinalizeJob: completed', [
'module_vmware_id' => $this->moduleVmwareId,
'new_vm_id' => $this->newVmId,
'old_vm_id' => $oldVmId,
]);
(new Telegram)->SendRebuildStepNotification($ctx->vmName, $ctx->ip, 'Hoàn tất & chuyển đổi VM', true, 'Rebuild thành công, VM mới đã online.');
}
public function failed(Throwable $e): void
{
Log::error('RebuildFinalizeJob: failed — DB swap aborted, old VM remains attached', [
'module_vmware_id' => $this->moduleVmwareId,
'new_vm_id' => $this->newVmId,
'error' => $e->getMessage(),
]);
ReinstallLog::where('id', $this->reinstallLogId)->update([
'status' => ReinstallLog::STATUS_FAILED,
'error_message' => $e->getMessage(),
'updated_at' => now(),
]);
$vm = ModuleVmware::with('customer')->find($this->moduleVmwareId);
(new Telegram)->SendRebuildStepNotification(
$vm->name ?? "VM #{$this->moduleVmwareId}",
$vm->ip ?? '',
'Hoàn tất & chuyển đổi VM',
false,
$e->getMessage(),
);
VmRebuildFailedJob::dispatch([
'customer_email' => $vm?->customer?->email,
'customer_name' => $vm?->customer?->fullname,
'vm_name' => $vm->name ?? "VM #{$this->moduleVmwareId}",
'ip' => $vm->ip ?? '',
'module_vmware_id' => $this->moduleVmwareId,
]);
}
private function sendCredentialsEmail(int $moduleVmwareId): void
{
$vm = ModuleVmware::with(['customer', 'os.osGroup', 'order.orderItems.service'])
->find($moduleVmwareId);
if (!$vm || !$vm->customer?->email) {
Log::warning('RebuildFinalizeJob: skip credentials email — VM/customer missing', [
'module_vmware_id' => $moduleVmwareId,
]);
return;
}
$setting = Setting::where('key', 'setting')->first();
$settingValue = $setting ? json_decode((string) $setting->value) : null;
$orderItem = $vm->order?->orderItems?->first();
$months = (int) ($orderItem?->months ?? 0);
$osGroup = $vm->os?->osGroup?->name ?? '';
$isWindows = str_contains(strtolower($osGroup), 'windows');
$osUsername = $isWindows ? 'Administrator' : 'root';
$emailData = [
'company_name' => $settingValue->company ?? 'Thế Giới Số',
'company_logo' => $settingValue->logo ?? '',
'client_name' => $vm->customer->fullname ?? '',
'product_name' => $orderItem?->getProductAttribute()?->name ?? 'Cloud VPS',
'product_billing' => ProductBillingCycle::vietnameseLabelForMonths($months),
'product_price' => $orderItem ? number_format((float) $orderItem->price, 0, ',', '.') : 'N/A',
'service_host' => $vm->ip ?? 'N/A',
'service_username' => $osUsername,
'service_port' => (string) ($vm->port ?? '22'),
'service_password' => $vm->plain_password ?? '(xem tai khoan quan ly)',
'os_name' => $vm->os ? ($osGroup . ' ' . $vm->os->version) : 'Unknown OS',
'service_due_exp' => $vm->next_due_date
? date('d/m/Y', (int) $vm->next_due_date)
: 'N/A',
'is_rebuild' => true,
];
$invoice = Invoice::where('order_id', $vm->order_id)->first();
SendInformationVirtualMachine::dispatch($emailData, $vm->customer, $invoice?->id);
AdminOrderNotificationJob::dispatch([
'customer_name' => $vm->customer->fullname ?? '',
'customer_email' => $vm->customer->email,
'client_ip' => null,
'order_num' => $vm->order?->order_num ?? $vm->order?->id ?? 'N/A',
'product_name' => ($orderItem?->getProductAttribute()?->name ?? 'Cloud VPS') . ' (Rebuild)',
'product_type' => 'Cloud VPS Rebuild',
'billing_cycle' => ProductBillingCycle::vietnameseLabelForMonths($months),
'total' => $orderItem?->price ?? 0,
'created_at' => now()->format('d/m/Y H:i:s'),
'ip' => $vm->ip ?? '',
'username' => $osUsername,
'password' => $vm->plain_password ?? '',
]);
}
}