Files
baodev0andClaude Sonnet 5.5 62596d9063 feat(vm): hoàn tiền khi cấp VM lỗi, theo dõi job queue, uuid dịch vụ cho ticket, chuẩn hoá RAM addon
- VmProvisionException/VmProvisionRefund xử lý lỗi cấp phát VM
- QueueJobInspector + QueueMonitorController xem chi tiết job
- Thêm service uuid cho ticket, thời lượng cho activity_logs
- Migration chuẩn hoá RAM addon sang MB, trạng thái deleting cho snapshot

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 11:54:05 +07:00

120 lines
3.9 KiB
PHP

<?php
namespace App\Jobs\VirtualMachine\Action;
use App\Events\NotificationStatus;
use App\Http\Services\PusherNotification;
use App\Http\Services\Telegram;
use App\Http\Services\vCenter as ServiceVCenter;
use App\Models\ModuleVmware;
use App\Models\vCenter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Throwable;
class VMJobs implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 1;
public int $timeout = 600;
protected int $data;
protected $push = null;
protected $telegram = null;
protected $action = null;
protected $update = true;
public function __construct($product = 0, $action = '', $update = true)
{
$this->data = $product;
$this->push = new PusherNotification;
$this->telegram = new Telegram;
$this->action = $action;
$this->update = $update;
}
public function handle(ServiceVCenter $serviceVCenter)
{
$product = ModuleVmware::with([
'Customer',
'IpAddress.Host.Datacenter.vCenter'
])->find($this->data);
if (!$product || !$product->IpAddress?->Host?->Datacenter?->vCenter) {
throw new \Exception("VMJobs: product {$this->data} missing vCenter relationship");
}
$vcenter = $product->IpAddress->Host->Datacenter->vCenter;
$serviceVCenter->setConnection($vcenter->host, $vcenter->token);
$vmwareId = $product->vmware_id;
$VirtualMachineStatus = null;
$status = 1;
$call = function (callable $action) use ($serviceVCenter, $vcenter) {
$result = $action();
if ($result === 'Login_Again') {
$token = $serviceVCenter->loginServer($vcenter);
if ($token) {
vCenter::where('id', $vcenter->id)->update(['token' => $token]);
$serviceVCenter->token = $token;
}
$result = $action();
}
return $result;
};
$powerState = fn () => $call(fn () => $serviceVCenter->CheckVirtualMachine($vmwareId))['power_state'] ?? null;
switch ($this->action) {
case 'Suspend':
$VirtualMachineStatus = $call(fn () => $serviceVCenter->SuspendVirtualMachine($vmwareId));
$status = 2;
break;
case 'Reboot':
$VirtualMachineStatus = $call(fn () => $serviceVCenter->RebootVirtualMachine($product, $vmwareId));
break;
case 'Start':
if ($powerState() !== 'POWERED_ON') {
$VirtualMachineStatus = $call(fn () => $serviceVCenter->StartVirtualMachine($product, $vmwareId));
}
break;
case 'ShutdownGuest':
$status = 0;
if ($powerState() !== 'POWERED_OFF') {
$VirtualMachineStatus = $call(fn () => $serviceVCenter->ShutdownGuestVirtualMachine($product, $vmwareId));
}
break;
case 'PowerOff':
$status = 0;
if ($powerState() !== 'POWERED_OFF') {
$VirtualMachineStatus = $call(fn () => $serviceVCenter->PowerOffVirtualMachine($vmwareId));
}
break;
}
if (is_array($VirtualMachineStatus) && ($VirtualMachineStatus['error'] ?? false)) {
throw new \Exception(json_encode($VirtualMachineStatus['data'] ?? $VirtualMachineStatus));
}
if ($this->update) {
$product->update(['status' => $status]);
}
}
public function failed(Throwable $exception)
{
Event(new NotificationStatus("Notification: " . $exception->getMessage(), false));
$this->telegram->SendErrorMessage($exception->getMessage());
}
}