feat: Implement UUID-based customer routing, enhance order request validation, and refactor VM creation job with updated payment logic and ModuleVmware handling.

This commit is contained in:
pbao9
2026-03-12 16:30:02 +07:00
parent 508d5186db
commit dbd3cdafcd
11 changed files with 207 additions and 283 deletions
@@ -1,115 +0,0 @@
<?php
namespace App\Jobs\VirtualMachine\Create;
use App\Http\Services\Telegram;
use App\Http\Services\vCenter as ServiceVCenter;
use App\Models\ModuleVmware;
use Carbon\Carbon;
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;
use Illuminate\Support\Str;
use Throwable;
class CreateVMJobs implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data = null;
protected $push = null;
protected $telegram = null;
public int $tries = 1;
public $message = "";
public $timeout = 0;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($product = 0)
{
$this->data = $product;
$this->telegram = new Telegram;
}
public function handle(ServiceVCenter $serviceVCenter)
{
$product = ModuleVmware::with([
'Order.OrderItem',
'Customer',
'IpAddress.Network',
'IpAddress.Host.OneDatastore',
'IpAddress.Host.Datacenter.vCenter','Os.OsGroup'
])->find($this->data);
$serviceVCenter->token = $product->IpAddress->Host->Datacenter->vCenter->token;
$serviceVCenter->apiUri = $product->IpAddress->Host->Datacenter->vCenter->host;
$CheckToken = $serviceVCenter->checkApiExits();
if (! $CheckToken)
{
$token = $serviceVCenter->loginServer($product->IpAddress->Host->Datacenter->vCenter);
if ($token)
{
$product->IpAddress->Host->Datacenter->vCenter->update(['token' => $token]);
$serviceVCenter->token = $token;
}
}
$name = $product->IpAddress->ip . '_' . str_replace('-','_',Str::slug($product->Customer->firstname));
$jobs_check = new GetVirtualMachine($name,$product,$serviceVCenter);
$vmware_id = $jobs_check->handle();
if ($vmware_id === false)
{
Log::channel('detail')->info("Bắt đầu tạo: " . time());
$vmware_id = $serviceVCenter->CreateVirtualMachine($product);
Log::channel('detail')->info("Kết thúc: " . time());
if (isset($vmware_id['error']) && $vmware_id['error'] === false) {
$vmware_id = $vmware_id['data'];
}
}
if ($vmware_id && is_string($vmware_id)) {
$product->update(['vmware_id' => $vmware_id]);
$jobs_GetStatus = new CheckStatusVirtualMachine($vmware_id,$product,$serviceVCenter);
dispatch($jobs_GetStatus)->delay(now()->addSeconds(30));
} else {
$this->message = "Notification: " . json_encode($vmware_id);
throw new \Exception(json_encode($vmware_id));
}
}
public function failed(Throwable $exception): void
{
Log::channel('Custom')->info("\r\n");
Log::channel('Custom')->info("=================================");
Log::channel('Custom')->info("Thông báo lỗi lúc: " .Carbon::now());
Log::channel('Custom')->info("Notification Create VirtualMachine: ");
Log::channel('Custom')->info("========");
Log::channel('Custom')->info($exception->getMessage());
Log::channel('Custom')->info("=================================");
$this->telegram->SendErrorMessage($exception->getMessage());
}
}
@@ -14,7 +14,6 @@ use App\Models\ModuleVmware;
use App\Models\ModuleIpAddress;
use App\Models\IpPool;
use App\Models\Log as CustomerLog;
use App\Models\Service;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -160,7 +159,7 @@ class CreateVirtualMachineJob implements ShouldQueue
'template' => $template,
'addon' => $addonData,
]);
Log::info('CreateVirtualMachineJob - Result: ' . json_encode($result));
if (is_array($result) && ($result['error'] ?? false)) {
$errorMessage = 'Tạo máy ảo thất bại: ' . json_encode($result);
Log::warning($errorMessage);
@@ -202,22 +201,14 @@ class CreateVirtualMachineJob implements ShouldQueue
if ($vmId !== 'unknown' && $vmId !== 'UNKNOWN') {
try {
// Lưu vm_id vào bảng invoice
$invoice = \App\Models\Invoice::where('order_id', $this->item->order->id)->first();
if ($invoice) {
$invoice->update(['vm_id' => $vmId]);
}
// Start VM ngay sau khi tạo thành công (sử dụng API trực tiếp)
// vm_id được lưu vào tbl.services qua ModuleVmwareObserver::created()
$startResult = $this->startVirtualMachineDirectly($vCenter, $vmId);
if (is_array($startResult) && $startResult['error'] === false) {
// Dispatch ConfigCLICommand để cấu hình VM và lấy password
$configJob = new ConfigCLICommand($vmId, $this->item, $vCenter);
dispatch($configJob)->delay(now()->addMinutes(2)); // Chờ 2 phút để VM boot hoàn toàn
}
// Lưu dữ liệu VM vào database
try {
$customer = $this->item->order->customer;
$serverName = $this->item->server_name ?? 'VM-' . $vmId;
@@ -236,42 +227,32 @@ class CreateVirtualMachineJob implements ShouldQueue
'status' => 1 // 1 = Assigned
]);
$months = (int) ($this->item->months ?? 0);
$firstPayment = (int) $this->item->price * ($months ?: 1);
$nextDueDate = $months > 0 ? now()->addMonths($months)->timestamp : null;
$moduleVmware = ModuleVmware::create([
'name' => $serverName,
'module_ip_address_id' => $moduleIpAddress->id,
'module_ip_address' => $moduleIpAddress->id,
'port' => 22,
'order_id' => $this->item->order->id,
'first_payment' => $this->item->order->first_payment ?? 0,
'next_payment' => $this->item->order->next_payment ?? 0,
'next_due_date' => $this->item->order->next_due_date ?? null,
'next_invoice' => $this->item->order->next_invoice ?? 0,
'user' => 'Administrator',
'password' => '',
'first_payment' => $firstPayment,
'next_payment' => $firstPayment,
'next_due_date' => $nextDueDate,
'next_invoice' => $nextDueDate,
'user' => 'administrator',
'password' => 'TGS@123',
'hostname' => $serverName,
'vmware_id' => $vmId,
'vcenter_id' => $vcenter->id,
'os_id' => $this->item->os_id ?? 1,
'customer_id' => $customer->id,
'status' => 1
'status' => 1,
]);
AssignIPJob::dispatch($vmId, $moduleVmware->id, $moduleIpAddress->ip)
->delay(now()->addMinutes(5));
// Lưu Service record để track dịch vụ của khách hàng
Service::create([
'customer_id' => $customer->id,
'product_id' => $this->item->product_id,
'order_item_id' => $this->item->id,
'invoice_id' => $invoice?->id,
'vm_id' => $vmId,
'server_name' => $serverName,
'status' => 'active',
'auto_renew' => false,
'started_at' => now(),
'expires_at' => $this->item->months > 0
? now()->addMonths($this->item->months)
: null,
]);
// Service record sẽ được tạo tự động bởi ModuleVmwareObserver::created()
} catch (\Exception $e) {
Log::error('❌ Lỗi khi lưu dữ liệu VM và IP address: ' . $e->getMessage(), [
'vmware_id' => $vmId,