update develop
This commit is contained in:
Vendored
BIN
Binary file not shown.
@@ -19,153 +19,87 @@ class CreateSnapshot implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $productId;
|
||||
public function __construct(
|
||||
public int $snapshotId
|
||||
) {}
|
||||
|
||||
protected $telegram;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param int $productId
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($productId = 0)
|
||||
{
|
||||
$this->productId = $productId;
|
||||
$this->telegram = new Telegram();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(ServiceVCenter $serviceVCenter)
|
||||
{
|
||||
$snapshot = ModuleSnapshot::with('vm.ipAddress.host.datacenter.vCenter')
|
||||
->find($this->snapshotId);
|
||||
|
||||
if (!$snapshot) {
|
||||
return;
|
||||
}
|
||||
|
||||
$snapshot->update(['status' => 'processing']);
|
||||
|
||||
$product = $snapshot->vm;
|
||||
$vcenter = $product->ipAddress->host->datacenter->vCenter;
|
||||
|
||||
try {
|
||||
// Load product with necessary relationships
|
||||
$product = ModuleVmware::with('ipAddress.host.datacenter.vCenter')->find($this->productId);
|
||||
|
||||
if (!$product) {
|
||||
throw new \Exception("Không tìm thấy thông tin VPS với ID: {$this->productId}");
|
||||
}
|
||||
|
||||
// Kiểm tra cấu hình vCenter
|
||||
if (!$product->ipAddress?->host?->datacenter?->vCenter) {
|
||||
throw new \Exception("VPS không có thông tin vCenter được cấu hình");
|
||||
}
|
||||
|
||||
$vcenter = $product->ipAddress->host->datacenter->vCenter;
|
||||
|
||||
// Set vCenter connection info
|
||||
// Config vCenter
|
||||
$serviceVCenter->token = $vcenter->token;
|
||||
$serviceVCenter->apiUri = $vcenter->host;
|
||||
|
||||
Log::info('🔄 Bắt đầu tạo snapshot', [
|
||||
'product_id' => $product->id,
|
||||
'vmware_id' => $product->vmware_id,
|
||||
'vcenter_host' => $vcenter->host
|
||||
]);
|
||||
// Call vCenter
|
||||
$result = $serviceVCenter->CreateSnapShot($product);
|
||||
|
||||
// Attempt to create snapshot
|
||||
$snapshot = $serviceVCenter->CreateSnapShot($product);
|
||||
|
||||
// Handle re-login if needed
|
||||
if ($snapshot === 'Login_Again') {
|
||||
Log::info('🔑 Cần đăng nhập lại vCenter');
|
||||
if ($result === 'Login_Again') {
|
||||
$token = $serviceVCenter->loginServer($vcenter);
|
||||
if ($token) {
|
||||
$vcenter->update(['token' => $token]);
|
||||
$serviceVCenter->token = $token;
|
||||
$snapshot = $serviceVCenter->CreateSnapShot($product);
|
||||
} else {
|
||||
throw new \Exception("Không thể đăng nhập lại vào vCenter");
|
||||
if (!$token) {
|
||||
throw new \Exception('Không thể login lại vCenter');
|
||||
}
|
||||
|
||||
$vcenter->update(['token' => $token]);
|
||||
$serviceVCenter->token = $token;
|
||||
$result = $serviceVCenter->CreateSnapShot($product);
|
||||
}
|
||||
|
||||
// Check for errors
|
||||
if (is_array($snapshot) && isset($snapshot['error']) && $snapshot['error']) {
|
||||
$errorMessage = 'Lỗi tạo snapshot';
|
||||
if (isset($snapshot['message'])) {
|
||||
$errorMessage .= ': ' . (is_array($snapshot['message']) ? json_encode($snapshot['message']) : $snapshot['message']);
|
||||
}
|
||||
throw new \Exception($errorMessage);
|
||||
if (is_array($result) && ($result['error'] ?? false)) {
|
||||
throw new \Exception($result['message'] ?? 'Lỗi tạo snapshot');
|
||||
}
|
||||
|
||||
if (is_string($snapshot) && $snapshot !== 'Login_Again') {
|
||||
throw new \Exception("Lỗi tạo snapshot: {$snapshot}");
|
||||
}
|
||||
|
||||
// Get snapshot name from response
|
||||
$snapshotName = null;
|
||||
if (is_array($snapshot) && isset($snapshot['data'])) {
|
||||
$snapshotName = $snapshot['data'];
|
||||
} else {
|
||||
$snapshotName = 'snapshot_' . now()->format('Y_m_d_H_i_s');
|
||||
}
|
||||
|
||||
// Update snapshot limit counter
|
||||
ModuleSnapshotLimit::where('customer_id', $product->customer_id)->increment('used', 1);
|
||||
|
||||
// Save snapshot to database
|
||||
ModuleSnapshot::create([
|
||||
'name' => $snapshotName,
|
||||
'customer_id' => $product->customer_id,
|
||||
'module_vmware_id' => $product->id
|
||||
// Update DB SUCCESS
|
||||
$snapshot->update([
|
||||
'name' => $result['name'] ?? $snapshot->name,
|
||||
'status' => 'success',
|
||||
'snapshot_ref' => $result['snapshot_id'] ?? null,
|
||||
'vcenter_task_id' => $result['task_id'] ?? null,
|
||||
]);
|
||||
|
||||
Log::info('✅ Tạo snapshot thành công', [
|
||||
'product_id' => $product->id,
|
||||
'snapshot_name' => $snapshotName
|
||||
// Increment quota CHỈ khi success
|
||||
ModuleSnapshotLimit::where('customer_id', $snapshot->customer_id)
|
||||
->increment('used');
|
||||
|
||||
event(new NotificationStatus(
|
||||
"Snapshot {$snapshot->name} tạo thành công",
|
||||
true
|
||||
));
|
||||
} catch (\Throwable $e) {
|
||||
$snapshot->update([
|
||||
'status' => 'failed',
|
||||
'error_message' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
// Send success notification
|
||||
Event(new NotificationStatus("Khởi tạo snapshot thành công cho VPS {$product->name}", true));
|
||||
|
||||
// Send Telegram notification
|
||||
$this->telegram->SendMessage(
|
||||
"🎉 <b>SNAPSHOT CREATED SUCCESSFULLY</b>\n\n" .
|
||||
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" .
|
||||
"🖥️ <b>Server:</b> `{$product->name}`\n" .
|
||||
"📸 <b>Snapshot:</b> `{$snapshotName}`\n" .
|
||||
"👤 <b>Customer:</b> `#{$product->customer_id}`\n" .
|
||||
"🆔 <b>VM ID:</b> `{$product->vmware_id}`\n" .
|
||||
"⏰ <b>Created:</b> `" . now()->format('d/m/Y H:i:s') . "`\n" .
|
||||
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" .
|
||||
"💡 <b>Status:</b> ✅ Completed\n" .
|
||||
"🔧 <b>Action:</b> Snapshot Creation\n" .
|
||||
"📊 <b>Queue:</b> snapshots"
|
||||
);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('❌ Lỗi tạo snapshot', [
|
||||
'product_id' => $this->productId,
|
||||
'error' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine()
|
||||
]);
|
||||
|
||||
throw $e; // Re-throw để kích hoạt failed method
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function failed(\Throwable $exception)
|
||||
{
|
||||
Log::error('❌ Job CreateSnapshot failed', [
|
||||
'product_id' => $this->productId,
|
||||
'error' => $exception->getMessage(),
|
||||
'trace' => $exception->getTraceAsString()
|
||||
]);
|
||||
$snapshot = ModuleSnapshot::find($this->snapshotId);
|
||||
|
||||
// Send failure notification
|
||||
Event(new NotificationStatus("Lỗi tạo snapshot: {$exception->getMessage()}", false));
|
||||
if ($snapshot) {
|
||||
$snapshot->update([
|
||||
'status' => 'failed',
|
||||
'error_message' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
// Send Telegram error notification
|
||||
$this->telegram->SendErrorMessage([
|
||||
'job_name' => 'CreateSnapshot',
|
||||
'product_id' => $this->productId,
|
||||
'error' => $exception->getMessage(),
|
||||
'trace' => $exception->getTraceAsString()
|
||||
]);
|
||||
event(new NotificationStatus(
|
||||
"Snapshot thất bại: {$exception->getMessage()}",
|
||||
false
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user