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-09-01 14:45:47 +00:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
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\Http;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-09-08 16:12:02 +00:00
|
|
|
|
2022-08-19 09:51:52 +00:00
|
|
|
class FetchModule implements ShouldQueue
|
|
|
|
{
|
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
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2022-09-01 14:45:47 +00:00
|
|
|
// 获取运行完成的时间
|
|
|
|
|
|
|
|
$last_run = Cache::get('servers_updated_at', false);
|
|
|
|
if ($last_run !== false) {
|
|
|
|
// 如果和上次运行时间间隔小于一分钟,则不运行
|
|
|
|
if (now()->diffInMinutes($last_run) < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 09:51:52 +00:00
|
|
|
//
|
2022-08-31 09:26:53 +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-16 02:29:50 +00:00
|
|
|
$http = Http::module($module->api_token, $module->url);
|
2022-09-01 14:45:47 +00:00
|
|
|
// dd($module->url);
|
|
|
|
$response = $http->get('remote');
|
|
|
|
} catch (ConnectException $e) {
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-19 09:51:52 +00:00
|
|
|
|
|
|
|
if ($response->successful()) {
|
2022-08-31 08:51:38 +00:00
|
|
|
$json = $response->json();
|
|
|
|
|
2022-09-03 05:18:13 +00:00
|
|
|
if (isset($json['data']['servers']) && is_array($json['data']['servers'])) {
|
2022-08-31 08:51:38 +00:00
|
|
|
// 只保留 name, status
|
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-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
|
|
|
];
|
|
|
|
}, $json['data']['servers']));
|
2022-09-22 06:00:03 +00:00
|
|
|
|
|
|
|
broadcast(new ServerEvent($servers));
|
2022-08-31 08:51:38 +00:00
|
|
|
}
|
2022-08-19 09:51:52 +00:00
|
|
|
// $module->update([
|
|
|
|
// 'data' => $response->json()
|
|
|
|
// ]);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// 缓存运行完成的时间
|
|
|
|
Cache::put('servers_updated_at', now(), now()->addMinutes(10));
|
2022-08-19 09:51:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|