Files
portal-api/app/Jobs/VirtualMachine/Handle/WaitStatusVirtualMachine.php
T

115 lines
4.4 KiB
PHP

<?php
namespace App\Jobs\VirtualMachine\Handle;
use App\Events\VirtualMachine_Panel_Status;
use Illuminate\Bus\Queueable;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class WaitStatusVirtualMachine implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected string $vmware_id;
protected $serviceVCetner;
protected bool $status;
protected int $user;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($vmware_id,$service,$user,$status)
{
//
$this->vmware_id = $vmware_id;
$this->serviceVCetner = $service;
$this->user = $user;
$this->status = $status;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info("WaitStatusVirtualMachine: Started for VM {$this->vmware_id}, Target Status: " . ($this->status ? 'True' : 'False'));
$status = $this->serviceVCetner->CheckVirtualMachine($this->vmware_id);
if ($status['error'] === true) {
Log::error("WaitStatusVirtualMachine: Error checking status for VM {$this->vmware_id}: " . 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);
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;
}
Log::info("WaitStatusVirtualMachine: Loop check - operations_ready: " . ($status['data']['operations_ready'] ? 'True' : 'False'));
} while ($status['data']['operations_ready'] === $this->status);
} else {
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]));
// 2. Update DB Status and Dispatch Event
$product = \App\Models\ModuleVmware::where('vmware_id', $this->vmware_id)->first();
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");
$product->update(['status' => $newStatus]);
$msg = ($newStatus == 1)
? __('Máy ảo đã hoạt động trở lại.')
: __('Máy ảo đã tắt hoàn toàn.');
$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}");
}
// 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));
}
}