Lae/app/Jobs/CheckHostIfExistsOnModule.php

51 lines
1.3 KiB
PHP
Raw Normal View History

2022-11-08 09:46:00 +00:00
<?php
namespace App\Jobs;
use App\Models\Host;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2022-11-08 13:01:43 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
2022-11-08 12:23:30 +00:00
use Illuminate\Support\Facades\Log;
2022-11-08 09:46:00 +00:00
class CheckHostIfExistsOnModule implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2022-11-08 09:48:59 +00:00
// now 添加1.5小时
2022-11-08 09:46:00 +00:00
//
2022-11-08 09:48:59 +00:00
Host::with('module')->where('created_at', '<', now()->subHour())->chunk(100, function ($hosts) {
2022-11-08 09:46:00 +00:00
foreach ($hosts as $host) {
$http = Http::remote($host->module->api_token, $host->module->url);
$response = $http->get('hosts/' . $host->id);
if ($response->status() === 404) {
2022-11-08 13:00:22 +00:00
Log::warning($host->module->name . ' ' . $host->name . ' ' . $host->id . ' 不存在,删除。');
2022-11-08 12:55:17 +00:00
dispatch(new \App\Jobs\Remote\Host($host, 'delete'));
2022-11-08 09:46:00 +00:00
}
}
});
}
}