作者 lyh

GXgeo设置

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