ipdate
This commit is contained in:
@@ -9,6 +9,7 @@ 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
|
||||
{
|
||||
@@ -39,18 +40,75 @@ class WaitStatusVirtualMachine implements ShouldQueue
|
||||
*/
|
||||
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.");
|
||||
}
|
||||
|
||||
Event(new VirtualMachine_Panel_Status('Yêu cầu được xử lý thành công',$this->user));
|
||||
// Action Completed
|
||||
// 1. Clear Cache again (to ensure next fetch gets new state)
|
||||
\Illuminate\Support\Facades\Cache::forget('vm_info_' . $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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +59,15 @@ class RebootVmJob implements ShouldQueue
|
||||
// Clear VM info cache
|
||||
\Illuminate\Support\Facades\Cache::forget('vm_info_' . $product->vmware_id);
|
||||
|
||||
$product->update(['status' => 3]); // Processing
|
||||
// Dispatch Event to Frontend
|
||||
\App\Events\VirtualMachineStatusChanged::dispatch(
|
||||
$this->userId,
|
||||
$product->vmware_id,
|
||||
'rebooted',
|
||||
__('Máy ảo đã được khởi động lại thành công.')
|
||||
);
|
||||
|
||||
$product->update(['status' => 3]); // Processing
|
||||
|
||||
// In VMJobs: $jobs = new WaitStatusVirtualMachine($product->vmware_id,$serviceVCenter,$product->customer_id,false);
|
||||
|
||||
|
||||
@@ -60,8 +60,16 @@ class StartVmJob implements ShouldQueue
|
||||
|
||||
// Clear VM info cache
|
||||
\Illuminate\Support\Facades\Cache::forget('vm_info_' . $product->vmware_id);
|
||||
|
||||
// Update status to 'processing' (e.g., 3) if needed, or leave it to WaitStatus
|
||||
|
||||
// Dispatch Event to Frontend
|
||||
\App\Events\VirtualMachineStatusChanged::dispatch(
|
||||
$this->userId,
|
||||
$product->vmware_id,
|
||||
'running',
|
||||
__('Máy ảo đã được khởi động thành công.')
|
||||
);
|
||||
|
||||
// Update status to 'processing' (e.g., 3) if needed, or leave it to WaitStatus
|
||||
$product->update(['status' => 3]); // 3 = Processing/Pending
|
||||
|
||||
// Dispatch wait status job
|
||||
|
||||
@@ -59,7 +59,15 @@ class StopVmJob implements ShouldQueue
|
||||
// Clear VM info cache
|
||||
\Illuminate\Support\Facades\Cache::forget('vm_info_' . $product->vmware_id);
|
||||
|
||||
$product->update(['status' => 3]); // Processing
|
||||
// Dispatch Event to Frontend
|
||||
\App\Events\VirtualMachineStatusChanged::dispatch(
|
||||
$this->userId,
|
||||
$product->vmware_id,
|
||||
'stopped',
|
||||
__('Máy ảo đã được tắt thành công.')
|
||||
);
|
||||
|
||||
$product->update(['status' => 3]); // Processing
|
||||
|
||||
dispatch(new WaitStatusVirtualMachine(
|
||||
$product->vmware_id,
|
||||
|
||||
Reference in New Issue
Block a user