Просмотр исходного кода

在排序结果不足时触发待发布缓存清理和异步重建推荐数据

wangyunpeng 15 часов назад
Родитель
Сommit
e4bd063528

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

@@ -236,7 +236,6 @@ public class ContentServiceImpl implements ContentService {
             log.info("articleSortResponse={}", articleSortResponse);
             String message = String.format("排序结果为空 planId=%s accountName=%s", planAccount.getPlanId(), planAccount.getAccountName());
             LarkRobotUtil.sendMessage(message);
-
             return new ArrayList<>();
         }
         ArticleSortResponseData data = articleSortResponse.getData();
@@ -258,11 +257,33 @@ public class ContentServiceImpl implements ContentService {
             String message = String.format("排序结果不足 planId=%s accountName=%s num=%s",
                     planAccount.getPlanId(), planAccount.getAccountName(), articleSortResponse.getData().getRank_list().size());
             LarkRobotUtil.sendMessage(message);
+            // 清除待发布缓存,成功后异步触发一次 /recommend 重建数据
+            triggerRebuildRecommend(planAccount, articleSortRequest);
             return new ArrayList<>();
         }
         return data.getRank_list();
     }
 
+    /**
+     * 排序结果不足时调用:
+     * 1. 调用 /deleteGzhWaiting 清除当前 planId + accountId 的待发布缓存
+     * 2. 清除成功后异步触发一次 /recommend 重新生成排序数据(不关心返回结果)
+     */
+    private void triggerRebuildRecommend(PlanAccount planAccount, ArticleSortRequest articleSortRequest) {
+        try {
+            boolean deleteSuccess = sortService.deleteGzhWaiting(planAccount.getPlanId(), planAccount.getAccountId());
+            if (deleteSuccess) {
+                sortService.asyncRecommend(articleSortRequest);
+            } else {
+                log.warn("deleteGzhWaiting fail, skip asyncRecommend planId={} accountId={}",
+                        planAccount.getPlanId(), planAccount.getAccountId());
+            }
+        } catch (Exception e) {
+            log.error("triggerRebuildRecommend error planId={} accountId={} errorMsg:{}",
+                    planAccount.getPlanId(), planAccount.getAccountId(), e.getMessage());
+        }
+    }
+
     public List<VideoDetail> getPublishVideoDetail(MatchVideo matchVideo, Integer publicFlag, PlanAccount planAccount, PublishContent publishContent) {
         List<CrawlerVideo> contentMiniVideos = getContentMiniVideo(matchVideo.getContentId(), publicFlag, planAccount);
         log.info("getPublishVideoDetail contentMiniVideos={}", contentMiniVideos);

+ 12 - 0
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/remote/SortService.java

@@ -8,4 +8,16 @@ public interface SortService {
     ArticleSortResponse publishArticleSort(ArticleSortRequest request);
 
     void addBatchGroupGZH(String ghId, String appId);
+
+    /**
+     * 清除指定 planId + publishAccountId 的待发布缓存(数据库 + Redis)
+     *
+     * @return true 表示接口返回 code=0,调用成功
+     */
+    boolean deleteGzhWaiting(String planId, String publishAccountId);
+
+    /**
+     * 异步调用一次 /recommend 接口,不关心返回结果
+     */
+    void asyncRecommend(ArticleSortRequest request);
 }

+ 37 - 0
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/remote/impl/SortServiceImpl.java

@@ -3,16 +3,19 @@ package com.tzld.piaoquan.longarticle.service.remote.impl;
 
 import cn.hutool.core.collection.CollectionUtil;
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.longarticle.model.dto.ArticleSortRequest;
 import com.tzld.piaoquan.longarticle.model.dto.ArticleSortResponse;
 import com.tzld.piaoquan.longarticle.service.remote.SortService;
 import com.tzld.piaoquan.longarticle.utils.HttpClientUtil;
 import com.tzld.piaoquan.longarticle.utils.HttpPoolClientUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
 import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
 
 @Slf4j
 @Service
@@ -39,6 +42,40 @@ public class SortServiceImpl implements SortService {
         return response;
     }
 
+    @Override
+    public boolean deleteGzhWaiting(String planId, String publishAccountId) {
+        if (StringUtils.isBlank(planId) || StringUtils.isBlank(publishAccountId)) {
+            log.warn("deleteGzhWaiting param invalid, planId={} publishAccountId={}", planId, publishAccountId);
+            return false;
+        }
+        String apiUrl = "http://101.37.174.139:80/deleteGzhWaiting?planId=" + planId
+                + "&publishAccountId=" + publishAccountId;
+        try {
+            String res = HTTP_POOL_CLIENT_UTIL_DEFAULT.get(apiUrl);
+            log.info("deleteGzhWaiting planId={} publishAccountId={} res={}", planId, publishAccountId, res);
+            if (StringUtils.isBlank(res)) {
+                return false;
+            }
+            JSONObject jsonObject = JSON.parseObject(res);
+            return jsonObject != null && Objects.equals(jsonObject.getInteger("code"), 0);
+        } catch (Exception e) {
+            log.error("deleteGzhWaiting error planId={} publishAccountId={} errorMsg:{}",
+                    planId, publishAccountId, e.getMessage());
+        }
+        return false;
+    }
+
+    @Override
+    public void asyncRecommend(ArticleSortRequest request) {
+        CompletableFuture.runAsync(() -> {
+            try {
+                publishArticleSort(request);
+            } catch (Exception e) {
+                log.error("asyncRecommend error request={} errorMsg:{}", request, e.getMessage());
+            }
+        });
+    }
+
     @Override
     public void addBatchGroupGZH(String ghId, String appId) {
         String apiUrl = "http://101.37.174.139:80/user/addGZH";