فهرست منبع

fix cos切换国内桶

zwhui 1 سال پیش
والد
کامیت
11c3f84e5a

+ 4 - 0
midjourney/src/main/java/com/yhlxj/dao/mapper/midjourney/MidjourneyUserConversationMapper.java

@@ -2,10 +2,14 @@ package com.yhlxj.dao.mapper.midjourney;
 
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.yhlxj.dao.model.entity.MidjourneyUserConversation;
 import com.yhlxj.dao.model.entity.MidjourneyUserConversation;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 
 /**
 /**
  * @author zwhui
  * @author zwhui
  * @date 2024/4/23 16:03
  * @date 2024/4/23 16:03
  */
  */
 public interface MidjourneyUserConversationMapper extends BaseMapper<MidjourneyUserConversation> {
 public interface MidjourneyUserConversationMapper extends BaseMapper<MidjourneyUserConversation> {
+    void updateBatchById(@Param("conversations") List<MidjourneyUserConversation> conversations);
 }
 }

+ 52 - 23
midjourney/src/main/java/com/yhlxj/service/midjourney/impl/MidjourneyServiceImpl.java

@@ -1291,31 +1291,60 @@ public class MidjourneyServiceImpl implements MidjourneyService {
 
 
     @Override
     @Override
     public synchronized void syncPicture() {
     public synchronized void syncPicture() {
-       while (true){
-           //上传图片到cos
-           List<MidjourneyUserConversation> imgConversations = conversationMapper.selectList(Wrappers.lambdaQuery(MidjourneyUserConversation.class)
-                   .eq(MidjourneyUserConversation::getStatus, "SUCCESS").notLike(MidjourneyUserConversation::getImageUrl, "cdn.mj.galaxydvd.com")
-                   .ge(MidjourneyUserConversation::getStartTime, DateUtil.offsetMinute(new Date(), -60 * 24 * 3).getTime())
-                   .eq(MidjourneyUserConversation::getImageInvalid,Boolean.FALSE).orderByAsc(MidjourneyUserConversation::getId).last(" limit 10"));
-           if(imgConversations.size() == 0){
-               break;
-           }
-           for (MidjourneyUserConversation conversation : imgConversations) {
-               try {
-                   String imageUrl = conversation.getImageUrl();
-                   if (StringUtil.isNotBlank(imageUrl)) {
-                       conversation.setImageUrl(uploadPic(imageUrl, "conversation" + conversation.getId()));
-                       conversationMapper.updateById(conversation);
-                   }
-               } catch (IOException e) {
-                   log.error("上传图片失败", e);
-                   conversation.setImageInvalid(Boolean.TRUE);
-                   conversationMapper.updateById(conversation);
-               }
-           }
-       }
+        int batchSize = 10;
+        int maxRetries = 3;
+        while (true) {
+            // 查询需要上传的图片对话
+            List<MidjourneyUserConversation> imgConversations = conversationMapper.selectList(Wrappers.lambdaQuery(MidjourneyUserConversation.class)
+                    .eq(MidjourneyUserConversation::getStatus, "SUCCESS")
+                    .notLike(MidjourneyUserConversation::getImageUrl, ".mj.galaxydvd.com")
+                    .ge(MidjourneyUserConversation::getStartTime, DateUtil.offsetMinute(new Date(), -60 * 24 * 3).getTime())
+                    .eq(MidjourneyUserConversation::getImageInvalid, Boolean.FALSE)
+                    .orderByAsc(MidjourneyUserConversation::getId)
+                    .last(" limit " + batchSize));
+
+            // 如果没有数据,跳出循环
+            if (imgConversations.isEmpty()) {
+                log.info("所有图片已处理完毕,结束同步任务");
+                break;
+            }
+
+            for (MidjourneyUserConversation conversation : imgConversations) {
+                String imageUrl = conversation.getImageUrl();
+                if (StringUtil.isBlank(imageUrl)) {
+                    log.warn("对话ID {} 的图片URL为空,跳过处理", conversation.getId());
+                    continue;
+                }
+
+                boolean uploadSuccess = false;
+                for (int i = 0; i < maxRetries; i++) {
+                    try {
+                        // 上传图片并更新URL
+                        String newImageUrl = uploadPic(imageUrl, "conversation" + conversation.getId());
+                        conversation.setImageUrl(newImageUrl);
+                        uploadSuccess = true;
+                        log.info("对话ID {} 图片上传成功,新的图片URL:{}", conversation.getId(), newImageUrl);
+                        break;
+                    } catch (IOException e) {
+                        log.error("对话ID {} 第 {} 次上传图片失败,原因:{}", conversation.getId(), i + 1, e.getMessage(), e);
+                    }
+                }
+
+                conversation.setImageInvalid(!uploadSuccess);
+                conversationMapper.updateById(conversation);
+            }
+
+            // 防止高频次查询,增加短暂的休眠时间
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+                log.error("线程休眠被中断", e);
+                Thread.currentThread().interrupt();
+            }
+        }
     }
     }
 
 
