2022-08-23 09:36:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Remote;
|
|
|
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
|
|
use App\Models\Host as HostModel;
|
|
|
|
|
|
|
|
class Host implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public HostModel $host;
|
|
|
|
public string $type;
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-09-03 06:01:51 +00:00
|
|
|
public function __construct($host, $type = 'post')
|
2022-08-23 09:36:10 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
$this->host = $host;
|
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
//
|
2022-09-03 06:01:51 +00:00
|
|
|
|
|
|
|
// dd($this->host);
|
|
|
|
|
|
|
|
// $host = HostModel::find($this->host);
|
|
|
|
$host = $this->host;
|
|
|
|
$host->load(['module']);
|
|
|
|
|
|
|
|
$http = Http::remote($host->module->api_token, $host->module->url);
|
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
switch ($this->type) {
|
2022-08-29 10:59:32 +00:00
|
|
|
case 'patch':
|
2022-09-03 06:01:51 +00:00
|
|
|
$response = $http->patch('hosts/' . $host->id, $host->toArray());
|
2022-08-23 09:36:10 +00:00
|
|
|
break;
|
|
|
|
case 'post':
|
2022-09-03 06:01:51 +00:00
|
|
|
$response = $http->post('hosts', $host->toArray());
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'delete':
|
2022-09-03 06:01:51 +00:00
|
|
|
$response = $http->delete('hosts/' . $host->id);
|
|
|
|
|
2022-09-03 10:55:01 +00:00
|
|
|
if ($response->status() === 404) {
|
|
|
|
$host->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2022-09-03 06:01:51 +00:00
|
|
|
// if response code is 404
|
|
|
|
// if ($response->successful() || $response->failed()) {
|
|
|
|
// $host->delete();
|
|
|
|
// }
|
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
// if success
|
2022-09-03 06:01:51 +00:00
|
|
|
// if ($response->successful()) {
|
|
|
|
// $host->delete();
|
|
|
|
// }
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$response->successful()) {
|
2022-09-03 06:01:51 +00:00
|
|
|
$host->status = 'error';
|
2022-08-23 09:36:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 06:01:51 +00:00
|
|
|
$host->save();
|
2022-08-23 09:36:10 +00:00
|
|
|
}
|
|
|
|
}
|