Lae/app/Jobs/Host/UpdateOrDeleteHostJob.php

53 lines
1.2 KiB
PHP
Raw Normal View History

2023-01-13 10:42:05 +00:00
<?php
namespace App\Jobs\Host;
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;
2023-01-18 12:11:49 +00:00
use Illuminate\Support\Arr;
2023-01-13 10:42:05 +00:00
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();
2023-01-18 12:11:49 +00:00
$json = $response->json();
2023-01-13 10:42:05 +00:00
if ($status === 200) {
2023-01-18 12:11:49 +00:00
$host->update(Arr::except($json, ['id', 'user_id', 'module_id', 'created_at', 'updated_at']));
2023-01-13 10:42:05 +00:00
} else if ($status === 404) {
Log::warning($host->module->name . ' ' . $host->name . ' ' . $host->id . ' 不存在,删除。');
dispatch(new HostJob($host, 'delete'));
}
}
}