Update source into github
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\VirtualMachine\Snapshot;
|
||||
|
||||
use App\Events\NotificationStatus;
|
||||
use App\Http\Services\Telegram;
|
||||
use App\Http\Services\vCenter as ServiceVCenter;
|
||||
use App\Models\ModuleSnapshot;
|
||||
use App\Models\ModuleSnapshotLimit;
|
||||
use App\Models\ModuleVmware;
|
||||
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;
|
||||
|
||||
class RemoveSnapshot implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $productId;
|
||||
protected $snapshotId;
|
||||
protected $telegram;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param int $productId
|
||||
* @param int $snapshotId
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($productId = 0, $snapshotId = 0)
|
||||
{
|
||||
$this->productId = $productId;
|
||||
$this->snapshotId = $snapshotId;
|
||||
$this->telegram = new Telegram();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(ServiceVCenter $serviceVCenter)
|
||||
{
|
||||
try {
|
||||
// Load product and snapshot with necessary relationships
|
||||
$product = ModuleVmware::with('ipAddress.host.datacenter.vCenter')->find($this->productId);
|
||||
$snapshot = ModuleSnapshot::find($this->snapshotId);
|
||||
|
||||
if (!$product || !$snapshot) {
|
||||
throw new \Exception("Không tìm thấy thông tin VPS hoặc snapshot");
|
||||
}
|
||||
|
||||
// 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
|
||||
$serviceVCenter->token = $vcenter->token;
|
||||
$serviceVCenter->apiUri = $vcenter->host;
|
||||
|
||||
Log::info('🗑️ Bắt đầu xóa snapshot', [
|
||||
'product_id' => $product->id,
|
||||
'snapshot_id' => $snapshot->id,
|
||||
'snapshot_name' => $snapshot->name,
|
||||
'vcenter_host' => $vcenter->host
|
||||
]);
|
||||
|
||||
// Attempt to remove snapshot
|
||||
$result = $serviceVCenter->RemoveSnapShot($product, $snapshot->name);
|
||||
|
||||
// Handle re-login if needed
|
||||
if ($result === 'Login_Again') {
|
||||
Log::info('🔑 Cần đăng nhập lại vCenter');
|
||||
$token = $serviceVCenter->loginServer($vcenter);
|
||||
if ($token) {
|
||||
$vcenter->update(['token' => $token]);
|
||||
$serviceVCenter->token = $token;
|
||||
$result = $serviceVCenter->RemoveSnapShot($product, $snapshot->name);
|
||||
} else {
|
||||
throw new \Exception("Không thể đăng nhập lại vào vCenter");
|
||||
}
|
||||
}
|
||||
|
||||
// Check for errors
|
||||
if (is_array($result) && isset($result['error']) && $result['error']) {
|
||||
$errorMessage = 'Lỗi xóa snapshot';
|
||||
if (isset($result['message'])) {
|
||||
$errorMessage .= ': ' . (is_array($result['message']) ? json_encode($result['message']) : $result['message']);
|
||||
}
|
||||
throw new \Exception($errorMessage);
|
||||
}
|
||||
|
||||
if (is_string($result) && $result !== 'Login_Again') {
|
||||
throw new \Exception("Lỗi xóa snapshot: {$result}");
|
||||
}
|
||||
|
||||
// Decrease snapshot limit counter
|
||||
ModuleSnapshotLimit::where('customer_id', $product->customer_id)->increment('used', -1);
|
||||
|
||||
// Store snapshot name before deletion for notification
|
||||
$snapshotName = $snapshot->name;
|
||||
|
||||
// Delete snapshot from database
|
||||
$snapshot->delete();
|
||||
|
||||
Log::info('✅ Xóa snapshot thành công', [
|
||||
'product_id' => $product->id,
|
||||
'snapshot_name' => $snapshotName
|
||||
]);
|
||||
|
||||
// Send success notification
|
||||
Event(new NotificationStatus("Xóa snapshot thành công cho VPS {$product->name}", true));
|
||||
|
||||
// Send Telegram notification
|
||||
$this->telegram->SendMessage(
|
||||
"🗑️ <b>SNAPSHOT REMOVED 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>Removed:</b> `" . now()->format('d/m/Y H:i:s') . "`\n" .
|
||||
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" .
|
||||
"💡 <b>Status:</b> ✅ Completed\n" .
|
||||
"🔧 <b>Action:</b> Snapshot Removal\n" .
|
||||
"📊 <b>Queue:</b> snapshots"
|
||||
);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('❌ Lỗi xóa snapshot', [
|
||||
'product_id' => $this->productId,
|
||||
'snapshot_id' => $this->snapshotId,
|
||||
'error' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine()
|
||||
]);
|
||||
|
||||
throw $e; // Re-throw để kích hoạt failed method
|
||||
}
|
||||
}
|
||||
|
||||
public function failed(\Throwable $exception)
|
||||
{
|
||||
Log::error('❌ Job RemoveSnapshot failed', [
|
||||
'product_id' => $this->productId,
|
||||
'snapshot_id' => $this->snapshotId,
|
||||
'error' => $exception->getMessage(),
|
||||
'trace' => $exception->getTraceAsString()
|
||||
]);
|
||||
|
||||
// Send failure notification
|
||||
Event(new NotificationStatus("Lỗi xóa snapshot: {$exception->getMessage()}", false));
|
||||
|
||||
// Send Telegram error notification
|
||||
$this->telegram->SendErrorMessage([
|
||||
'job_name' => 'RemoveSnapshot',
|
||||
'product_id' => $this->productId,
|
||||
'snapshot_id' => $this->snapshotId,
|
||||
'error' => $exception->getMessage(),
|
||||
'trace' => $exception->getTraceAsString()
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user