Lae/app/Jobs/Host/HostJob.php
iVampireSP.com 9bb969577e
增加 模块异常回应的安全处理
自动将模块切换为维护模式并防止意外删除
2023-01-19 03:04:16 +08:00

72 lines
1.6 KiB
PHP

<?php
namespace App\Jobs\Host;
use App\Models\Host as HostModel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
// use Illuminate\Contracts\Queue\ShouldBeUnique;
class HostJob implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
public HostModel $host;
public string $type;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(HostModel $host, $type = 'post')
{
//
$this->host = $host;
$this->type = $type;
}
/**
* Execute the job.
*
* @return void
* @noinspection PhpUndefinedVariableInspection
*/
public function handle(): void
{
$host = $this->host;
$host->load(['module']);
switch ($this->type) {
case 'patch':
$response = $host->module->http()->patch('hosts/' . $host->id, $host->toArray());
break;
case 'post':
$response = $host->module->http()->post('hosts', $host->toArray());
break;
case 'delete':
$response = $host->module->baseRequest('delete', 'hosts/' . $host->id);
// if successful
if ($response['status'] === 404) {
$host->delete();
}
break;
}
if ($this->type !== 'delete') {
if (!$response->successful()) {
$host->status = 'error';
}
$host->save();
}
}
}