改进 状态处理

This commit is contained in:
iVampireSP.com 2023-02-06 15:28:00 +08:00
parent 71ab005e07
commit 4a3f6d20ff
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 21 additions and 3 deletions

View File

@ -7,6 +7,7 @@
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
// use Illuminate\Contracts\Queue\ShouldBeUnique; // use Illuminate\Contracts\Queue\ShouldBeUnique;
@ -54,6 +55,15 @@ public function handle(): void
$host->load(['module']); $host->load(['module']);
if ($host->module->status !== 'up') {
Log::warning('模块不可用,跳过主机更新。', [
'host' => $host->name,
'module' => $host->module->name,
]);
return;
}
switch ($this->type) { switch ($this->type) {
case 'patch': case 'patch':
$response = $host->module->http()->patch('hosts/'.$host->id, $host->toArray()); $response = $host->module->http()->patch('hosts/'.$host->id, $host->toArray());

View File

@ -42,7 +42,13 @@ public function handle(): void
try { try {
$response = $module->http()->get('remote'); $response = $module->http()->get('remote');
} catch (Exception $e) { } catch (Exception $e) {
Log::error($e->getMessage()); Log::error('无法连接到模块 - down: '.$e->getMessage());
// 如果模块状态不为 down则更新为 down
if ($module->status !== 'down') {
$module->status = 'down';
$module->save();
}
return; return;
} }
@ -51,6 +57,7 @@ public function handle(): void
// 如果模块状态不为 up则更新为 up // 如果模块状态不为 up则更新为 up
if ($module->status !== 'up') { if ($module->status !== 'up') {
$module->status = 'up'; $module->status = 'up';
Log::error('模块状态更新为 up: '.$module->name);
} }
$json = $response->json(); $json = $response->json();

View File

@ -31,6 +31,7 @@ class Module extends Authenticatable
'id', 'id',
'name', 'name',
'api_token', 'api_token',
'status',
]; ];
protected $hidden = [ protected $hidden = [
@ -118,7 +119,6 @@ private function getResponse(Response $response): array
* @param $method * @param $method
* @param $path * @param $path
* @param $requests * @param $requests
*
* @return array * @return array
*/ */
public function request($method, $path, $requests): array public function request($method, $path, $requests): array
@ -127,6 +127,7 @@ public function request($method, $path, $requests): array
return $this->baseRequest($method, "functions/$path", $requests); return $this->baseRequest($method, "functions/$path", $requests);
} catch (ConnectException|ConnectionException $e) { } catch (ConnectException|ConnectionException $e) {
Log::error('在执行 call '.$method.' '.$path.' 时发生错误: '.$e->getMessage()); Log::error('在执行 call '.$method.' '.$path.' 时发生错误: '.$e->getMessage());
return [ return [
'body' => null, 'body' => null,
'json' => null, 'json' => null,