瀏覽代碼

Merge branch 'wyp/1024-xxl-error' of Server/long-article-recommend into master

wangyunpeng 8 月之前
父節點
當前提交
23af50e012

+ 33 - 28
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/XxlJobService.java

@@ -30,6 +30,7 @@ import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
@@ -154,25 +155,37 @@ public class XxlJobService {
                     }
                 }
             }
-            if (CollectionUtil.isNotEmpty(saveList)) {
-                List<String> rootSourceIds = saveList.stream().map(LongArticlesRootSourceId::getRootSourceId).distinct().collect(Collectors.toList());
-                List<LongArticlesRootSourceId> existList = longArticlesRootSourceIdRepository.getByRootSourceIdIn(rootSourceIds);
-                for (LongArticlesRootSourceId existItem : existList) {
-                    saveList.removeIf(item -> item.getRootSourceId().equals(existItem.getRootSourceId()));
-                }
-                if (CollectionUtil.isNotEmpty(saveList)) {
-                    crawlerBaseMapper.batchInsertLongArticlesRootSourceId(saveList);
-                }
-            }
+            saveLongArticlesRootSourceId(saveList);
+        } catch (DuplicateKeyException e) {
+            log.error("Error processCrawlerEachData: {}", JSONObject.toJSONString(longArticlesVideo), e);
+            String errMsg = e.getMessage().substring(0, e.getMessage().indexOf("### The error"));
+            sendFeishuProcessCrawlerEachData("processCrawlerEachData", longArticlesVideo.getTraceId(), errMsg);
         } catch (Exception e) {
             log.error("Error processCrawlerEachData: {}", JSONObject.toJSONString(longArticlesVideo), e);
-            FeishuMessageSender.sendWebHookMessage(FeishuRobotIdEnum.JOB.getRobotId(),
-                    "processCrawlerEachData 异常\n"
-                            + "异常数据: " + longArticlesVideo.getTraceId() + "\n"
-                            + "错误信息:" + e.getMessage());
+            sendFeishuProcessCrawlerEachData("processCrawlerEachData", longArticlesVideo.getTraceId(), e.getMessage());
+        }
+    }
+
+    private void saveLongArticlesRootSourceId(List<LongArticlesRootSourceId> saveList) {
+        if (CollectionUtil.isNotEmpty(saveList)) {
+            List<String> rootSourceIds = saveList.stream().map(LongArticlesRootSourceId::getRootSourceId).distinct().collect(Collectors.toList());
+            List<LongArticlesRootSourceId> existList = longArticlesRootSourceIdRepository.getByRootSourceIdIn(rootSourceIds);
+            for (LongArticlesRootSourceId existItem : existList) {
+                saveList.removeIf(item -> item.getRootSourceId().equals(existItem.getRootSourceId()));
+            }
+            if (CollectionUtil.isNotEmpty(saveList)) {
+                crawlerBaseMapper.batchInsertLongArticlesRootSourceId(saveList);
+            }
         }
     }
 
+    private void sendFeishuProcessCrawlerEachData(String method, String traceId, String errMsg) {
+        FeishuMessageSender.sendWebHookMessage(FeishuRobotIdEnum.JOB.getRobotId(),
+                method + " 异常\n"
+                        + "异常数据TraceId: " + traceId + "\n"
+                        + "错误信息:" + errMsg);
+    }
+
     @XxlJob("migrateArticleRootSourceId")
     public ReturnT<String> migrateArticleRootSourceId(String param) {
         try {
@@ -212,22 +225,14 @@ public class XxlJobService {
                 saveItem.setVideoId(jsonNode.getLong("videoId"));
                 saveList.add(saveItem);
             }
-            if (CollectionUtil.isNotEmpty(saveList)) {
-                List<String> rootSourceIds = saveList.stream().map(LongArticlesRootSourceId::getRootSourceId).distinct().collect(Collectors.toList());
-                List<LongArticlesRootSourceId> existList = longArticlesRootSourceIdRepository.getByRootSourceIdIn(rootSourceIds);
-                for (LongArticlesRootSourceId existItem : existList) {
-                    saveList.removeIf(item -> item.getRootSourceId().equals(existItem.getRootSourceId()));
-                }
-                if (CollectionUtil.isNotEmpty(saveList)) {
-                    crawlerBaseMapper.batchInsertLongArticlesRootSourceId(saveList);
-                }
-            }
+            saveLongArticlesRootSourceId(saveList);
+        } catch (DuplicateKeyException e) {
+            log.error("Error processArticleEachData: {}", JSONObject.toJSONString(longArticlesMatchVideo), e);
+            String errMsg = e.getMessage().substring(0, e.getMessage().indexOf("### The error"));
+            sendFeishuProcessCrawlerEachData("processArticleEachData", longArticlesMatchVideo.getTraceId(), errMsg);
         } catch (Exception e) {
             log.error("Error processArticleEachData: {}", JSONObject.toJSONString(longArticlesMatchVideo), e);
-            FeishuMessageSender.sendWebHookMessage(FeishuRobotIdEnum.JOB.getRobotId(),
-                    "processArticleEachData 异常\n"
-                            + "异常数据: " + longArticlesMatchVideo.getTraceId() + "\n"
-                            + "错误信息:" + e.getMessage());
+            sendFeishuProcessCrawlerEachData("processArticleEachData", longArticlesMatchVideo.getTraceId(), e.getMessage());
         }
     }