Browse Source

腾讯云视频接口请求限制bugfix

wangliang 3 years ago
parent
commit
1ace0d6561

+ 6 - 0
themis-business/src/main/java/com/qmth/themis/business/constant/SystemConstant.java

@@ -219,6 +219,8 @@ public class SystemConstant {
 
     public static final int MAX_EXAM_STATUS_COUNT = 20;
 
+    public static final int MAX_TENCENT_VIDEO_STREAM_ID_COUNT = 30;
+
     public static final String EXCEL_PREFIX = ".xlsx";
 
     public static final String TXT_PREFIX = ".txt";
@@ -311,6 +313,8 @@ public class SystemConstant {
 
     public static final String REDIS_LOCK_TENCENT_VIDEO_PREFIX = "lock:tencent:video:";//腾讯云视频锁
 
+    public static final String REDIS_LOCK_TENCENT_VIDEO_STREAM_ID_PREFIX = "lock:tencent:video:stream:id";//腾讯云视频锁
+
     /**
      * redis过期时间
      */
@@ -324,6 +328,8 @@ public class SystemConstant {
 
     public static final long REDIS_LOCK_TENCENT_VIDEO_TIME_OUT = 60L * 1;
 
+    public static final long REDIS_LOCK_TENCENT_VIDEO_STREAM_ID_TIME_OUT = 60L * 20;
+
     /**
      * rocket mq
      */

+ 1 - 0
themis-business/src/main/java/com/qmth/themis/business/service/TGErrorService.java

@@ -14,6 +14,7 @@ public interface TGErrorService extends IService<TGError> {
 
     /**
      * 保存考试错误信息
+     *
      * @param s
      */
     void saveExamTgError(Object... s);

+ 5 - 1
themis-business/src/main/java/com/qmth/themis/business/service/impl/TGErrorServiceImpl.java

@@ -9,6 +9,8 @@ import com.qmth.themis.business.entity.TGError;
 import com.qmth.themis.business.service.TGErrorService;
 import org.springframework.stereotype.Service;
 
+import java.util.Objects;
+
 /**
  * @Description: 全局异常错误信息 服务实现类
  * @Param:
@@ -29,7 +31,9 @@ public class TGErrorServiceImpl extends ServiceImpl<TGErrorMapper, TGError> impl
         JSONObject jsonObject = new JSONObject();
         jsonObject.put(SystemConstant.RECORD_ID, (Long) s[0]);
         jsonObject.put("method", s[1]);
-        jsonObject.put("examStatus", ExamRecordCacheUtil.getStatus((Long) s[0]));
+        if (Objects.nonNull(ExamRecordCacheUtil.getStatus((Long) s[0]))) {
+            jsonObject.put("examStatus", ExamRecordCacheUtil.getStatus((Long) s[0]));
+        }
         TGError tgError = new TGError(jsonObject.toJSONString(), System.currentTimeMillis());
         this.save(tgError);
     }

+ 42 - 7
themis-mq/src/main/java/com/qmth/themis/mq/service/impl/MqLogicServiceImpl.java

@@ -20,7 +20,6 @@ import com.qmth.themis.business.templete.TaskImportTemplete;
 import com.qmth.themis.business.templete.impl.*;
 import com.qmth.themis.business.util.*;
 import com.qmth.themis.common.contanst.Constants;
-import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.exception.BusinessException;
 import com.qmth.themis.common.util.SimpleBeanUtil;
 import com.qmth.themis.mq.service.MqLogicService;
@@ -34,6 +33,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
 
 import javax.annotation.Resource;
 import java.io.IOException;
@@ -123,6 +123,9 @@ public class MqLogicServiceImpl implements MqLogicService {
     @Resource
     TOeExamRecordService tOeExamRecordService;
 
+    @Resource
+    TGErrorService tgErrorService;
+
     /**
      * mq最大重试次数逻辑
      *
@@ -303,13 +306,45 @@ public class MqLogicServiceImpl implements MqLogicService {
             streamIds[i] = SystemConstant.setStreamId(monitorUtil.getMonitorDomain().getPrefix(), recordId, MonitorVideoSourceEnum.convertToName(monitorRecordList.get(i)));
         }
 
-        Map<String, Object> resultMap = tencentYunUtil.tencentVodSdk(tencentYunUtil.getTencentYunDomain().getSecretId(),
-                tencentYunUtil.getTencentYunDomain().getSecretKey(),
-                tencentYunUtil.getTencentYunDomain().getQueryUrl(),
-                tencentYunUtil.getTencentYunDomain().getVodAppId(),
-                streamIds);
+        Map<String, Object> resultMap = null;
+        boolean lockStreamIds = false;
+        for (int i = 0; i < SystemConstant.MAX_TENCENT_VIDEO_STREAM_ID_COUNT; i++) {
+            lockStreamIds = redisUtil.lock(SystemConstant.REDIS_LOCK_TENCENT_VIDEO_STREAM_ID_PREFIX + streamIds.toString(),
+                    SystemConstant.REDIS_LOCK_TENCENT_VIDEO_STREAM_ID_TIME_OUT);
+            if (lockStreamIds) {
+                try {
+                    resultMap = tencentYunUtil.tencentVodSdk(tencentYunUtil.getTencentYunDomain().getSecretId(),
+                            tencentYunUtil.getTencentYunDomain().getSecretKey(),
+                            tencentYunUtil.getTencentYunDomain().getQueryUrl(),
+                            tencentYunUtil.getTencentYunDomain().getVodAppId(),
+                            streamIds);
+                    break;
+                } catch (Exception e) {
+                    log.error("请求出错", e);
+                    if (i == SystemConstant.MAX_TENCENT_VIDEO_STREAM_ID_COUNT - 1) {
+                        tgErrorService.saveExamTgError(recordId, "execMqTencentVideoLogic");
+                    } else {
+                        try {
+                            Thread.sleep(3000);
+                        } catch (InterruptedException ex) {
+                            ex.printStackTrace();
+                        }
+                    }
+                } finally {
+                    if (Objects.nonNull(streamIds)) {
+                        redisUtil.releaseLock(SystemConstant.REDIS_LOCK_TENCENT_VIDEO_STREAM_ID_PREFIX + streamIds.toString());
+                    }
+                }
+            } else {
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
 
-        if (Objects.nonNull(resultMap) && resultMap.size() > 0) {
+        if (!CollectionUtils.isEmpty(resultMap)) {
             SearchMediaResponse response = (SearchMediaResponse) resultMap.get("object");
             if (Objects.nonNull(response) && response.getTotalCount().intValue() > 0) {
                 Map<String, String> map = new HashMap<>();