87 lines
3.0 KiB
PHP
87 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Power;
|
|
|
|
use App\Http\Services\vCenter;
|
|
use App\Jobs\VirtualMachine\Handle\WaitStatusVirtualMachine;
|
|
use App\Models\ModuleVmware;
|
|
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;
|
|
|
|
class RebootVmJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public $timeout = 120;
|
|
public $tries = 3;
|
|
|
|
protected $productId;
|
|
protected $userId;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct($productId, $userId)
|
|
{
|
|
$this->productId = $productId;
|
|
$this->userId = $userId;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(vCenter $vCenterService)
|
|
{
|
|
Log::info("Job RebootVmJob started for Product ID: {$this->productId}");
|
|
|
|
$product = ModuleVmware::with('ipAddress.host.datacenter.vCenter')->find($this->productId);
|
|
|
|
if (!$product) {
|
|
Log::error("RebootVmJob: Product not found ID: {$this->productId}");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$result = $vCenterService->RebootVirtualMachine($product, $product->vmware_id);
|
|
|
|
if ($result === 'Login_Again') {
|
|
$vCenterService->loginServer($product->ipAddress->host->datacenter->vCenter);
|
|
$result = $vCenterService->RebootVirtualMachine($product, $product->vmware_id);
|
|
}
|
|
|
|
if (is_array($result) && isset($result['error']) && $result['error'] === false) {
|
|
Log::info("RebootVmJob: Command sent successfully for VM {$product->vmware_id}");
|
|
|
|
// Clear VM info cache
|
|
\Illuminate\Support\Facades\Cache::forget(\App\Support\Cache\CacheKey::build('vm', 'info', ['id' => $product->vmware_id]));
|
|
|
|
// 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);
|
|
|
|
dispatch(new WaitStatusVirtualMachine($this->productId, false))->delay(now()->addSeconds(5));
|
|
|
|
} else {
|
|
$msg = is_array($result) ? ($result['data'] ?? 'Unknown error') : $result;
|
|
Log::error("RebootVmJob: Failed for VM {$product->vmware_id}. Error: {$msg}");
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error("RebootVmJob: Exception for VM {$product->vmware_id}: " . $e->getMessage());
|
|
$this->fail($e);
|
|
}
|
|
}
|
|
}
|