SyncSubmitTask.php
4.0 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
<?php
namespace App\Console\Commands\Sync;
use App\Exceptions\InquiryFilterException;
use App\Models\Inquiry\InquiryRelateDomain;
use App\Models\Project\Project;
use App\Models\SyncSubmitTask\SyncSubmitTask as SyncSubmitTaskModel;
use App\Services\SyncSubmitTaskService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
/**
*
* Class SyncSubmitTask
* @package App\Console\Commands
* @author zbj
* @date 2023/11/28
*/
class SyncSubmitTask extends Command
{
protected $signature = 'sync_submit_task';
protected $description = '询盘、访问异步任务';
public function handle()
{
while (true) {
$task_id = Redis::rpop('sync_submit_task');
if(!$task_id){
sleep(1);
continue;
}
$this->output('任务' . $task_id . '开始');
$time = time();
DB::enableQueryLog(); //启用查询日志
//清除之前的查询日志
DB::flushQueryLog();
$task_info = SyncSubmitTaskModel::find($task_id);
if (empty($task_info) || $task_info->status !=3) {
$this->output('任务不存在或者已执行');
continue;
}
try {
//有globalso-domain时,用globalso-domain,兼容白帽版的-海龙
if(!empty($task_info['data']['data']['globalso-domain'])){
$data = $task_info['data'];
$data['domain'] = $task_info['data']['data']['globalso-domain'];
$task_info['data'] = $data;
}
$project = Project::getProjectByDomain($task_info['data']['domain'] ?? '');
if(!$project){
//是否有关联的域名
$relate_domain = InquiryRelateDomain::getRelateDomain($task_info['data']['domain'] ?? '');
if(!$relate_domain){
throw new \Exception('项目不存在1');
}
$project = Project::getProjectByDomain($relate_domain);
if(!$project){
throw new \Exception('项目不存在2');
}
}
$task_info->project_id = $project->id;
SyncSubmitTaskService::handler($task_info, '', $relate_domain??'');
$task_info->status = 1;
$task_info->save();
$this->output('任务完成');
} catch (InquiryFilterException $e) {
$task_info->status = 1;
$task_info->is_filtered = 1;
$task_info->remark = $e->getMessage();
$task_info->save();
$this->output('任务完成' . $e->getMessage());
} catch (\Exception $e) {
$task_info->retry = $task_info->retry + 1;
if ($task_info->retry >= 3) {
$task_info->status = 2;
$task_info->remark = Str::substr($e->getMessage(), 0, 200);
} else {
Redis::lpush('sync_submit_task', $task_id);
}
$task_info->save();
Log::channel('inquiry')->error($task_id . '处理失败', [$e->getMessage(), $e->getFile(), $e->getLine()]);
$this->output('任务失败:' . $e->getMessage());
}
$use_time = time() - $time;
if($use_time > 1){
//数据库查询
$this->output('任务用时:' .$use_time . ' | ' . json_encode(DB::getQueryLog(),JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}
//清除之前的查询日志
DB::flushQueryLog();
}
}
/**
* 输出处理日志
*/
public function output($message): bool
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
}