2022-08-19 09:51:52 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2022-11-16 02:29:50 +00:00
|
|
|
|
namespace App\Jobs\Module;
|
2022-08-19 09:51:52 +00:00
|
|
|
|
|
2022-09-22 06:00:03 +00:00
|
|
|
|
use App\Events\ServerEvent;
|
2022-11-06 11:28:22 +00:00
|
|
|
|
use App\Models\Module;
|
2022-12-27 16:25:22 +00:00
|
|
|
|
use Exception;
|
2022-11-06 11:28:22 +00:00
|
|
|
|
use Illuminate\Bus\Queueable;
|
2022-08-19 09:51:52 +00:00
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2022-11-06 11:28:22 +00:00
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-09-08 16:12:02 +00:00
|
|
|
|
|
2022-12-28 13:17:54 +00:00
|
|
|
|
class FetchModuleJob implements ShouldQueue
|
2022-08-19 09:51:52 +00:00
|
|
|
|
{
|
2022-09-08 16:12:02 +00:00
|
|
|
|
use InteractsWithQueue, Queueable, SerializesModels;
|
2022-08-19 09:51:52 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new job instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the job.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2022-12-31 11:28:21 +00:00
|
|
|
|
public function handle(): void
|
2022-08-19 09:51:52 +00:00
|
|
|
|
{
|
2022-09-01 14:45:47 +00:00
|
|
|
|
// 获取运行完成的时间
|
|
|
|
|
|
2022-11-23 03:25:12 +00:00
|
|
|
|
// $last_run = Cache::get('servers_updated_at', false);
|
|
|
|
|
// if ($last_run !== false) {
|
|
|
|
|
// // 如果和上次运行时间间隔小于一分钟,则不运行
|
|
|
|
|
// if (now()->diffInMinutes($last_run) < 1) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2022-09-01 14:45:47 +00:00
|
|
|
|
|
2022-08-19 09:51:52 +00:00
|
|
|
|
//
|
2022-11-23 03:26:36 +00:00
|
|
|
|
Module::whereNotNull('url')->chunk(100, function ($modules) {
|
2022-08-31 08:51:38 +00:00
|
|
|
|
$servers = [];
|
|
|
|
|
|
2022-08-19 09:51:52 +00:00
|
|
|
|
foreach ($modules as $module) {
|
2022-09-01 14:45:47 +00:00
|
|
|
|
try {
|
2022-11-30 07:50:46 +00:00
|
|
|
|
$response = $module->http()->get('remote');
|
2022-12-27 16:25:22 +00:00
|
|
|
|
} catch (Exception $e) {
|
2022-12-05 04:47:48 +00:00
|
|
|
|
Log::error($e->getMessage());
|
2022-09-01 14:45:47 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 09:51:52 +00:00
|
|
|
|
if ($response->successful()) {
|
2022-11-23 03:16:23 +00:00
|
|
|
|
|
2022-11-23 03:26:36 +00:00
|
|
|
|
// 如果模块状态不为 up,则更新为 up
|
|
|
|
|
if ($module->status !== 'up') {
|
2022-11-23 03:20:06 +00:00
|
|
|
|
$module->status = 'up';
|
2022-11-23 03:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 08:51:38 +00:00
|
|
|
|
$json = $response->json();
|
|
|
|
|
|
2022-12-18 03:54:01 +00:00
|
|
|
|
if (isset($json['servers']) && is_array($json['servers'])) {
|
2022-11-30 07:50:46 +00:00
|
|
|
|
// 只保留 name, status, meta
|
2022-08-31 08:57:22 +00:00
|
|
|
|
$servers = array_merge($servers, array_map(function ($server) use ($module) {
|
2022-08-31 08:51:38 +00:00
|
|
|
|
return [
|
|
|
|
|
'name' => $server['name'],
|
|
|
|
|
'status' => $server['status'],
|
2022-11-30 07:50:46 +00:00
|
|
|
|
'meta' => $server['meta'] ?? [],
|
2022-09-03 05:18:13 +00:00
|
|
|
|
'created_at' => $server['created_at'] ?? now(),
|
|
|
|
|
'updated_at' => $server['updated_at'] ?? now(),
|
2022-09-03 06:08:21 +00:00
|
|
|
|
'module' => [
|
|
|
|
|
'id' => $module->id,
|
|
|
|
|
'name' => $module->name,
|
|
|
|
|
]
|
2022-08-31 08:51:38 +00:00
|
|
|
|
];
|
2022-12-18 03:54:01 +00:00
|
|
|
|
}, $json['servers']));
|
2022-09-22 06:00:03 +00:00
|
|
|
|
|
2022-12-30 11:29:04 +00:00
|
|
|
|
// broadcast(new ServerEvent($servers));
|
2022-08-31 08:51:38 +00:00
|
|
|
|
}
|
2022-11-23 03:16:23 +00:00
|
|
|
|
|
2022-11-23 03:10:36 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// if module return maintenance, then set module status to maintenance
|
|
|
|
|
if ($response->status() == 503) {
|
2022-11-23 03:20:06 +00:00
|
|
|
|
$module->status = 'maintenance';
|
2022-11-23 03:10:36 +00:00
|
|
|
|
} else {
|
2022-11-23 03:20:06 +00:00
|
|
|
|
$module->status = 'down';
|
2022-11-23 03:10:36 +00:00
|
|
|
|
}
|
2022-08-19 09:51:52 +00:00
|
|
|
|
}
|
2022-11-23 03:20:06 +00:00
|
|
|
|
|
|
|
|
|
$module->save();
|
2022-08-19 09:51:52 +00:00
|
|
|
|
}
|
2022-08-31 08:51:38 +00:00
|
|
|
|
|
2022-08-31 09:26:53 +00:00
|
|
|
|
// if local
|
|
|
|
|
if (config('app.env') === 'local') {
|
|
|
|
|
Cache::forever('servers', $servers);
|
|
|
|
|
} else {
|
|
|
|
|
Cache::put('servers', $servers, now()->addMinutes(10));
|
|
|
|
|
}
|
2022-09-01 14:45:47 +00:00
|
|
|
|
|
|
|
|
|
// 缓存运行完成的时间
|
2022-11-23 03:25:12 +00:00
|
|
|
|
// Cache::put('servers_updated_at', now(), now()->addMinutes(10));
|
2022-08-19 09:51:52 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|