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

82 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs\VirtualMachine\Rebuild;
use App\Services\VirtualMachine\Rebuild\VmRebuildAddonService;
use App\Services\VirtualMachine\Rebuild\VmRebuildService;
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;
/**
* RebuildAddonInstallJob
*
* Reapplies CPU / RAM / Disk addons attached to the existing service onto
* the new VM. Idempotent — Upgrade* primitives are no-ops when the spec
* already matches.
*/
final class RebuildAddonInstallJob 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,
VmRebuildAddonService $addonService,
): void {
Log::info('RebuildAddonInstallJob: start', [
'module_vmware_id' => $this->moduleVmwareId,
'new_vm_id' => $this->newVmId,
]);
$ctx = $rebuildService
->buildContext($this->moduleVmwareId, $this->newOsId, $this->newVmId);
$addonService->reinstall($ctx);
RebuildFinalizeJob::dispatch(
$this->moduleVmwareId,
$this->newOsId,
$this->newVmId,
$this->reinstallLogId,
)->onQueue('default');
Log::info('RebuildAddonInstallJob: completed, finalize dispatched', [
'module_vmware_id' => $this->moduleVmwareId,
'new_vm_id' => $this->newVmId,
]);
}
public function failed(Throwable $e): void
{
Log::error('RebuildAddonInstallJob: failed — old VM preserved', [
'module_vmware_id' => $this->moduleVmwareId,
'new_vm_id' => $this->newVmId,
'error' => $e->getMessage(),
]);
\App\Models\ReinstallLog::where('id', $this->reinstallLogId)->update([
'status' => \App\Models\ReinstallLog::STATUS_FAILED,
'error_message' => 'AddonInstall failed: ' . $e->getMessage(),
'updated_at' => now(),
]);
}
}