telegram = new Telegram();
}
public function handle(ServiceVCenter $serviceVCenter)
{
$snapshot = ModuleSnapshot::with('vm')
->find($this->snapshotId);
if (!$snapshot) {
Log::error('Snapshot not found', ['snapshot_id' => $this->snapshotId]);
return;
}
$snapshot->update(['status' => 'processing']);
$product = $snapshot->vm;
$vcenter = $serviceVCenter->resolveVCenterFromProduct($product);
if (!$vcenter) {
$snapshot->update([
'status' => 'failed',
'error_message' => 'Không tìm thấy thông tin vCenter',
]);
throw new \Exception('Không tìm thấy thông tin vCenter cho VM');
}
try {
$serviceVCenter->token = $vcenter->token;
$serviceVCenter->apiUri = $vcenter->host;
$result = $serviceVCenter->CreateSnapShot($product, $snapshot->name);
if ($result === 'Login_Again') {
$token = $serviceVCenter->loginServer($vcenter);
if (!$token) {
throw new \Exception('Không thể login lại vCenter');
}
$vcenter->update(['token' => $token]);
$serviceVCenter->token = $token;
$result = $serviceVCenter->CreateSnapShot($product, $snapshot->name);
}
if (is_array($result) && ($result['error'] ?? false)) {
throw new \Exception($result['message'] ?? 'Lỗi tạo snapshot');
}
$snapshot->update([
'status' => 'success',
'snapshot_ref' => $result['data'] ?? $snapshot->name,
]);
ModuleSnapshotLimit::where('customer_id', $snapshot->customer_id)
->increment('used');
event(new NotificationStatus(
"Snapshot {$snapshot->name} tạo thành công",
true
));
$product->loadMissing('customer');
if ($product->customer?->email) {
Mail::to($product->customer->email)->queue(new SnapshotActionMail([
'action' => 'create',
'fullname' => $product->customer->fullname,
'vm_name' => $product->name,
'snapshot_name' => $snapshot->name,
'action_at' => now()->format('d/m/Y H:i:s'),
]));
}
$this->telegram->SendMessage(
"📸 SNAPSHOT CREATED SUCCESSFULLY\n\n" .
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" .
"🖥️ Server: `{$product->name}`\n" .
"📸 Snapshot: `{$snapshot->name}`\n" .
"👤 Customer: `#{$snapshot->customer_id}`\n" .
"🆔 VM ID: `{$product->vmware_id}`\n" .
"⏰ Created: `" . now()->format('d/m/Y H:i:s') . "`\n" .
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" .
"💡 Status: ✅ Completed\n" .
"🔧 Action: Snapshot Creation\n" .
"📊 Queue: snapshots"
);
} catch (\Throwable $e) {
$snapshot->update([
'status' => 'failed',
'error_message' => $e->getMessage(),
]);
throw $e;
}
}
public function failed(\Throwable $exception)
{
$snapshot = ModuleSnapshot::find($this->snapshotId);
if ($snapshot) {
$snapshot->update([
'status' => 'failed',
'error_message' => $exception->getMessage(),
]);
}
event(new NotificationStatus(
"Snapshot thất bại: {$exception->getMessage()}",
false
));
$this->telegram->SendErrorMessage([
'job_name' => 'CreateSnapshot',
'snapshot_id' => $this->snapshotId,
'error' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
]);
}
}