作者 zhl

优化日志

@@ -40,8 +40,7 @@ class AiBlogTask extends Command @@ -40,8 +40,7 @@ class AiBlogTask extends Command
40 protected $signature = 'save_ai_blog'; 40 protected $signature = 'save_ai_blog';
41 41
42 public $updateProject = [];//需更新的列表 42 public $updateProject = [];//需更新的列表
43 -  
44 - 43 + public $projectSetting = [];
45 44
46 /** 45 /**
47 * The console command description. 46 * The console command description.
@@ -50,10 +49,14 @@ class AiBlogTask extends Command @@ -50,10 +49,14 @@ class AiBlogTask extends Command
50 */ 49 */
51 protected $description = '查询ai_blog是否已经生成'; 50 protected $description = '查询ai_blog是否已经生成';
52 51
  52 + /**
  53 + * @return bool
  54 + * @throws \Exception
  55 + */
53 public function handle(){ 56 public function handle(){
54 while (true){ 57 while (true){
55 //获取任务id 58 //获取任务id
56 - $task_id = $this->getTaskId(2); 59 + $task_id = $this->getTaskId();
57 if(empty($task_id)){ 60 if(empty($task_id)){
58 sleep(300); 61 sleep(300);
59 continue; 62 continue;
@@ -64,16 +67,15 @@ class AiBlogTask extends Command @@ -64,16 +67,15 @@ class AiBlogTask extends Command
64 } 67 }
65 68
66 /** 69 /**
67 - * @remark :请求方法  
68 - * @name :sendRequest  
69 - * @author :lyh  
70 - * @method :post  
71 - * @time :2025/3/19 16:48 70 + * 请求方法
  71 + * @param $task_id
  72 + * @return bool
  73 + * @throws \Exception
72 */ 74 */
73 public function sendRequest($task_id){ 75 public function sendRequest($task_id){
74 $aiBlogTaskModel = new AiBlogTaskModel(); 76 $aiBlogTaskModel = new AiBlogTaskModel();
75 $item = $aiBlogTaskModel->read(['id'=>$task_id]); 77 $item = $aiBlogTaskModel->read(['id'=>$task_id]);
76 - echo date('Y-m-d H:i:s') . '开始->任务id:' . $item['task_id'] . PHP_EOL; 78 + $this->output('start:project ID: ' . $item['project_id'] . ', task ID: ' . $task_id);
77 //获取配置 79 //获取配置
78 $aiSettingInfo = $this->getSetting($item['project_id']); 80 $aiSettingInfo = $this->getSetting($item['project_id']);
79 $aiBlogService = new AiBlogService(); 81 $aiBlogService = new AiBlogService();
@@ -82,7 +84,9 @@ class AiBlogTask extends Command @@ -82,7 +84,9 @@ class AiBlogTask extends Command
82 $aiBlogService->task_id = $item['task_id']; 84 $aiBlogService->task_id = $item['task_id'];
83 //拉取文章数据 85 //拉取文章数据
84 $result = $aiBlogService->getDetail(); 86 $result = $aiBlogService->getDetail();
85 - if(!isset($result['status']) || ($result['status'] != 200)){ 87 +
  88 + // 获取失败
  89 + if(empty($result['status']) || $result['status'] != 200){
86 if($item['sort'] < 5){ 90 if($item['sort'] < 5){
87 $aiBlogTaskModel->edit(['sort'=>$item['sort'] + 1],['id'=>$item['id']]); 91 $aiBlogTaskModel->edit(['sort'=>$item['sort'] + 1],['id'=>$item['id']]);
88 }else{ 92 }else{
@@ -90,15 +94,16 @@ class AiBlogTask extends Command @@ -90,15 +94,16 @@ class AiBlogTask extends Command
90 // 钉钉通知 94 // 钉钉通知
91 $dingService = new DingService(); 95 $dingService = new DingService();
92 $body = [ 96 $body = [
93 - 'keyword' => 'AI_BLOG生成错误',  
94 - 'msg' => '任务id:'.$item['task_id'].'拉取数据失败'.PHP_EOL.'返回信息:'.json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 97 + 'keyword' => 'AI_BLOG获取失败',
  98 + 'msg' => '任务ID:' . $item['task_id'] . PHP_EOL . '返回信息:' . json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
95 'isAtAll' => false, // 是否@所有人 99 'isAtAll' => false, // 是否@所有人
96 ]; 100 ];
97 $dingService->handle($body); 101 $dingService->handle($body);
98 } 102 }
99 - echo date('Y-m-d H:i:s'). '错误信息:'.json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).PHP_EOL; 103 + $this->output('error: 数据获取失败,status:' . $result['status'] . ', message: ' . ($result['message'] ?? 'null'));
100 return false; 104 return false;
101 } 105 }
  106 +
102 //保存当前项目ai_blog数据 107 //保存当前项目ai_blog数据
103 ProjectServer::useProject($item['project_id']); 108 ProjectServer::useProject($item['project_id']);
104 $aiBlogModel = new AiBlog(); 109 $aiBlogModel = new AiBlog();
@@ -108,11 +113,12 @@ class AiBlogTask extends Command @@ -108,11 +113,12 @@ class AiBlogTask extends Command
108 $dingService = new DingService(); 113 $dingService = new DingService();
109 $body = [ 114 $body = [
110 'keyword' => 'AI_BLOG生成错误', 115 'keyword' => 'AI_BLOG生成错误',
111 - 'msg' => '子任务不存在:'.$item['task_id'], 116 + 'msg' => '任务ID:' . $item['task_id'] . ', 子库获取数据失败, 检查子库数据是否被删除!',
112 'isAtAll' => false, // 是否@所有人 117 'isAtAll' => false, // 是否@所有人
113 ]; 118 ];
114 $dingService->handle($body); 119 $dingService->handle($body);
115 - echo '任务id不存在:'.$item['task_id'].PHP_EOL; 120 +
  121 + $this->output('error: 子库获取数据失败, task id: ' . $task_id);
116 $aiBlogTaskModel->edit(['status'=>9],['id'=>$item['id']]); 122 $aiBlogTaskModel->edit(['status'=>9],['id'=>$item['id']]);
117 DB::disconnect('custom_mysql'); 123 DB::disconnect('custom_mysql');
118 return false; 124 return false;
@@ -128,19 +134,17 @@ class AiBlogTask extends Command @@ -128,19 +134,17 @@ class AiBlogTask extends Command
128 $aiBlogModel->edit(['new_title'=>$result['data']['title'], 'image'=>$result['data']['thumb'], 'text'=>$result['data']['section'], 'author_id'=>$result['data']['author_id'],'seo_title'=>$result['data']['title'],'seo_keyword'=>$result['data']['keyword'],'seo_description'=>$result['data']['description'], 'route'=>$route ,'status'=>$aiBlogModel::STATUS_FINISH], ['task_id'=>$item['task_id']]); 134 $aiBlogModel->edit(['new_title'=>$result['data']['title'], 'image'=>$result['data']['thumb'], 'text'=>$result['data']['section'], 'author_id'=>$result['data']['author_id'],'seo_title'=>$result['data']['title'],'seo_keyword'=>$result['data']['keyword'],'seo_description'=>$result['data']['description'], 'route'=>$route ,'status'=>$aiBlogModel::STATUS_FINISH], ['task_id'=>$item['task_id']]);
129 DB::disconnect('custom_mysql'); 135 DB::disconnect('custom_mysql');
130 $aiBlogTaskModel->edit(['status'=>$aiBlogModel::STATUS_FINISH],['id'=>$item['id']]); 136 $aiBlogTaskModel->edit(['status'=>$aiBlogModel::STATUS_FINISH],['id'=>$item['id']]);
131 - echo date('Y-m-d H:i:s').'结束->任务id:' . $item['task_id'] . PHP_EOL; 137 +
  138 + $this->output('success: task id: ' . $task_id);
132 return true; 139 return true;
133 } 140 }
134 141
135 /** 142 /**
136 - * @remark :获取任务id  
137 - * @name :getTaskId  
138 - * @author :lyh  
139 - * @method :post  
140 - * @time :2025/3/19 16:16  
141 - * @param : 143 + * 获取任务id
  144 + * @param int $finish_at
  145 + * @return mixed
142 */ 146 */
143 - public function getTaskId($finish_at = 1) 147 + public function getTaskId($finish_at = 2)
144 { 148 {
145 $task_id = Redis::rpop('ai_blog_task'); 149 $task_id = Redis::rpop('ai_blog_task');
146 if (empty($task_id)) { 150 if (empty($task_id)) {
@@ -150,7 +154,7 @@ class AiBlogTask extends Command @@ -150,7 +154,7 @@ class AiBlogTask extends Command
150 } 154 }
151 $aiBlogTaskModel = new AiBlogTaskModel(); 155 $aiBlogTaskModel = new AiBlogTaskModel();
152 $finish_at = date('Y-m-d H:i:s', strtotime('-' . $finish_at . ' hour')); 156 $finish_at = date('Y-m-d H:i:s', strtotime('-' . $finish_at . ' hour'));
153 - $ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING,'type'=>$aiBlogTaskModel::TYPE_BLOG,'updated_at'=>['<=',$finish_at]])->pluck('id'); 157 + $ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING, 'type'=>$aiBlogTaskModel::TYPE_BLOG, 'updated_at'=>['<=',$finish_at]])->pluck('id');
154 if(!empty($ids)){ 158 if(!empty($ids)){
155 foreach ($ids as $id) { 159 foreach ($ids as $id) {
156 Redis::lpush('ai_blog_task', $id); 160 Redis::lpush('ai_blog_task', $id);
@@ -162,49 +166,57 @@ class AiBlogTask extends Command @@ -162,49 +166,57 @@ class AiBlogTask extends Command
162 } 166 }
163 167
164 /** 168 /**
165 - * @remark :更新项目作者页面及列表页  
166 - * @name :updateProject  
167 - * @author :lyh  
168 - * @method :post  
169 - * @time :2025/3/4 10:25 169 + * 更新项目作者页面及列表页
  170 + * @param $updateProject
  171 + * @return bool
170 */ 172 */
171 public function updateProject($updateProject){ 173 public function updateProject($updateProject){
172 if(empty($updateProject)){ 174 if(empty($updateProject)){
173 return true; 175 return true;
174 } 176 }
  177 +
175 foreach ($updateProject as $project_id => $author){ 178 foreach ($updateProject as $project_id => $author){
176 ProjectServer::useProject($project_id); 179 ProjectServer::useProject($project_id);
177 $aiSettingInfo = $this->getSetting($project_id); 180 $aiSettingInfo = $this->getSetting($project_id);
  181 + $this->output('sync: list start, project_id: ' . $project_id);
178 $this->updateBlogList($aiSettingInfo); 182 $this->updateBlogList($aiSettingInfo);
  183 + $this->output('sync: list end');
179 //更新作者 184 //更新作者
  185 + $this->output('sync: author start, project_id: ' . $project_id);
180 foreach ($author as $val){ 186 foreach ($author as $val){
181 $this->updateAiBlogAuthor($aiSettingInfo,$val); 187 $this->updateAiBlogAuthor($aiSettingInfo,$val);
182 } 188 }
  189 + $this->output('sync: author end');
183 DB::disconnect('custom_mysql'); 190 DB::disconnect('custom_mysql');
184 $this->curlDelRoute($project_id); 191 $this->curlDelRoute($project_id);
185 } 192 }
  193 +
186 return true; 194 return true;
187 } 195 }
188 196
189 /** 197 /**
190 - * @remark :获取项目配置  
191 - * @name :getSetting  
192 - * @author :lyh  
193 - * @method :post  
194 - * @time :2025/2/14 11:27 198 + * 获取项目配置
  199 + * @param $project_id
  200 + * @return bool
195 */ 201 */
196 - public function getSetting($project_id){ 202 + public function getSetting($project_id)
  203 + {
  204 + $project_setting = $this->projectSetting;
  205 + if (FALSE == empty($project_setting[$project_id]))
  206 + return $project_setting[$project_id];
  207 +
197 $projectAiSettingModel = new ProjectAiSetting(); 208 $projectAiSettingModel = new ProjectAiSetting();
198 $aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]); 209 $aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
  210 +
  211 + $this->projectSetting[$project_id] = $aiSettingInfo;
199 return $aiSettingInfo; 212 return $aiSettingInfo;
200 } 213 }
201 214
202 /** 215 /**
203 - * @remark :更新作者的页面  
204 - * @name :updateAiBlogAuthor  
205 - * @author :lyh  
206 - * @method :post  
207 - * @time :2025/2/21 11:53 216 + * 更新作者的页面
  217 + * @param $aiSettingInfo
  218 + * @param $author_id
  219 + * @return bool
208 */ 220 */
209 public function updateAiBlogAuthor($aiSettingInfo,$author_id){ 221 public function updateAiBlogAuthor($aiSettingInfo,$author_id){
210 if(empty($author_id)){ 222 if(empty($author_id)){
@@ -226,11 +238,9 @@ class AiBlogTask extends Command @@ -226,11 +238,9 @@ class AiBlogTask extends Command
226 } 238 }
227 239
228 /** 240 /**
229 - * @remark :更新列表页  
230 - * @name :updateBlogList  
231 - * @author :lyh  
232 - * @method :post  
233 - * @time :2025/2/26 15:42 241 + * 更新列表页
  242 + * @param $aiSettingInfo
  243 + * @return bool
234 */ 244 */
235 public function updateBlogList($aiSettingInfo){ 245 public function updateBlogList($aiSettingInfo){
236 $aiBlogService = new AiBlogService(); 246 $aiBlogService = new AiBlogService();
@@ -268,55 +278,69 @@ class AiBlogTask extends Command @@ -268,55 +278,69 @@ class AiBlogTask extends Command
268 } 278 }
269 279
270 /** 280 /**
271 - * @remark :通知C端生成界面  
272 - * @name :sendNotice  
273 - * @author :lyh  
274 - * @method :post  
275 - * @time :2025/3/6 11:51 281 + * 通知C端生成界面
  282 + * @param $project_id
  283 + * @return bool
276 */ 284 */
277 public function curlDelRoute($project_id){ 285 public function curlDelRoute($project_id){
278 $domainModel = new DomainInfo(); 286 $domainModel = new DomainInfo();
279 //获取项目域名 287 //获取项目域名
280 $domain = $domainModel->getProjectIdDomain($project_id); 288 $domain = $domainModel->getProjectIdDomain($project_id);
281 - if(!empty($domain)){  
282 - //判断是否是自建站服务器,如果是,不请求C端接口,数据直接入库  
283 - $project_model = new Project();  
284 - $project_info = $project_model->read(['id'=>$project_id],['serve_id']);  
285 - if(!$project_info){  
286 - echo '项目不存在:' . $project_id . PHP_EOL . date('Y-m-d H:i:s');  
287 - return true;  
288 - }  
289 - $serve_ip_model = new ServersIp();  
290 - $serve_ip_info = $serve_ip_model->read(['id'=>$project_info['serve_id']],['servers_id']);  
291 - $servers_id = $serve_ip_info ? $serve_ip_info['servers_id'] : 0;  
292 - if($servers_id == ServerConfig::SELF_SITE_ID){  
293 - //判断是否已有更新进行中  
294 - $notify_model = new Notify();  
295 - $data = [  
296 - 'project_id' => $project_id,  
297 - 'type' => Notify::TYPE_MASTER,  
298 - 'route' => Notify::ROUTE_AI_BLOG,  
299 - 'server_id' => ServerConfig::SELF_SITE_ID,  
300 - 'status' => ['!=',Notify::STATUS_FINISH_SITEMAP]  
301 - ];  
302 - $notify = $notify_model->read($data,['id']); 289 + if (empty($domain)) {
  290 + $this->output('send: 域名不存在, project id: ' . $project_id);
  291 + return true;
  292 + }
303 293
304 - if(!$notify){  
305 - $domain_array = parse_url($domain);  
306 - $data['data'] = Arr::a2s(['domain'=>$domain_array['host'],'url'=>[],'language'=>[]]);  
307 - $data['status'] = Notify::STATUS_INIT;  
308 - $data['sort'] = 2;  
309 - $notify_model->add($data);  
310 - }  
311 - echo '自建站项目:'.$project_id.'更新';  
312 - }else{  
313 - $url = $domain.'api/update_page/?project_id='.$project_id.'&route=7';  
314 - $res = http_get($url);  
315 - echo '通知C端:'.json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).PHP_EOL; 294 +
  295 + //判断是否是自建站服务器,如果是,不请求C端接口,数据直接入库
  296 + $project_model = new Project();
  297 + $project_info = $project_model->read(['id'=>$project_id],['serve_id']);
  298 + if(!$project_info){
  299 + $this->output('send: 项目不存在, project id: ' . $project_id);
  300 + return true;
  301 + }
  302 + $serve_ip_model = new ServersIp();
  303 + $serve_ip_info = $serve_ip_model->read(['id'=>$project_info['serve_id']],['servers_id']);
  304 + $servers_id = $serve_ip_info ? $serve_ip_info['servers_id'] : 0;
  305 + if($servers_id == ServerConfig::SELF_SITE_ID){
  306 + //判断是否已有更新进行中
  307 + $notify_model = new Notify();
  308 + $data = [
  309 + 'project_id' => $project_id,
  310 + 'type' => Notify::TYPE_MASTER,
  311 + 'route' => Notify::ROUTE_AI_BLOG,
  312 + 'server_id' => ServerConfig::SELF_SITE_ID,
  313 + 'status' => ['!=',Notify::STATUS_FINISH_SITEMAP]
  314 + ];
  315 + $notify = $notify_model->read($data,['id']);
  316 +
  317 + if(!$notify){
  318 + $domain_array = parse_url($domain);
  319 + $data['data'] = Arr::a2s(['domain'=>$domain_array['host'],'url'=>[],'language'=>[]]);
  320 + $data['status'] = Notify::STATUS_INIT;
  321 + $data['sort'] = 2;
  322 + $notify_model->add($data);
316 } 323 }
  324 + $this->output('send: 自建站项目, project id: ' . $project_id);
317 }else{ 325 }else{
318 - echo '域名不存在:' . $project_id . PHP_EOL . date('Y-m-d H:i:s'); 326 + $url = $domain . 'api/update_page/?project_id='.$project_id.'&route=7';
  327 + $res = http_get($url);
  328 + $this->output('notify: project id: ' . $project_id . ', result: ' . json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
319 } 329 }
  330 +
  331 + return true;
  332 + }
  333 +
  334 + /**
  335 + * 输入日志
  336 + * @param $message
  337 + * @return bool
  338 + */
  339 + public function output($message)
  340 + {
  341 + $message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
  342 + echo $message;
  343 + file_put_contents(storage_path('logs/AiBlog/') . date('Ymd') . '.log', $message, FILE_APPEND);
320 return true; 344 return true;
321 } 345 }
322 } 346 }