diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 98617af..5d432f7 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -29,7 +29,7 @@ protected function schedule(Schedule $schedule) $schedule->command('sanctum:prune-expired --hours=24')->daily(); // 扣费 - $schedule->job(new HostCost(now()->hour))->hourly()->withoutOverlapping()->onOneServer(); + $schedule->job(new HostCost(now()->minute))->everyMinute()->withoutOverlapping()->onOneServer(); // 获取模块暴露的信息(服务器等) $schedule->job(new FetchModule())->withoutOverlapping()->everyMinute(); diff --git a/app/Http/Controllers/Admin/ModuleController.php b/app/Http/Controllers/Admin/ModuleController.php index c76385b..b92bdbd 100644 --- a/app/Http/Controllers/Admin/ModuleController.php +++ b/app/Http/Controllers/Admin/ModuleController.php @@ -61,21 +61,14 @@ public function store(Request $request): RedirectResponse $module->name = $request->name; $module->api_token = $api_token; $module->url = $request->url; + $module->status = $request->status; + $module->save(); return redirect()->route('admin.modules.index')->with('success', '模块创建成功, 请重置以获得 API Token。'); } - private function rules(): array - { - return [ - 'id' => 'required|string|max:255', - 'name' => 'required|string|max:255', - 'url' => 'required|url', - ]; - } - /** * Display the specified resource. * @@ -128,6 +121,7 @@ public function update(Request $request, Module $module): RedirectResponse $module->id = $request->id; $module->name = $request->name; $module->url = $request->url; + $module->status = $request->status; $module->save(); @@ -156,4 +150,15 @@ public function destroy(Module $module): RedirectResponse return redirect()->route('admin.modules.index')->with('success', '模块已删除。'); } + + private function rules(): array + { + return [ + 'id' => 'required|string|max:255', + 'name' => 'required|string|max:255', + 'url' => 'required|url', + 'status' => 'required|string|in:up,down,maintenance', + ]; + } + } diff --git a/app/Http/Controllers/Api/ModuleController.php b/app/Http/Controllers/Api/ModuleController.php index 81f86e1..2848306 100644 --- a/app/Http/Controllers/Api/ModuleController.php +++ b/app/Http/Controllers/Api/ModuleController.php @@ -20,6 +20,4 @@ public function call(Request $request, Module $module): JsonResponse { return (new \App\Http\Controllers\Modules\ModuleController())->call($request, $module); } - - } diff --git a/app/Jobs/CheckHostIfExistsOnModule.php b/app/Jobs/CheckHostIfExistsOnModule.php index 696dcab..430ed3a 100644 --- a/app/Jobs/CheckHostIfExistsOnModule.php +++ b/app/Jobs/CheckHostIfExistsOnModule.php @@ -32,11 +32,15 @@ public function __construct() */ public function handle() { - // now 添加1.5小时 - - // + // 删除所有模块中不存在的主机 Host::with('module')->where('created_at', '<', now()->subHour())->chunk(100, function ($hosts) { foreach ($hosts as $host) { + + // 忽略维护中的模块 + if ($host->module->status !== 'up') { + continue; + } + $http = Http::module($host->module->api_token, $host->module->url); $response = $http->get('hosts/' . $host->id); diff --git a/app/Jobs/HostCost.php b/app/Jobs/HostCost.php index f22a32a..acbf6fe 100644 --- a/app/Jobs/HostCost.php +++ b/app/Jobs/HostCost.php @@ -13,17 +13,17 @@ class HostCost implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels, Lock; - public $hour, $cache, $user; + public $minute, $cache, $user; /** * Create a new job instance. * * @return void */ - public function __construct($hour) + public function __construct($minute) { // - $this->hour = $hour; + $this->minute = $minute; } /** @@ -34,7 +34,7 @@ public function __construct($hour) public function handle() { // chunk hosts and load user - Host::where('hour_at', $this->hour)->whereIn('status', ['running', 'stopped'])->with('user')->chunk(1000, function ($hosts) { + Host::where('minute_at', $this->minute)->whereIn('status', ['running', 'stopped'])->with('user')->chunk(500, function ($hosts) { foreach ($hosts as $host) { $host->cost(); } diff --git a/app/Jobs/Module/FetchModule.php b/app/Jobs/Module/FetchModule.php index 5615488..55c0766 100644 --- a/app/Jobs/Module/FetchModule.php +++ b/app/Jobs/Module/FetchModule.php @@ -36,13 +36,13 @@ public function handle() { // 获取运行完成的时间 - $last_run = Cache::get('servers_updated_at', false); - if ($last_run !== false) { - // 如果和上次运行时间间隔小于一分钟,则不运行 - if (now()->diffInMinutes($last_run) < 1) { - return; - } - } + // $last_run = Cache::get('servers_updated_at', false); + // if ($last_run !== false) { + // // 如果和上次运行时间间隔小于一分钟,则不运行 + // if (now()->diffInMinutes($last_run) < 1) { + // return; + // } + // } // Module::whereNotNull('url')->chunk(100, function ($modules) { @@ -58,8 +58,13 @@ public function handle() continue; } - if ($response->successful()) { + + // 如果模块状态不为 up,则更新为 up + if ($module->status !== 'up') { + $module->status = 'up'; + } + $json = $response->json(); if (isset($json['data']['servers']) && is_array($json['data']['servers'])) { @@ -79,10 +84,18 @@ public function handle() broadcast(new ServerEvent($servers)); } - // $module->update([ - // 'data' => $response->json() - // ]); + + } else { + + // if module return maintenance, then set module status to maintenance + if ($response->status() == 503) { + $module->status = 'maintenance'; + } else { + $module->status = 'down'; + } } + + $module->save(); } // if local @@ -93,7 +106,7 @@ public function handle() } // 缓存运行完成的时间 - Cache::put('servers_updated_at', now(), now()->addMinutes(10)); + // Cache::put('servers_updated_at', now(), now()->addMinutes(10)); }); } } diff --git a/app/Models/Host.php b/app/Models/Host.php index f6faef0..ad9479e 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -98,6 +98,7 @@ protected static function boot() static::creating(function ($model) { $model->hour_at = now()->hour; + $model->minute_at = now()->minute_at; if ($model->price !== null) { $model->price = round($model->price, 2); @@ -201,7 +202,10 @@ public function safeDelete(): bool { // 如果创建时间大于大于 1 小时 if ($this->created_at->diffInHours(now()) > 1) { - $this->cost(); + // 如果当前时间比扣费时间小,则说明没有扣费。执行扣费。 + if (now()->minute < $this->minute_at) { + $this->cost(); + } } dispatch(new \App\Jobs\Module\Host($this, 'delete')); diff --git a/database/migrations/2022_11_23_103355_add_minute_at_to_hosts.php b/database/migrations/2022_11_23_103355_add_minute_at_to_hosts.php new file mode 100644 index 0000000..136c1a0 --- /dev/null +++ b/database/migrations/2022_11_23_103355_add_minute_at_to_hosts.php @@ -0,0 +1,47 @@ +tinyInteger('minute_at')->index()->nullable()->after('hour_at'); + }); + + echo PHP_EOL . '将开始刷新主机的分钟数...'; + Host::chunk(100, function ($hosts) { + foreach ($hosts as $host) { + echo '刷新: ' . $host->id . PHP_EOL; + $host->minute_at = $host->created_at->minute; + $host->save(); + } + }); + echo ' 完成!' . PHP_EOL; + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('hosts', function (Blueprint $table) { + // + + $table->dropColumn('minute_at'); + }); + } +}; diff --git a/database/migrations/2022_11_23_104838_add_status_to_modules_table.php b/database/migrations/2022_11_23_104838_add_status_to_modules_table.php new file mode 100644 index 0000000..2c1ddee --- /dev/null +++ b/database/migrations/2022_11_23_104838_add_status_to_modules_table.php @@ -0,0 +1,35 @@ +enum('status', ['up', 'down', 'maintenance'])->index()->default('down')->after('url'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('modules', function (Blueprint $table) { + // + + $table->dropColumn('status'); + }); + } +}; diff --git a/resources/views/admin/modules/create.blade.php b/resources/views/admin/modules/create.blade.php index 57f454d..d4efe04 100644 --- a/resources/views/admin/modules/create.blade.php +++ b/resources/views/admin/modules/create.blade.php @@ -3,27 +3,37 @@ @section('title', '新建模块') @section('content') -
状态: {{ $module->status }}
+编辑 +ID | 名称 | 用户 | @@ -21,41 +22,41 @@创建时间 | 更新时间 | 操作 | - + - + @foreach ($hosts as $host) -|
---|---|---|---|---|---|---|
- - {{ $host->id }} - - | -- {{ $host->name }} - | -- {{ $host->user->name }} - | -- {{ $host->price }} 元 - | -- {{ $host->created_at }} - | -- {{ $host->updated_at }} - | -- 编辑 - | -
+ + {{ $host->id }} + + | ++ {{ $host->name }} + | ++ {{ $host->user->name }} + | ++ {{ $host->price }} 元 + | ++ {{ $host->created_at }} + | ++ {{ $host->updated_at }} + | ++ 编辑 + | +