Files
portal-api/app/Jobs/VirtualMachine/Snapshot/CreateSnapshot.php
T

147 lines
5.0 KiB
PHP

<?php
namespace App\Jobs\VirtualMachine\Snapshot;
use App\Events\NotificationStatus;
use App\Http\Services\Telegram;
use App\Http\Services\vCenter as ServiceVCenter;
use App\Mail\Customer\SnapshotActionMail;
use App\Models\ModuleSnapshot;
use App\Models\ModuleSnapshotLimit;
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\Facades\Mail;
class CreateSnapshot implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected Telegram $telegram;
public function __construct(
public int $snapshotId
) {
$this->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(
"📸 <b>SNAPSHOT CREATED SUCCESSFULLY</b>\n\n" .
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" .
"🖥️ <b>Server:</b> `{$product->name}`\n" .
"📸 <b>Snapshot:</b> `{$snapshot->name}`\n" .
"👤 <b>Customer:</b> `#{$snapshot->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 (\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(),
]);
}
}