增加 刷新主机任务

This commit is contained in:
iVampireSP.com 2023-01-13 18:42:05 +08:00
parent f945f11791
commit 0e8972329f
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
7 changed files with 84 additions and 19 deletions

View File

@ -4,9 +4,9 @@
use App\Jobs\AutoCloseWorkOrderJob; use App\Jobs\AutoCloseWorkOrderJob;
use App\Jobs\CheckAndChargeBalanceJob; use App\Jobs\CheckAndChargeBalanceJob;
use App\Jobs\CheckHostIfExistsOnModuleJob;
use App\Jobs\ClearTasksJob; use App\Jobs\ClearTasksJob;
use App\Jobs\DeleteHostJob; use App\Jobs\DeleteHostJob;
use App\Jobs\Host\ScanAllHostsJob;
use App\Jobs\HostCostJob; use App\Jobs\HostCostJob;
use App\Jobs\Module\FetchModuleJob; use App\Jobs\Module\FetchModuleJob;
use App\Jobs\Module\PushWorkOrderJob; use App\Jobs\Module\PushWorkOrderJob;
@ -48,7 +48,7 @@ protected function schedule(Schedule $schedule): void
$schedule->job(new DeleteHostJob())->hourly(); $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(); $schedule->job(new CheckAndChargeBalanceJob())->everyFiveMinutes()->onOneServer()->withoutOverlapping();

View File

@ -43,8 +43,6 @@ public function index(Request $request): View
*/ */
public function edit(Host $host): View public function edit(Host $host): View
{ {
//
return view('admin.hosts.edit', compact('host')); return view('admin.hosts.edit', compact('host'));
} }
@ -60,7 +58,7 @@ public function update(Request $request, Host $host): RedirectResponse
{ {
$request->validate([ $request->validate([
'name' => 'sometimes|string|max:255', 'name' => 'sometimes|string|max:255',
'status' => 'sometimes|in:running,stopped,error,suspended,pending', 'status' => 'sometimes|in:running,stopped,suspended,pending',
'price' => 'sometimes|numeric', 'price' => 'sometimes|numeric',
'managed_price' => 'nullable|numeric', 'managed_price' => 'nullable|numeric',
]); ]);
@ -83,4 +81,11 @@ public function destroy(Host $host): RedirectResponse
return redirect()->route('admin.hosts.index')->with('success', '正在排队删除此主机。'); return redirect()->route('admin.hosts.index')->with('success', '正在排队删除此主机。');
} }
public function updateOrDelete(Host $host): RedirectResponse
{
$host->updateOrDelete();
return back()->with('success', '正在排队刷新此主机的状态。');
}
} }

View File

@ -1,6 +1,6 @@
<?php <?php
namespace App\Jobs; namespace App\Jobs\Host;
use App\Models\Host; use App\Models\Host;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
@ -8,9 +8,10 @@
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class CheckHostIfExistsOnModuleJob implements ShouldQueue // use Illuminate\Support\Facades\Log;
class ScanAllHostsJob implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -34,23 +35,13 @@ public function handle(): void
// 删除所有模块中不存在的主机 // 删除所有模块中不存在的主机
Host::with('module')->where('created_at', '<', now()->subHour())->chunk(100, function ($hosts) { Host::with('module')->where('created_at', '<', now()->subHour())->chunk(100, function ($hosts) {
foreach ($hosts as $host) { foreach ($hosts as $host) {
// 忽略维护中的模块 // 忽略维护中的模块
if ($host->module->status !== 'up') { if ($host->module->status !== 'up') {
continue; 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'));
}
} }
}); });
} }

View File

@ -0,0 +1,50 @@
<?php
namespace App\Jobs\Host;
use App\Jobs\Module\HostJob;
use App\Models\Host;
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 UpdateOrDeleteHostJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private Host $host;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Host $host)
{
$this->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'));
}
}
}

View File

@ -3,6 +3,7 @@
namespace App\Models; namespace App\Models;
use App\Events\Users; use App\Events\Users;
use App\Jobs\Host\UpdateOrDeleteHostJob;
use App\Jobs\Module\HostJob; use App\Jobs\Module\HostJob;
use App\Notifications\WebNotification; use App\Notifications\WebNotification;
use GeneaLabs\LaravelModelCaching\Traits\Cachable; use GeneaLabs\LaravelModelCaching\Traits\Cachable;
@ -152,6 +153,13 @@ public function safeDelete(): bool
return true; return true;
} }
public function updateOrDelete(): bool
{
dispatch(new UpdateOrDeleteHostJob($this));
return true;
}
public function cost($amount = null, $auto = true): bool public function cost($amount = null, $auto = true): bool
{ {
$this->load('user'); $this->load('user');

View File

@ -29,6 +29,7 @@
<option value="running" {{ $host->status == 'running' ? 'selected' : '' }}>运行中</option> <option value="running" {{ $host->status == 'running' ? 'selected' : '' }}>运行中</option>
<option value="stopped" {{ $host->status == 'stopped' ? 'selected' : '' }}>已停止</option> <option value="stopped" {{ $host->status == 'stopped' ? 'selected' : '' }}>已停止</option>
<option value="suspended" {{ $host->status == 'suspended' ? 'selected' : '' }}>已暂停</option> <option value="suspended" {{ $host->status == 'suspended' ? 'selected' : '' }}>已暂停</option>
<option value="error" {{ $host->status == 'error' ? 'selected' : '' }}>错误 (提交此项目将会被忽略)</option>
</select> </select>
</div> </div>
@ -36,6 +37,14 @@
</form> </form>
<hr/>
<form method="post" action="{{ route('admin.hosts.refresh', $host) }}">
@csrf
<button type="submit" class="btn btn-primary mt-3">刷新此主机</button>
</form>
<hr/>
<form method="post" action="{{ route('admin.hosts.destroy', $host) }}"> <form method="post" action="{{ route('admin.hosts.destroy', $host) }}">
@csrf @csrf

View File

@ -38,6 +38,8 @@
Route::get('modules/{module}/fast-login', [ModuleController::class, 'fast_login'])->name('modules.fast-login'); Route::get('modules/{module}/fast-login', [ModuleController::class, 'fast_login'])->name('modules.fast-login');
Route::resource('applications', ApplicationController::class); Route::resource('applications', ApplicationController::class);
Route::post('hosts/{host}/refresh', [HostController::class, 'updateOrDelete'])->name('hosts.refresh');
Route::resource('hosts', HostController::class)->only(['index', 'edit', 'update', 'destroy']); Route::resource('hosts', HostController::class)->only(['index', 'edit', 'update', 'destroy']);
Route::resource('work-orders', WorkOrderController::class)->only(['index', 'show', 'edit', 'update', 'destroy']); Route::resource('work-orders', WorkOrderController::class)->only(['index', 'show', 'edit', 'update', 'destroy']);