Implement VM provision locking mechanism and enhance VM job handling

This commit is contained in:
baodev0
2026-07-13 11:23:53 +07:00
parent 0976e23d1a
commit 8d3410f8cc
11 changed files with 143 additions and 131 deletions
@@ -3,6 +3,8 @@
namespace App\Jobs\VirtualMachine\Handle;
use App\Events\VirtualMachine_Panel_Status;
use App\Http\Services\vCenter as ServiceVCenter;
use App\Models\ModuleVmware;
use Illuminate\Bus\Queueable;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -15,54 +17,49 @@ class WaitStatusVirtualMachine implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected string $vmware_id;
protected $serviceVCetner;
protected int $productId;
protected bool $status;
protected int $user;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($vmware_id,$service,$user,$status)
public function __construct(int $productId, bool $status)
{
//
$this->vmware_id = $vmware_id;
$this->serviceVCetner = $service;
$this->user = $user;
$this->productId = $productId;
$this->status = $status;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
public function handle(ServiceVCenter $serviceVCenter)
{
Log::info("WaitStatusVirtualMachine: Started for VM {$this->vmware_id}, Target Status: " . ($this->status ? 'True' : 'False'));
$product = ModuleVmware::with(['IpAddress.Host.Datacenter.vCenter'])->find($this->productId);
$status = $this->serviceVCetner->CheckVirtualMachine($this->vmware_id);
if (!$product || !$product->IpAddress?->Host?->Datacenter?->vCenter) {
Log::error("WaitStatusVirtualMachine: Product {$this->productId} missing vCenter relationship");
return;
}
$vcenter = $product->IpAddress->Host->Datacenter->vCenter;
$serviceVCenter->setConnection($vcenter->host, $vcenter->token);
$vmwareId = $product->vmware_id;
$userId = $product->customer_id;
Log::info("WaitStatusVirtualMachine: Started for VM {$vmwareId}, Target Status: " . ($this->status ? 'True' : 'False'));
$status = $serviceVCenter->CheckVirtualMachine($vmwareId);
if ($status['error'] === true) {
Log::error("WaitStatusVirtualMachine: Error checking status for VM {$this->vmware_id}: " . json_encode($status['message']));
Log::error("WaitStatusVirtualMachine: Error checking status for VM {$vmwareId}: " . json_encode($status['message']));
throw new \Exception(json_encode($status['message']));
}
Log::info("WaitStatusVirtualMachine: Initial operations_ready: " . ($status['data']['operations_ready'] ? 'True' : 'False'));
// Wait loop: "Wait while operations_ready matches $this->status"
// Example: Stop Job passes true. We wait while it is true. When it becomes false (Off), we exit.
if ($status['data']['operations_ready'] === $this->status) {
Log::info("WaitStatusVirtualMachine: Entering wait loop...");
do {
sleep(10);
$status = $this->serviceVCetner->CheckVirtualMachine($this->vmware_id);
$status = $serviceVCenter->CheckVirtualMachine($vmwareId);
if ($status['error'] === true) {
Log::warning("WaitStatusVirtualMachine: Failed to check status in loop. Breaking.");
// If check fails, maybe network glitch? We can break or continue.
// Let's break to avoid infinite loop on error
break;
}
@@ -73,17 +70,11 @@ class WaitStatusVirtualMachine implements ShouldQueue
Log::info("WaitStatusVirtualMachine: Skipping wait loop, condition already met.");
}
// Action Completed
// 1. Clear Cache again (to ensure next fetch gets new state)
\Illuminate\Support\Facades\Cache::forget(\App\Support\Cache\CacheKey::build('vm', 'info', ['id' => $this->vmware_id]));
\Illuminate\Support\Facades\Cache::forget(\App\Support\Cache\CacheKey::build('vm', 'info', ['id' => $vmwareId]));
// 2. Update DB Status and Dispatch Event
$product = \App\Models\ModuleVmware::where('vmware_id', $this->vmware_id)->first();
$product->refresh();
if ($product) {
Log::info("WaitStatusVirtualMachine: Product found. Updating status...");
// If waiting for True (On) to become False (Off) -> New Status 0 (Off)
// If waiting for False (Off) to become True (On) -> New Status 1 (Active)
$newStatus = ($this->status === true) ? 0 : 1;
Log::info("WaitStatusVirtualMachine: Setting status to $newStatus");
@@ -96,19 +87,9 @@ class WaitStatusVirtualMachine implements ShouldQueue
$statusStr = ($newStatus == 1) ? 'running' : 'stopped';
Log::info("WaitStatusVirtualMachine: Dispatching VirtualMachineStatusChanged event.");
\App\Events\VirtualMachineStatusChanged::dispatch(
$this->user,
$this->vmware_id,
$statusStr,
$msg
);
} else {
Log::error("WaitStatusVirtualMachine: Product not found for vmware_id: {$this->vmware_id}");
\App\Events\VirtualMachineStatusChanged::dispatch($userId, $vmwareId, $statusStr, $msg);
}
// Legacy event (optional, keep to avoid breaking other things if they listen to it)
Event(new VirtualMachine_Panel_Status('Yêu cầu được xử lý thành công', $this->user));
Event(new VirtualMachine_Panel_Status('Yêu cầu được xử lý thành công', $userId));
}
}