作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !2331
@@ -46,51 +46,54 @@ class GeoQuestionRes extends Command @@ -46,51 +46,54 @@ class GeoQuestionRes extends Command
46 sleep(300); 46 sleep(300);
47 continue; 47 continue;
48 } 48 }
49 - $task = GeoQuestion::where(['id' => $task_id, 'status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->first();  
50 - if (empty($task)) { 49 + echo date('Y-m-d H:i:s').'执行的任务id:'.$task_id.PHP_EOL;
  50 + $geoQuestionModel = new GeoQuestion();
  51 + $taskInfo = $geoQuestionModel->read(['id'=>$task_id]);
  52 + if ($taskInfo === false) {
  53 + $this->output('当前任务详情为空!');
51 continue; 54 continue;
52 } 55 }
53 - dd($task);  
54 - $project = Project::select(['geo_status', 'geo_frequency'])->where(['id' => $task->project_id])->first();  
55 - if (empty($project->get_status)) {  
56 - $task->status = GeoQuestion::STATUS_CLOSE;  
57 - $task->save(); 56 + $projectModel = new Project();
  57 + $projectInfo = $projectModel->read(['id' => $taskInfo['project_id']],['geo_status', 'geo_frequency']);
  58 + if ($projectInfo === false) {
  59 + $this->output('未获取到项目详情!');
  60 + $geoQuestionModel->edit(['status'=>$geoQuestionModel::STATUS_CLOSE],['id'=>$task_id]);
58 continue; 61 continue;
59 } 62 }
60 - if ((empty($task->question) || FALSE == is_array($task->question)) || (empty($task->keywords) && empty($task->url))) { 63 + if(empty($taskInfo['question']) || empty($taskInfo['keywords']) || empty($taskInfo['url'])){
61 $this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!'); 64 $this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!');
62 - $task->status = GeoQuestion::STATUS_CLOSE;  
63 - $task->save(); 65 + $geoQuestionModel->edit(['status'=>$geoQuestionModel::STATUS_CLOSE],['id'=>$task_id]);
64 continue; 66 continue;
65 } 67 }
66 - $platforms = GeoPlatform::where(['status' => GeoPlatform::STATUS_ON])->get();  
67 - if ($platforms->isEmpty) { 68 + $geoPlatformModel = new GeoPlatform();
  69 + $platformsArr = $geoPlatformModel->selectField(['status' => GeoPlatform::STATUS_ON],'en_name');
  70 + if (empty($platformsArr)) {
68 $this->output('未设置AI模型!'); 71 $this->output('未设置AI模型!');
69 continue; 72 continue;
70 } 73 }
71 $geo_service = new GeoService(); 74 $geo_service = new GeoService();
72 $geoResultModel = new GeoQuestionResult(); 75 $geoResultModel = new GeoQuestionResult();
73 $geoLogModel = new GeoQuestionLog(); 76 $geoLogModel = new GeoQuestionLog();
74 - foreach ($task->question as $question) { 77 + foreach ($taskInfo['question'] as $question) {
75 $error_num = 0; 78 $error_num = 0;
76 - foreach ($platforms as $platform) { 79 + foreach ($platformsArr as $platform) {
77 // 设置重试, 有的平台不一定能正常获取到数据 80 // 设置重试, 有的平台不一定能正常获取到数据
78 GET_RESULT: 81 GET_RESULT:
79 $error_num++; 82 $error_num++;
80 try { 83 try {
81 - if ($platform->en_name == 'Google AI Overview') { 84 + if ($platform == 'Google AI Overview') {
82 // overview 数据结构不确定, 需要单独处理数据 85 // overview 数据结构不确定, 需要单独处理数据
83 $data = $geo_service->getGooglePlatformResult($question); 86 $data = $geo_service->getGooglePlatformResult($question);
84 $result = $this->dealGoogleData($data); 87 $result = $this->dealGoogleData($data);
85 } else { 88 } else {
86 - $result = $geo_service->getAiPlatformResult($question, $platform->en_name); 89 + $result = $geo_service->getAiPlatformResult($question, $platform);
87 } 90 }
88 if (empty($result['text'])){ 91 if (empty($result['text'])){
89 goto GET_RESULT; 92 goto GET_RESULT;
90 } 93 }
91 } catch (\Exception $e) { 94 } catch (\Exception $e) {
92 $this->output('task id:' . $task_id . ', question: ' . $question . ', platform: ' . $question . ', error: ' . $e->getMessage()); 95 $this->output('task id:' . $task_id . ', question: ' . $question . ', platform: ' . $question . ', error: ' . $e->getMessage());
93 - if ($error_num < 5) { 96 + if ($error_num < 2) {
94 goto GET_RESULT; 97 goto GET_RESULT;
95 } 98 }
96 continue; 99 continue;
@@ -106,25 +109,25 @@ class GeoQuestionRes extends Command @@ -106,25 +109,25 @@ class GeoQuestionRes extends Command
106 // 命中关键词和路由 109 // 命中关键词和路由
107 $hit_keyword = $hit_url = []; 110 $hit_keyword = $hit_url = [];
108 $hit = 0; 111 $hit = 0;
109 - if ($task->keywords) {  
110 - $pattern = '/(' . implode('|', array_map('preg_quote', $task->keywords)) . ')/i'; 112 + if ($taskInfo['keywords']) {
  113 + $pattern = '/(' . implode('|', array_map('preg_quote', $taskInfo['keywords'])) . ')/i';
111 if (preg_match($pattern, $hit_string, $matches)) { 114 if (preg_match($pattern, $hit_string, $matches)) {
112 $hit_keyword = $matches[0]; 115 $hit_keyword = $matches[0];
113 $hit++; 116 $hit++;
114 } 117 }
115 } 118 }
116 - if ($task->url) {  
117 - $pattern = '/(' . implode('|', array_map('preg_quote', $task->url)) . ')/i'; 119 + if ($taskInfo['url']) {
  120 + $pattern = '/(' . implode('|', array_map('preg_quote', $taskInfo['url'])) . ')/i';
118 if (preg_match($pattern, $hit_string, $matches)) { 121 if (preg_match($pattern, $hit_string, $matches)) {
119 $hit_url = $matches[0]; 122 $hit_url = $matches[0];
120 $hit++; 123 $hit++;
121 } 124 }
122 } 125 }
123 // 保存数据结果 126 // 保存数据结果
124 - $geo_result = GeoQuestionResult::where(['project_id' => $task['project_id'], 'question_id' => $task['id'], 'platform' => $platform, 'question' => $question])->first(); 127 + $geo_result = $geoResultModel->read(['project_id' => $taskInfo['project_id'], 'question_id' => $task_id, 'platform' => $platform, 'question' => $question],['id']);
125 $save_data = [ 128 $save_data = [
126 - 'project_id' => $task->project_id,  
127 - 'question_id' => $task->id, 129 + 'project_id' => $taskInfo['project_id'],
  130 + 'question_id' => $task_id,
128 'type' => $task->type ?? GeoQuestion::TYPE_BRAND, 131 'type' => $task->type ?? GeoQuestion::TYPE_BRAND,
129 'platform' => $platform, 132 'platform' => $platform,
130 'question' => $question, 133 'question' => $question,
@@ -133,7 +136,7 @@ class GeoQuestionRes extends Command @@ -133,7 +136,7 @@ class GeoQuestionRes extends Command
133 'text' => json_encode($result,true), 136 'text' => json_encode($result,true),
134 'hit' => $hit 137 'hit' => $hit
135 ]; 138 ];
136 - if(empty($geo_result)){ 139 + if($geo_result === false){
137 $geoResultModel->addReturnId($save_data); 140 $geoResultModel->addReturnId($save_data);
138 }else{ 141 }else{
139 $geoResultModel->edit($save_data, ['id' => $geo_result->id]); 142 $geoResultModel->edit($save_data, ['id' => $geo_result->id]);
@@ -142,9 +145,8 @@ class GeoQuestionRes extends Command @@ -142,9 +145,8 @@ class GeoQuestionRes extends Command
142 $geoLogModel->addReturnId($data); 145 $geoLogModel->addReturnId($data);
143 } 146 }
144 } 147 }
145 - $task->current_time = date('Y-m-d');  
146 - $task->next_time = date('Y-m-d', strtotime('+' . $project->geo_frequency . ' days'));  
147 - $task->save(); 148 + $next_time = date('Y-m-d', strtotime('+' . ($projectInfo['geo_frequency'] ?? 3) . ' days'));
  149 + $geoQuestionModel->edit(['current_time'=>date('Y-m-d'),'next_time'=>$next_time],['id'=>$task_id]);
148 } 150 }
149 return true; 151 return true;
150 } 152 }
@@ -209,7 +211,7 @@ class GeoQuestionRes extends Command @@ -209,7 +211,7 @@ class GeoQuestionRes extends Command
209 $task_id = Redis::rpop($key); 211 $task_id = Redis::rpop($key);
210 if(empty($task_id)){ 212 if(empty($task_id)){
211 $questionModel = new GeoQuestion(); 213 $questionModel = new GeoQuestion();
212 - $ids = $questionModel->selectField(['status'=>1,'next_time'=>['<=',date('Y-m-d')]],'id'); 214 + $ids = $questionModel->selectField(['status'=>$questionModel::STATUS_OPEN,'next_time'=>['<=',date('Y-m-d')]],'id');
213 if(!empty($ids)){ 215 if(!empty($ids)){
214 foreach ($ids as $id) { 216 foreach ($ids as $id) {
215 Redis::lpush($key, $id); 217 Redis::lpush($key, $id);