EditAmpDomainBt.php
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace App\Jobs;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Project;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class EditAmpDomainBt implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1; // 可配置任务重试次数
protected $domain_id;
/**
* Create a new job instance.
*
* @param $domain_id
*/
public function __construct($domain_id)
{
$this->domain_id = $domain_id;
}
/**
* Execute the job.
*
* @return bool
*/
public function handle()
{
//获取域名数据
$domain_model = new DomainInfo();
$domain_info = $domain_model->read(['id' => $this->domain_id]);
if ($domain_info === false) {
return $this->output($domain_info['domain'] . ':获取域名数据失败');
}
//获取项目数据
$project_model = new Project();
$project_info = $project_model->read(['id' => $domain_info['project_id']], 'serve_id');
if ($project_info === false) {
return $this->output($domain_info['domain'] . ':获取项目数据失败');
}
//获取服务器数据
$serverIpModel = new ServersIp();
$serversIpInfo = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id']);
if ($serversIpInfo === false) {
return $this->output($domain_info['domain'] . ':获取服务器数据失败');
}
$serverModel = new Servers();
$serverInfo = $serverModel->read(['id' => $serversIpInfo['servers_id']], ['init_domain']);
if ($serverInfo === false) {
return $this->output($domain_info['domain'] . ':获取服务器数据失败');
}
//编辑amp站
$api_url_amp = 'http://' . $serverInfo['init_domain'] . '/api/createSiteAmp';
$api_param_amp = [
'domain' => $domain_info['domain'],
'not_allow_country' => $domain_info['not_allow_country'],
'not_allow_ip' => $domain_info['not_allow_ip'],
'is_redirect' => $domain_info['is_redirect']
];
if ($domain_info['amp_type'] == 2) {
$api_param_amp['private_key'] = $domain_info['amp_private_key'];
$api_param_amp['cert'] = $domain_info['amp_private_cert'];
}
try {
$rs_amp = HttpUtils::get($api_url_amp, $api_param_amp);
$rs_amp = json_decode($rs_amp, true);
if (isset($rs_amp['status']) && $rs_amp['status'] == 200) {
$this->output($domain_info['domain'] . ':amp站编辑成功');
} else {
$this->output($domain_info['domain'] . ':amp站编辑失败,原因:' . ($rs_amp['message'] ?? ''));
}
} catch (\Exception | GuzzleException $e_amp) {
$this->output($domain_info['domain'] . ':amp站编辑失败,原因:' . $e_amp->getMessage());
}
return true;
}
/**
* 输出处理日志
* @param $message
* @return bool
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
public function failed(\Exception $exception)
{
return $this->output($exception->getMessage());
}
}