Lae/app/Jobs/CheckHostIfExistsOnModuleJob.php

58 lines
1.5 KiB
PHP
Raw Normal View History

2022-11-08 09:46:00 +00:00
<?php
namespace App\Jobs;
use App\Models\Host;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2022-11-08 13:01:43 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2022-11-08 12:23:30 +00:00
use Illuminate\Support\Facades\Log;
2022-11-08 09:46:00 +00:00
2022-12-28 13:17:54 +00:00
class CheckHostIfExistsOnModuleJob implements ShouldQueue
2022-11-08 09:46:00 +00:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
2022-12-11 12:59:17 +00:00
public function handle(): void
2022-11-08 09:46:00 +00:00
{
2022-11-23 03:10:36 +00:00
// 删除所有模块中不存在的主机
2022-11-08 09:48:59 +00:00
Host::with('module')->where('created_at', '<', now()->subHour())->chunk(100, function ($hosts) {
2022-11-08 09:46:00 +00:00
foreach ($hosts as $host) {
2022-11-23 03:10:36 +00:00
// 忽略维护中的模块
if ($host->module->status !== 'up') {
continue;
}
2022-11-30 07:57:31 +00:00
$response = $host->module->http()->get('hosts/' . $host->id);
2022-11-08 09:46:00 +00:00
2023-01-13 10:04:25 +00:00
$status = $response->status();
if ($status === 200) {
// 更新主机
$host->update($response->json());
} else if ($status === 404) {
2022-11-08 13:00:22 +00:00
Log::warning($host->module->name . ' ' . $host->name . ' ' . $host->id . ' 不存在,删除。');
2022-12-28 13:17:54 +00:00
dispatch(new Module\HostJob($host, 'delete'));
2022-11-08 09:46:00 +00:00
}
}
});
}
}