Explorar el Código

增加文章匹配结果状态

xueyiming hace 5 meses
padre
commit
c1576f64f6

+ 26 - 0
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/common/enums/ContentResultStatusEnum.java

@@ -0,0 +1,26 @@
+package com.tzld.piaoquan.longarticle.common.enums;
+
+public enum ContentResultStatusEnum {
+
+    DEFAULT(0, "待处理"),
+    SUCCESS(1, "处理成功"),
+    FAIL(2, "处理失败");
+
+    private final int status;
+    private final String message;
+
+    ContentResultStatusEnum(int status, String message) {
+        this.status = status;
+        this.message = message;
+    }
+
+    public int getStatus() {
+        return status;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+
+}

+ 1 - 2
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/local/impl/CrawlerVideoServiceImpl.java

@@ -56,8 +56,7 @@ public class CrawlerVideoServiceImpl {
     }};
 
     public boolean addCrawlerVideo(String contentId, String rootContentId, LongArticlesText kimiText) {
-        boolean b = existCrawlerVideo(contentId);
-        if (b) {
+        if (existCrawlerVideo(contentId)) {
             return true;
         }
         //查询原contentId,如果已经爬取到视频,则不再爬取

+ 66 - 32
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/local/impl/MatchVideoServiceImpl.java

@@ -2,6 +2,7 @@ package com.tzld.piaoquan.longarticle.service.local.impl;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.longarticle.common.enums.ContentResultStatusEnum;
 import com.tzld.piaoquan.longarticle.common.enums.ContentStatusEnum;
 import com.tzld.piaoquan.longarticle.common.enums.PublicFlagEnum;
 import com.tzld.piaoquan.longarticle.component.RedisLock;
@@ -24,10 +25,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.UUID;
+import java.util.*;
 import java.util.concurrent.*;
 
 @Slf4j
@@ -47,7 +45,7 @@ public class MatchVideoServiceImpl {
 
     private static final String KIMI_FAIL_COUNT_KEY = "kimi_count_key_%s";
 
-    private static final String FAIL_KEY = "fail_key_%s";
+    private static final String EXIST_RESULT_KEY = "exist_result_key_%s";
 
 
     @Autowired
@@ -230,8 +228,8 @@ public class MatchVideoServiceImpl {
         }
         //执行kimi任务
         if (kimiText.getKimiStatus() == 0) {
-            //已经存在失败  设置为失败
-            if (setMatchFail(matchVideo)) {
+            //已经存在结果  直接设置
+            if (existMatchVideoResult(matchVideo)) {
                 return;
             }
             String lockKey = String.format(KIMI_LOCK_KEY, matchVideo.getContentId());
@@ -239,6 +237,7 @@ public class MatchVideoServiceImpl {
             boolean lock = redisLock.tryLock(lockKey, lockValue, 300, TimeUnit.SECONDS);
             if (lock) {
                 log.info("processKimiMatchContent lock lockKey={}", lockKey);
+                addStartProcessingTime(matchVideo.getContentId());
                 boolean res = kimiService.updateKimiContent(matchVideo.getContentId());
                 if (res) {
                     updateStatus(matchVideo.getId(), ContentStatusEnum.KIMI_SUCCESS.getStatusCode());
@@ -250,7 +249,7 @@ public class MatchVideoServiceImpl {
                         LarkRobotUtil.sendMessage("kimiService updateKimiContent fail contentId=" + matchVideo.getContentId());
                         log.error("kimiService updateKimiContent fail contentId={}", matchVideo.getContentId());
                         kimiService.updateKimiContentStatus(matchVideo.getContentId(), 2);
-                        updateStatus(matchVideo.getId(), ContentStatusEnum.ERROR_99.getStatusCode());
+                        failMatchVideo(matchVideo);
                     } else {
                         if (count == null) {
                             redisTemplate.opsForValue().set(countKey, 1, 3, TimeUnit.DAYS);
@@ -326,8 +325,8 @@ public class MatchVideoServiceImpl {
         boolean existCrawlerVideo = crawlerVideoService.existCrawlerVideo(matchVideo.getContentId());
         log.info("processCrawlerMatchContent contentId={} existCrawlerVideo={}", matchVideo.getContentId(), existCrawlerVideo);
         if (!existCrawlerVideo) {
-            //已经存在失败  设置为失败
-            if (setMatchFail(matchVideo)) {
+            //已经存在结果  直接设置
+            if (existMatchVideoResult(matchVideo)) {
                 return;
             }
             String lockKey = String.format(CRAWLER_LOCK_KEY, matchVideo.getContentId());
@@ -348,7 +347,7 @@ public class MatchVideoServiceImpl {
                             //更新状态为失败
                             LarkRobotUtil.sendMessage("crawlerVideoService addCrawlerVideo fail contentId=" + matchVideo.getContentId());
                             log.error("crawlerVideoService addCrawlerVideo fail contentId={}", matchVideo.getContentId());
-                            updateStatus(matchVideo.getId(), ContentStatusEnum.ERROR_99.getStatusCode());
+                            failMatchVideo(matchVideo);
                         } else {
                             if (count == null) {
                                 redisTemplate.opsForValue().set(countKey, 1, 3, TimeUnit.DAYS);
@@ -358,7 +357,7 @@ public class MatchVideoServiceImpl {
                         }
                     }
                 } else if (kimiText.getKimiStatus() == 2) {
-                    updateStatus(matchVideo.getId(), ContentStatusEnum.ERROR_99.getStatusCode());
+                    failMatchVideo(matchVideo);
                 }
                 redisLock.unlock(lockKey, lockValue);
             }
@@ -447,8 +446,8 @@ public class MatchVideoServiceImpl {
         boolean existUploadCrawlerVideo = crawlerVideoService.existUploadCrawlerVideo(matchVideo.getContentId());
         log.info("processUploadCrawlerVideo contentId={} existCrawlerVideo={}", matchVideo.getContentId(), existUploadCrawlerVideo);
         if (!existUploadCrawlerVideo) {
-            //已经存在失败  设置为失败
-            if (setMatchFail(matchVideo)) {
+            //已经存在结果  直接设置
+            if (existMatchVideoResult(matchVideo)) {
                 return;
             }
             String lockKey = String.format(UPLOAD_CRAWLER_LOCK_KEY, matchVideo.getContentId());
@@ -458,6 +457,7 @@ public class MatchVideoServiceImpl {
                 boolean res = crawlerVideoService.uploadCrawlerVideo(matchVideo.getContentId());
                 if (res) {
                     updateStatus(matchVideo.getId(), ContentStatusEnum.SUCCESS_3.getStatusCode());
+                    updateResultStatus(matchVideo.getContentId(), ContentResultStatusEnum.SUCCESS.getStatus());
                 } else {
                     //上传失败记录
                     String countKey = String.format(UPLOAD_CRAWLER_FAIL_COUNT_KEY, matchVideo.getContentId());
@@ -466,7 +466,7 @@ public class MatchVideoServiceImpl {
                         LarkRobotUtil.sendMessage("crawlerVideoService uploadCrawlerVideo fail contentId=" + matchVideo.getContentId());
                         log.error("crawlerVideoService uploadCrawlerVideo fail contentId={}", matchVideo.getContentId());
                         //更新状态为失败
-                        updateStatus(matchVideo.getId(), ContentStatusEnum.ERROR_99.getStatusCode());
+                        failMatchVideo(matchVideo);
                     } else {
                         if (count == null) {
                             redisTemplate.opsForValue().set(countKey, 1, 3, TimeUnit.DAYS);
@@ -544,31 +544,65 @@ public class MatchVideoServiceImpl {
         matchVideoMapper.updateByPrimaryKeySelective(matchVideo);
     }
 
-    private boolean setMatchFail(MatchVideo matchVideo) {
-        String failKey = String.format(FAIL_KEY, matchVideo.getContentId());
-        Integer status = (Integer) redisTemplate.opsForValue().get(failKey);
+    private boolean existMatchVideoResult(MatchVideo matchVideo) {
+
+        //先查询缓存  缓存中存在直接返回结果
+        String key = String.format(EXIST_RESULT_KEY, matchVideo.getContentId());
+        Integer status = (Integer) redisTemplate.opsForValue().get(key);
         if (status != null) {
             updateStatus(matchVideo.getId(), status);
             return true;
         }
-        //查询相同的contentId,如果已经失败,则直接更新状态为失败
-        MatchVideoExample example = new MatchVideoExample();
+
+        //查询文章状态 查询到更新缓存
+        LongArticlesTextExample example = new LongArticlesTextExample();
         example.createCriteria().andContentIdEqualTo(matchVideo.getContentId());
-        List<MatchVideo> matchVideos = matchVideoMapper.selectByExample(example);
-        if (!CollectionUtils.isEmpty(matchVideos)) {
-            for (MatchVideo matchVideo1 : matchVideos) {
-                if (matchVideo1.getId().equals(matchVideo.getId())) {
-                    continue;
-                }
-                if (ContentStatusEnum.isFail(matchVideo1.getContentStatus())) {
-                    updateStatus(matchVideo.getId(), matchVideo1.getContentStatus());
-                    redisTemplate.opsForValue().set(failKey, matchVideo1.getContentStatus(), 1, TimeUnit.DAYS);
-                    return true;
-                }
-            }
+        List<LongArticlesText> longArticlesTexts = longArticlesTextMapper.selectByExample(example);
+        if (CollectionUtils.isEmpty(longArticlesTexts)) {
+            return false;
+        }
+        LongArticlesText longArticlesText = longArticlesTexts.get(0);
+        if (ContentResultStatusEnum.SUCCESS.getStatus() == longArticlesText.getResultStatus()) {
+            updateStatus(matchVideo.getId(), ContentStatusEnum.SUCCESS_3.getStatusCode());
+            redisTemplate.opsForValue().set(key, ContentStatusEnum.SUCCESS_3.getStatusCode(), 1, TimeUnit.DAYS);
+            return true;
+        }
+        if (ContentResultStatusEnum.FAIL.getStatus() == longArticlesText.getResultStatus()) {
+            updateStatus(matchVideo.getId(), ContentStatusEnum.ERROR_99.getStatusCode());
+            redisTemplate.opsForValue().set(key, ContentStatusEnum.ERROR_99.getStatusCode(), 1, TimeUnit.DAYS);
+            return true;
         }
         return false;
     }
+
+    private void addStartProcessingTime(String contentId) {
+        LongArticlesTextExample example = new LongArticlesTextExample();
+        example.createCriteria().andContentIdEqualTo(contentId);
+        List<LongArticlesText> longArticlesTexts = longArticlesTextMapper.selectByExample(example);
+        if (CollectionUtils.isEmpty(longArticlesTexts)) {
+            return;
+        }
+        LongArticlesText longArticlesText = longArticlesTexts.get(0);
+        if (longArticlesText.getStartProcessingTime() == null) {
+            LongArticlesText updateLongArticlesText = new LongArticlesText();
+            updateLongArticlesText.setContentId(contentId);
+            updateLongArticlesText.setStartProcessingTime(new Date());
+            longArticlesTextMapper.updateByPrimaryKeySelective(updateLongArticlesText);
+        }
+    }
+
+    private void updateResultStatus(String contentId, Integer resultStatus) {
+        LongArticlesText updateLongArticlesText = new LongArticlesText();
+        updateLongArticlesText.setContentId(contentId);
+        updateLongArticlesText.setResultStatus(resultStatus);
+        longArticlesTextMapper.updateByPrimaryKeySelective(updateLongArticlesText);
+
+    }
+
+    private void failMatchVideo(MatchVideo matchVideo) {
+        updateStatus(matchVideo.getId(), ContentStatusEnum.ERROR_99.getStatusCode());
+        updateResultStatus(matchVideo.getContentId(), ContentResultStatusEnum.FAIL.getStatus());
+    }
 }