+
     @Override
     @Override
     public Long checkUserLimit(MidjourneyUser user,Integer mode){
     public Long checkUserLimit(MidjourneyUser user,Integer mode){
         Long num = 0L;
         Long num = 0L;

+ 3 - 3
midjourney/src/main/java/com/yhlxj/util/COSUtil.java

@@ -23,8 +23,8 @@ public class COSUtil {
      */
      */
     public static String SECRET_ID = "AKIDUzYL8rV9kEaoy0tcQH3kR9obeqDlw6ZK"; //这个参数需要你替换为自己的
     public static String SECRET_ID = "AKIDUzYL8rV9kEaoy0tcQH3kR9obeqDlw6ZK"; //这个参数需要你替换为自己的
     public static String SECRET_KEY = "258AQj1DPcvTkEtXIOZrKtcSfvhiibUN"; //这个参数需要你替换为自己的
     public static String SECRET_KEY = "258AQj1DPcvTkEtXIOZrKtcSfvhiibUN"; //这个参数需要你替换为自己的
-    public static String BUCKET_NAME = "mj-1310013563"; //这个参数需要你替换为自己的
-    public static String REGION = "ap-hongkong"; //这个参数需要你替换为自己的
+    public static String BUCKET_NAME = "chat-1310013563"; //这个参数需要你替换为自己的
+    public static String REGION = "ap-shanghai"; //这个参数需要你替换为自己的
 
 
     public static String prePath="conversation/"; //这个参数需要你替换为自己的
     public static String prePath="conversation/"; //这个参数需要你替换为自己的
 
 
@@ -53,7 +53,7 @@ public class COSUtil {
         objectMetadata.setContentLength(in.available());
         objectMetadata.setContentLength(in.available());
         COSClient instance = getInstance();
         COSClient instance = getInstance();
         instance.putObject(BUCKET_NAME, key, in, objectMetadata);
         instance.putObject(BUCKET_NAME, key, in, objectMetadata);
-        String url = "https://cdn.mj.galaxydvd.com/" + key;
+        String url = "https://cdn2.mj.galaxydvd.com/" + key;
         instance.shutdown();
         instance.shutdown();
         return url;
         return url;
     }
     }

+ 5 - 1
midjourney/src/main/java/com/yhlxj/web/GlobalExceptionHandler.java

@@ -7,6 +7,7 @@ import com.cyksj.dto.Result;
 import com.cyksj.enums.GatewayApiCode;
 import com.cyksj.enums.GatewayApiCode;
 import com.cyksj.enums.GatewayResponse;
 import com.cyksj.enums.GatewayResponse;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.exceptions.PersistenceException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.BindException;
 import org.springframework.validation.BindException;
 import org.springframework.validation.FieldError;
 import org.springframework.validation.FieldError;
@@ -42,7 +43,7 @@ public class GlobalExceptionHandler {
         StackTraceElement[] trace = e.getStackTrace();
         StackTraceElement[] trace = e.getStackTrace();
         log.info("{}",e);
         log.info("{}",e);
         for (StackTraceElement element : trace) {
         for (StackTraceElement element : trace) {
-            if (StringUtil.startsWith(element.getClassName(), "com.cyksj")) {
+            if (StringUtil.startsWith(element.getClassName(), "com.yhlxj")) {
                 builder.append(element.getFileName())
                 builder.append(element.getFileName())
                         .append("[").append(element.getLineNumber()).append("]")
                         .append("[").append(element.getLineNumber()).append("]")
                         .append("&");
                         .append("&");
@@ -62,6 +63,9 @@ public class GlobalExceptionHandler {
                 }
                 }
             }
             }
         }
         }
+        if (e instanceof PersistenceException) {
+            return GatewayResponse.FAIL.newBuilder().setMsg(GatewayApiCode.SYSTEM_BUSYNESS_ERROR.getMsg()).toResult(value);
+        }
         log.error("全局错误处理: \n Cause: {} \n Value: {}", message, value);
         log.error("全局错误处理: \n Cause: {} \n Value: {}", message, value);
         if (apiCode != null) {
         if (apiCode != null) {
             return GatewayResponse.FAIL.newBuilder().buildGatewayCode(apiCode).toResult();
             return GatewayResponse.FAIL.newBuilder().buildGatewayCode(apiCode).toResult();

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
midjourney/src/main/resources/application-prd.yml


+ 14 - 0
midjourney/src/main/resources/mapper/MidjourneyUserConversationMapper.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.yhlxj.dao.mapper.midjourney.MidjourneyUserConversationMapper">
+
+    <update id="updateBatchById">
+        <foreach collection="conversations" item="conversation" separator=";">
+            UPDATE midjourney_user_conversation
+            SET image_url = #{conversation.imageUrl},
+            image_invalid = #{conversation.imageInvalid}
+            WHERE id = #{conversation.id}
+        </foreach>
+    </update>
+
+</mapper>

+ 1 - 0
netflix-common/src/main/java/com/cyksj/enums/GatewayApiCode.java

@@ -38,6 +38,7 @@ public enum GatewayApiCode {
      */
      */
     ILLEGAL_ENUM_VALUE(100001, "异常枚举类型"),
     ILLEGAL_ENUM_VALUE(100001, "异常枚举类型"),
     METADATA_SYSTEM_ERROR(100002, "系统错误"),
     METADATA_SYSTEM_ERROR(100002, "系统错误"),
+    SYSTEM_BUSYNESS_ERROR(100003, "系统繁忙"),
 
 
     /**
     /**
      * 11-mq处理异常
      * 11-mq处理异常

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است