Lae/app/Jobs/Module/PushHostJob.php

61 lines
1.4 KiB
PHP
Raw Normal View History

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
use App\Models\Host;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
2022-11-06 11:28:22 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2022-09-08 16:12:02 +00:00
// use Illuminate\Contracts\Queue\ShouldBeUnique;
2022-08-19 09:51:52 +00:00
2022-12-28 13:17:54 +00:00
class PushHostJob 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
*/
public function handle()
{
//
Host::whereIn('status', ['pending', 'error'])->with(['module', 'user'])->chunk(100, function ($hosts) {
foreach ($hosts as $host) {
$host->status = 'running';
2022-09-08 16:12:02 +00:00
2022-11-30 07:57:31 +00:00
$response = $host->module->http()->post('hosts', $host->toArray());
2022-09-08 16:12:02 +00:00
2022-08-19 09:51:52 +00:00
if (!$response->successful()) {
$host->status = 'error';
}
2022-08-26 14:37:20 +00:00
// dd($response);
$response_json = $response->json();
// 检测是否有价格
2022-12-18 03:54:01 +00:00
if (isset($response_json['price'])) {
$host->price = $response_json['price'];
} else {
$host->status = 'error';
}
2022-09-08 16:12:02 +00:00
2022-08-19 09:51:52 +00:00
$host->save();
2022-09-08 16:12:02 +00:00
2022-08-19 09:51:52 +00:00
}
});
}
}