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;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
2022-09-08 16:12:02 +00:00
|
|
|
|
|
|
|
// use Illuminate\Contracts\Queue\ShouldBeUnique;
|
2022-08-19 09:51:52 +00:00
|
|
|
|
|
|
|
class PushHost 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()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
Host::whereIn('status', ['pending', 'error'])->with(['module', 'user'])->chunk(100, function ($hosts) {
|
|
|
|
foreach ($hosts as $host) {
|
2022-11-16 02:29:50 +00:00
|
|
|
$http = Http::module($host->module->api_token, $host->module->url);
|
2022-08-19 09:51:52 +00:00
|
|
|
$host->status = 'running';
|
2022-09-08 16:12:02 +00:00
|
|
|
|
2022-08-19 09:51:52 +00:00
|
|
|
$response = $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-08-27 08:33:08 +00:00
|
|
|
// 检测是否有价格
|
2022-08-27 13:49:32 +00:00
|
|
|
if (isset($response_json['data']['price'])) {
|
|
|
|
$host->price = $response_json['data']['price'];
|
2022-08-27 08:33:08 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|