EventExpend.php
7.1 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2022/11/05
* Time: 14:11
*/
namespace App\Console\Commands;
use App\Models\BtEvents;
use App\Repositories\BtRepository;
use App\Repositories\ToolRepository;
use Illuminate\Console\Command;
/**
* Class EventExpend
* @package App\Console\Commands
*/
class EventExpend extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'event:expend';
/**
* The console command description.
*
* @var string
*/
protected $description = '消费提交事件';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return bool
*/
public function handle()
{
while (true) {
$start_at = date('Y-m-d H:i:s');
$events = BtEvents::where('status', 0)->where('times', '<', BtEvents::MAX_TRIES_TIMES)->where('start_at', '<', $start_at)->orderBy('id', 'asc')->limit(10)->get();
// 没有需要处理的数据
if ($events->isEmpty()) {
echo $start_at . ', empty event.' . PHP_EOL;
sleep(30);
} else {
foreach ($events as $val) {
}
}
}
return true;
}
public function expend($data)
{
foreach ($data as $val) {
$param = json_decode($val->param, true);
switch ($val->type) {
case BtEvents::TYPE_CREATE_SITE:
$result = $this->createSiteEvent($val->id, $param);
break;
case BtEvents::TYPE_DELETE_SITE:
$result = $this->deleteSiteEvent($val->id, $param);
break;
default:
$result = false;
break;
}
}
}
/**
* 创建站点
* @param $id
* @param $param
* @return bool
*/
public function createSiteEvent($id, $param)
{
if (empty($param) || FALSE == is_array($param))
return false;
$callback = $param['callback'] ?? '';
$domain = $param['domain'] ?? '';
$ssl_open = empty($param['ssl_open']) ? 1 : 0;
$ssl_auto = empty($param['ssl_auto']) ? 1 : 0;
$ssl_auto_day = $param['ssl_auto_day'] ?? 10;
$ssl_auto_day = $ssl_auto_day > 15 ? 15 : max($ssl_auto_day, 5);
if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL)) {
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_message'] = 'domain信息错误';
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, BtEvents::STATUS_FINISH, 'domain信息错误');
}
try {
$flag = app(BtRepository::class)->createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day);
$status = $flag ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR;
$note = $flag ? '' : '创建站点失败';
} catch (\Exception $e) {
$status = BtEvents::STATUS_ERROR;
$note = $e->getMessage();
}
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = $status == BtEvents::STATUS_FINISH ? 200 : 400;
$param['result_message'] = $note;
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, $status, $note);
}
/**
* 删除站点
* @param $id
* @param $param
* @return bool
*/
public function deleteSiteEvent($id, $param)
{
if (empty($param) || FALSE == is_array($param))
return false;
$callback = $param['callback'] ?? '';
$domain = $param['domain'] ?? '';
if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL)) {
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = 400;
$param['result_message'] = 'domain信息错误';
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, BtEvents::STATUS_FINISH, 'domain信息错误');
}
try {
$flag = app(BtRepository::class)->deleteBtSite($domain);
$status = $flag ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR;
$note = $flag ? '' : '删除站点失败';
} catch (\Exception $e) {
$status = BtEvents::STATUS_ERROR;
$note = $e->getMessage();
}
if (FALSE == empty($callback) && filter_var($callback, FILTER_VALIDATE_URL)) {
$param['result_status'] = $status == BtEvents::STATUS_FINISH ? 200 : 400;
$param['result_message'] = $note;
BtEvents::createBtEvent(BtEvents::TYPE_EVENT_CALLBACK, json_encode($param));
}
return $this->setEvent($id, $status, $note);
}
/**
* 回调信息
* @param $id
* @param $param
* @return bool
*/
public function callbackEvent($id, $param)
{
if (empty($param) || FALSE == is_array($param))
return false;
$callback = $param['callback'] ?? '';
if (empty($callback) || FALSE == filter_var($callback, FILTER_VALIDATE_URL))
return $this->setEvent($id, BtEvents::STATUS_FINISH, 'callback信息错误');
$status = $param['result_status'];
$message = $param['result_message'];
unset($param['result_status']);
unset($param['result_message']);
$array = [
'status' => $status,
'message' => $message,
'param' => $param
];
list($code, $result) = app(ToolRepository::class)->curlRequest($callback, $array);
$status = $code == 200 ? BtEvents::STATUS_FINISH : BtEvents::STATUS_ERROR;
return $this->setEvent($id, $status, $result);
}
/**
* 更新任务状态
* @param $id
* @param $status
* @param string $note
* @return bool
*/
public function setEvent($id, $status, $note = '')
{
$event = BtEvents::where(['id' => $id])->first();
if (empty($event))
return false;
$times = $event->times + 1;
if ($status == BtEvents::STATUS_ERROR && $times < BtEvents::MAX_TRIES_TIMES) {
$status = BtEvents::STATUS_INIT;
// 第一次执行失败以后, 5分钟后再执行一次; 第二次执行失败以后, 10分钟后再执行一次 0->5->15
if ($times == 1)
$event->start_at = date('Y-m-d H:i:s', time() + 300);
if ($times == 2)
$event->start_at = date('Y-m-d H:i:s', time() + 600);
}
$event->stauts = $status;
$event->times = $times;
$event->note = $note;
$event->save();
return true;
}
}