From 4a3f6d20ffdf0eba385fb551ff07e6f02befb170 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 6 Feb 2023 15:28:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Jobs/Host/HostJob.php | 10 ++++++++++ app/Jobs/Module/FetchModuleJob.php | 9 ++++++++- app/Models/Module.php | 5 +++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/Jobs/Host/HostJob.php b/app/Jobs/Host/HostJob.php index 8a0be8e..ec457b2 100644 --- a/app/Jobs/Host/HostJob.php +++ b/app/Jobs/Host/HostJob.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Log; // use Illuminate\Contracts\Queue\ShouldBeUnique; @@ -54,6 +55,15 @@ public function handle(): void $host->load(['module']); + if ($host->module->status !== 'up') { + Log::warning('模块不可用,跳过主机更新。', [ + 'host' => $host->name, + 'module' => $host->module->name, + ]); + + return; + } + switch ($this->type) { case 'patch': $response = $host->module->http()->patch('hosts/'.$host->id, $host->toArray()); diff --git a/app/Jobs/Module/FetchModuleJob.php b/app/Jobs/Module/FetchModuleJob.php index aeb21ad..fca1306 100644 --- a/app/Jobs/Module/FetchModuleJob.php +++ b/app/Jobs/Module/FetchModuleJob.php @@ -42,7 +42,13 @@ public function handle(): void try { $response = $module->http()->get('remote'); } catch (Exception $e) { - Log::error($e->getMessage()); + Log::error('无法连接到模块 - down: '.$e->getMessage()); + + // 如果模块状态不为 down,则更新为 down + if ($module->status !== 'down') { + $module->status = 'down'; + $module->save(); + } return; } @@ -51,6 +57,7 @@ public function handle(): void // 如果模块状态不为 up,则更新为 up if ($module->status !== 'up') { $module->status = 'up'; + Log::error('模块状态更新为 up: '.$module->name); } $json = $response->json(); diff --git a/app/Models/Module.php b/app/Models/Module.php index 602cdd1..6f80d90 100644 --- a/app/Models/Module.php +++ b/app/Models/Module.php @@ -31,6 +31,7 @@ class Module extends Authenticatable 'id', 'name', 'api_token', + 'status', ]; protected $hidden = [ @@ -118,7 +119,6 @@ private function getResponse(Response $response): array * @param $method * @param $path * @param $requests - * * @return array */ public function request($method, $path, $requests): array @@ -126,7 +126,8 @@ public function request($method, $path, $requests): array try { return $this->baseRequest($method, "functions/$path", $requests); } catch (ConnectException|ConnectionException $e) { - Log::error('在执行 call ' . $method . ' ' . $path . ' 时发生错误: ' . $e->getMessage()); + Log::error('在执行 call '.$method.' '.$path.' 时发生错误: '.$e->getMessage()); + return [ 'body' => null, 'json' => null,