140 lines
5.2 KiB
PHP
140 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Sync;
|
|
|
|
use App\Http\Services\Telegram;
|
|
use App\Http\Services\vCenter as ServiceVCenter;
|
|
use App\Models\ModuleDatacenter;
|
|
use App\Models\ModuleDatastore;
|
|
use App\Models\ModuleHost;
|
|
use App\Models\vCenter;
|
|
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 SyncDatastore implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $vCenter = null;
|
|
protected $data = null;
|
|
protected Telegram $telegram;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->data = vCenter::select('id', 'host', 'user', 'password', 'token')->get();
|
|
$this->vCenter = new ServiceVCenter;
|
|
$this->telegram = new Telegram();
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
try {
|
|
foreach ($this->data as $item) {
|
|
|
|
$this->vCenter->apiUri = $item->host;
|
|
|
|
if (!$item->token) {
|
|
$token = $this->vCenter->loginServer($item);
|
|
if ($token) {
|
|
vCenter::where('id', $item->id)->update(['token' => $token]);
|
|
|
|
$this->vCenter->token = $token;
|
|
}
|
|
}
|
|
$this->vCenter->token = $item->token;
|
|
|
|
if (!$this->vCenter->token) {
|
|
|
|
continue;
|
|
}
|
|
|
|
$dataCenter = ModuleDatacenter::where('vcenter_id', $item->id)->select('id', 'datacenter_id')->get();
|
|
foreach ($dataCenter as $_item) {
|
|
$allHost = ModuleHost::where('module_datacenter_id', $_item->id)->select('id', 'name')->get();
|
|
if ($allHost) {
|
|
foreach ($allHost as $hostItem) {
|
|
$datastore = $this->vCenter->loadDatastore($item, $hostItem->name);
|
|
|
|
if ($datastore === "Login_Again") {
|
|
$token = $this->vCenter->loginServer($item);
|
|
if ($token) {
|
|
vCenter::where('id', $item->id)->update(['token' => $token]);
|
|
$this->vCenter->token = $token;
|
|
}
|
|
}
|
|
if ($datastore) {
|
|
|
|
foreach ($datastore as $_subItem) {
|
|
|
|
if (!ModuleDatastore::where('datastore', $_subItem['datastore'])->first()) {
|
|
|
|
$template = $this->vCenter->getAllDataStore();
|
|
|
|
if ($template) {
|
|
ModuleDatastore::create([
|
|
'name' => $_subItem['name'],
|
|
'datastore' => $_subItem['datastore'],
|
|
'type' => $_subItem['type'],
|
|
'space' => floor($_subItem['free_space'] / 1073741824),
|
|
'module_host_id' => $hostItem->id
|
|
]);
|
|
ModuleHost::where('id', $hostItem->id)->update(['template' => $template]);
|
|
} else {
|
|
ModuleDatastore::create([
|
|
'name' => $_subItem['name'],
|
|
'datastore' => $_subItem['datastore'],
|
|
'type' => $_subItem['type'],
|
|
'space' => floor($_subItem['free_space'] / 1073741824),
|
|
'module_host_id' => $hostItem->id
|
|
]);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Notify success via Telegram
|
|
$this->telegram->SendJobRunInfo('SyncDatastore', 'Success', [
|
|
'message' => 'Sync datastore completed successfully',
|
|
'synced_vcenters' => $this->data->count(),
|
|
]);
|
|
} catch (\Throwable $exception) {
|
|
// Ghi log Laravel
|
|
Log::error('SyncDatastore Job failed', [
|
|
'error' => $exception->getMessage(),
|
|
'trace' => $exception->getTraceAsString(),
|
|
]);
|
|
|
|
// Gửi log lỗi qua Telegram
|
|
$this->telegram->SendJobRunInfo('SyncDatastore', 'Failed', [
|
|
'error' => $exception->getMessage(),
|
|
'file' => $exception->getFile(),
|
|
'line' => $exception->getLine(),
|
|
]);
|
|
|
|
// Quăng lại lỗi để Laravel xử lý retry/failure
|
|
throw $exception;
|
|
}
|
|
|
|
|
|
}
|
|
}
|