157 lines
5.5 KiB
PHP
157 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Sync;
|
|
|
|
use App\Http\Services\vCenter;
|
|
use App\Models\Invoice;
|
|
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\Cache;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class UpdateVMInfoJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 3;
|
|
public int $timeout = 30;
|
|
public int $backoff = 60; // Wait 1 minute between retries
|
|
|
|
protected int $invoiceId;
|
|
protected string $vmId;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct(int $invoiceId, string $vmId)
|
|
{
|
|
$this->invoiceId = $invoiceId;
|
|
$this->vmId = $vmId;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(vCenter $vCenterService): void
|
|
{
|
|
try {
|
|
$invoice = Invoice::with([
|
|
'invoiceItem.os.osGroup',
|
|
'invoiceItem.addons'
|
|
])->find($this->invoiceId);
|
|
|
|
if (!$invoice) {
|
|
Log::warning('Invoice not found for VM info update', ['invoice_id' => $this->invoiceId]);
|
|
return;
|
|
}
|
|
|
|
$orderItem = $invoice->invoiceItem->first();
|
|
if (!$orderItem) {
|
|
Log::warning('Order item not found for invoice', ['invoice_id' => $this->invoiceId]);
|
|
return;
|
|
}
|
|
|
|
$product = $orderItem->getProductAttribute();
|
|
if (!$product) {
|
|
Log::warning('Product not found for order item', ['order_item_id' => $orderItem->id]);
|
|
return;
|
|
}
|
|
|
|
$host = $product->host;
|
|
$datacenter = $host?->datacenter;
|
|
$vcenter = $datacenter?->vcenter;
|
|
|
|
if (!$vcenter) {
|
|
Log::warning('vCenter not found for VM', ['vm_id' => $this->vmId]);
|
|
return;
|
|
}
|
|
|
|
// Login to vCenter
|
|
if (!$vCenterService->loginServer($vcenter)) {
|
|
Log::warning('Failed to login to vCenter', ['vm_id' => $this->vmId]);
|
|
return;
|
|
}
|
|
|
|
// Get VM details
|
|
$vmDetail = $vCenterService->GetDetailVirtualMachine($this->vmId);
|
|
if ($vmDetail['error'] === true) {
|
|
Log::warning('Failed to get VM details', ['vm_id' => $this->vmId, 'error' => $vmDetail['message'] ?? 'Unknown error']);
|
|
return;
|
|
}
|
|
|
|
$vmData = $vmDetail['data'];
|
|
|
|
// Get IP information
|
|
$ipInfo = $vCenterService->GetIPAddressVirtualMachine($this->vmId);
|
|
$ipAddress = 'N/A';
|
|
if ($ipInfo['error'] === false && !empty($ipInfo['data']['ip_addresses'])) {
|
|
$ipAddress = $ipInfo['data']['ip_addresses'][0] ?? 'N/A';
|
|
}
|
|
|
|
// Calculate hardware information
|
|
$cpuCount = $vmData['cpu']['count'] ?? 1;
|
|
$memoryMB = $vmData['memory']['size_MiB'] ?? 1024;
|
|
$memoryGB = round($memoryMB / 1024, 1);
|
|
|
|
$diskSizeGB = 0;
|
|
if (isset($vmData['disks']) && is_array($vmData['disks'])) {
|
|
foreach ($vmData['disks'] as $disk) {
|
|
$diskSizeGB += round(($disk['value']['capacity'] ?? 0) / (1024 * 1024 * 1024), 1);
|
|
}
|
|
}
|
|
|
|
$vmInfo = [
|
|
'vm_id' => $this->vmId,
|
|
'status' => $vmData['power_state'],
|
|
'ip_address' => $ipAddress,
|
|
'hostname' => $orderItem->server_name ?? 'Unknown',
|
|
'os' => $orderItem->os->osGroup->name ?? 'Unknown',
|
|
'cpu' => $cpuCount . ' cores',
|
|
'ram' => $memoryGB . ' GB',
|
|
'disk' => $diskSizeGB . ' GB',
|
|
'uptime' => 'N/A',
|
|
'last_updated' => now()->format('Y-m-d H:i:s'),
|
|
'vm_name' => $vmData['name'] ?? 'Unknown',
|
|
'vm_folder' => $vmData['folder'] ?? 'Unknown',
|
|
'vm_resource_pool' => $vmData['resource_pool'] ?? 'Unknown',
|
|
'vm_datastore' => $vmData['datastore'] ?? 'Unknown',
|
|
'vm_network' => $vmData['nics'][0]['backing']['network'] ?? 'Unknown',
|
|
'vm_mac' => $vmData['nics'][0]['mac_address'] ?? 'N/A',
|
|
'vm_guest_id' => $vmData['guest_ID'] ?? 'Unknown',
|
|
'vm_version' => $vmData['version'] ?? 'Unknown',
|
|
'vm_hardware_version' => $vmData['hardware']['version'] ?? 'Unknown',
|
|
];
|
|
|
|
// Cache the result for 5 minutes
|
|
$cacheKey = \App\Support\Cache\CacheKey::build('vm', 'info', ['id' => $this->vmId]);
|
|
Cache::put($cacheKey, $vmInfo, 300);
|
|
|
|
Log::info('VM info updated successfully', ['vm_id' => $this->vmId]);
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error('Error updating VM info', [
|
|
'vm_id' => $this->vmId,
|
|
'invoice_id' => $this->invoiceId,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
|
|
// Don't throw exception to prevent job from failing completely
|
|
// The job will be retried automatically
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle a job failure.
|
|
*/
|
|
public function failed(\Throwable $exception): void
|
|
{
|
|
Log::error('UpdateVMInfoJob failed', [
|
|
'vm_id' => $this->vmId,
|
|
'invoice_id' => $this->invoiceId,
|
|
'error' => $exception->getMessage()
|
|
]);
|
|
}
|
|
}
|