diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index e4af124..34ed25d 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -4,9 +4,9 @@ use App\Jobs\AutoCloseWorkOrderJob; use App\Jobs\CheckAndChargeBalanceJob; -use App\Jobs\CheckHostIfExistsOnModuleJob; use App\Jobs\ClearTasksJob; use App\Jobs\DeleteHostJob; +use App\Jobs\Host\ScanAllHostsJob; use App\Jobs\HostCostJob; use App\Jobs\Module\FetchModuleJob; use App\Jobs\Module\PushWorkOrderJob; @@ -48,7 +48,7 @@ protected function schedule(Schedule $schedule): void $schedule->job(new DeleteHostJob())->hourly(); // 检查主机是否存在于模块 - $schedule->job(new CheckHostIfExistsOnModuleJob())->everyThirtyMinutes()->withoutOverlapping()->onOneServer(); + $schedule->job(new ScanAllHostsJob())->everyThirtyMinutes()->withoutOverlapping()->onOneServer(); // 检查未充值的订单,并充值 $schedule->job(new CheckAndChargeBalanceJob())->everyFiveMinutes()->onOneServer()->withoutOverlapping(); diff --git a/app/Http/Controllers/Admin/HostController.php b/app/Http/Controllers/Admin/HostController.php index 8733a3d..c8528d3 100644 --- a/app/Http/Controllers/Admin/HostController.php +++ b/app/Http/Controllers/Admin/HostController.php @@ -43,8 +43,6 @@ public function index(Request $request): View */ public function edit(Host $host): View { - // - return view('admin.hosts.edit', compact('host')); } @@ -60,7 +58,7 @@ public function update(Request $request, Host $host): RedirectResponse { $request->validate([ 'name' => 'sometimes|string|max:255', - 'status' => 'sometimes|in:running,stopped,error,suspended,pending', + 'status' => 'sometimes|in:running,stopped,suspended,pending', 'price' => 'sometimes|numeric', 'managed_price' => 'nullable|numeric', ]); @@ -83,4 +81,11 @@ public function destroy(Host $host): RedirectResponse return redirect()->route('admin.hosts.index')->with('success', '正在排队删除此主机。'); } + + public function updateOrDelete(Host $host): RedirectResponse + { + $host->updateOrDelete(); + + return back()->with('success', '正在排队刷新此主机的状态。'); + } } diff --git a/app/Jobs/CheckHostIfExistsOnModuleJob.php b/app/Jobs/Host/ScanAllHostsJob.php similarity index 59% rename from app/Jobs/CheckHostIfExistsOnModuleJob.php rename to app/Jobs/Host/ScanAllHostsJob.php index 2c442bd..69b8762 100644 --- a/app/Jobs/CheckHostIfExistsOnModuleJob.php +++ b/app/Jobs/Host/ScanAllHostsJob.php @@ -1,6 +1,6 @@ where('created_at', '<', now()->subHour())->chunk(100, function ($hosts) { foreach ($hosts as $host) { - // 忽略维护中的模块 if ($host->module->status !== 'up') { continue; } - $response = $host->module->http()->get('hosts/' . $host->id); + $host->updateOrDelete(); - $status = $response->status(); - - if ($status === 200) { - // 更新主机 - $host->update($response->json()); - } else if ($status === 404) { - Log::warning($host->module->name . ' ' . $host->name . ' ' . $host->id . ' 不存在,删除。'); - dispatch(new Module\HostJob($host, 'delete')); - } } }); } diff --git a/app/Jobs/Host/UpdateOrDeleteHostJob.php b/app/Jobs/Host/UpdateOrDeleteHostJob.php new file mode 100644 index 0000000..b27fb4d --- /dev/null +++ b/app/Jobs/Host/UpdateOrDeleteHostJob.php @@ -0,0 +1,50 @@ +host = $host; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle(): void + { + $host = $this->host; + + $response = $host->module->http()->get('hosts/' . $host->id); + + $status = $response->status(); + + if ($status === 200) { + $host->update($response->json()); + } else if ($status === 404) { + Log::warning($host->module->name . ' ' . $host->name . ' ' . $host->id . ' 不存在,删除。'); + dispatch(new HostJob($host, 'delete')); + } + } +} diff --git a/app/Models/Host.php b/app/Models/Host.php index 5c9a8ad..58f9813 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Events\Users; +use App\Jobs\Host\UpdateOrDeleteHostJob; use App\Jobs\Module\HostJob; use App\Notifications\WebNotification; use GeneaLabs\LaravelModelCaching\Traits\Cachable; @@ -152,6 +153,13 @@ public function safeDelete(): bool return true; } + public function updateOrDelete(): bool + { + dispatch(new UpdateOrDeleteHostJob($this)); + + return true; + } + public function cost($amount = null, $auto = true): bool { $this->load('user'); diff --git a/resources/views/admin/hosts/edit.blade.php b/resources/views/admin/hosts/edit.blade.php index 9dd4d07..253c975 100644 --- a/resources/views/admin/hosts/edit.blade.php +++ b/resources/views/admin/hosts/edit.blade.php @@ -29,6 +29,7 @@ + @@ -36,6 +37,14 @@ +