96 lines
3.5 KiB
PHP
96 lines
3.5 KiB
PHP
<?php
|
|
|
|
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;
|
|
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 int $productId;
|
|
protected bool $status;
|
|
|
|
public function __construct(int $productId, bool $status)
|
|
{
|
|
$this->productId = $productId;
|
|
$this->status = $status;
|
|
}
|
|
|
|
public function handle(ServiceVCenter $serviceVCenter)
|
|
{
|
|
$product = ModuleVmware::with(['IpAddress.Host.Datacenter.vCenter'])->find($this->productId);
|
|
|
|
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 {$vmwareId}: " . json_encode($status['message']));
|
|
throw new \Exception(json_encode($status['message']));
|
|
}
|
|
|
|
Log::info("WaitStatusVirtualMachine: Initial operations_ready: " . ($status['data']['operations_ready'] ? 'True' : 'False'));
|
|
|
|
if ($status['data']['operations_ready'] === $this->status) {
|
|
Log::info("WaitStatusVirtualMachine: Entering wait loop...");
|
|
do {
|
|
sleep(10);
|
|
$status = $serviceVCenter->CheckVirtualMachine($vmwareId);
|
|
|
|
if ($status['error'] === true) {
|
|
Log::warning("WaitStatusVirtualMachine: Failed to check status in loop. Breaking.");
|
|
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.");
|
|
}
|
|
|
|
\Illuminate\Support\Facades\Cache::forget(\App\Support\Cache\CacheKey::build('vm', 'info', ['id' => $vmwareId]));
|
|
|
|
$product->refresh();
|
|
|
|
if ($product) {
|
|
$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';
|
|
|
|
\App\Events\VirtualMachineStatusChanged::dispatch($userId, $vmwareId, $statusStr, $msg);
|
|
}
|
|
|
|
Event(new VirtualMachine_Panel_Status('Yêu cầu được xử lý thành công', $userId));
|
|
}
|
|
}
|