chenbiao 2 лет назад
Родитель
Сommit
b1dede8acc

+ 2 - 0
midjourney/src/main/java/com/yhlxj/service/midjourney/MidjourneyService.java

@@ -44,4 +44,6 @@ public interface MidjourneyService {
     MidjourneyUserConversation uploadFile(SubmitUploadDTO uploadDTO,MidjourneyUser user) throws Exception;
 
     void syncMidjourneyStatus();
+
+    void syncPicture();
 }

+ 3 - 5
midjourney/src/main/java/com/yhlxj/service/midjourney/impl/MidJourneyAccountServiceImpl.java

@@ -326,11 +326,9 @@ public class MidJourneyAccountServiceImpl extends ServiceImpl<MidjourneyAccountM
                         redisService.hset(key,midjourneyAccount.getInstanceId().toString(),accountsUsage.values().toArray()[0]);
                     }
                     //重置setting
-                    TASK_EXECUTOR.execute(() -> {
-                        if (!"default".equals(accountInfo.getVersion()) || !accountInfo.isRaw()) {
-                            resetAccount(instanceId);
-                        }
-                    });
+                    if (!"default".equals(accountInfo.getVersion()) || !accountInfo.isRaw()) {
+                        resetAccount(instanceId);
+                    }
                 }else {
                     redisService.hdel(key, instanceId.toString());
                 }

+ 34 - 22
midjourney/src/main/java/com/yhlxj/service/midjourney/impl/MidjourneyServiceImpl.java

@@ -118,7 +118,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
 
     private final MidjourneyApiChannelMapper midjourneyApiChannelMapper;
 
-    public static final Map<String, MjVersionStatus> MJ_VERSION_STATUS_MAP = new ConcurrentHashMap<>();
+    public static final Map<String, MjVersionStatus> MJ_VERSION_STATUS_MAP = new HashMap<>();
 
     private final MidjourneyWhiteMapper midjourneyWhiteMapper;
 
@@ -991,6 +991,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
 
     @Override
     public void cancelConversation(MidjourneyUser user, Long id) {
+
         MidjourneyUserConversation conversation = conversationMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUserConversation.class).eq(MidjourneyUserConversation::getId, id));
         if (conversation == null) {
             throw BusinessRuntimeException.getInstance("会话不存在");
@@ -1196,11 +1197,19 @@ public class MidjourneyServiceImpl implements MidjourneyService {
 
         synchronized (key.intern()) {
             String currentProgressStr = redisService.getStr(key);
-            int currentProgress = StringUtils.isBlank(currentProgressStr) ? 0 : Integer.parseInt(currentProgressStr);
-            if (conversation.getProgressNum() > currentProgress) {
-                redisService.set(key, String.valueOf(conversation.getProgressNum()), RedisService.key.MIDJOURNEY_PROGRESS.getTimeout());
+            if(StringUtils.isNotBlank(currentProgressStr)){
+                if (StringUtils.isNumeric(currentProgressStr)) {
+                    int currentProgress = Integer.parseInt(currentProgressStr);
+                    if (conversation.getProgressNum() > currentProgress) {
+                        redisService.set(key, String.valueOf(conversation.getProgressNum()), RedisService.key.MIDJOURNEY_PROGRESS.getTimeout());
+                        sendProgressToClient(conversation);
+                    }
+                }
+            }else {
+                redisService.set(key, 0, RedisService.key.MIDJOURNEY_PROGRESS.getTimeout());
                 sendProgressToClient(conversation);
             }
+
         }
     }
 
@@ -1292,23 +1301,6 @@ public class MidjourneyServiceImpl implements MidjourneyService {
         for (Object json : relaxArray) {
             notifyHook(Jsons.toJson(json));
         }
-
-        //上传图片到cos
-        List<MidjourneyUserConversation> imgConversations = conversationMapper.selectList(Wrappers.lambdaQuery(MidjourneyUserConversation.class)
-                .eq(MidjourneyUserConversation::getStatus, "SUCCESS").notLike(MidjourneyUserConversation::getImageUrl, "cdn.mj.galaxydvd.com")
-                .ge(MidjourneyUserConversation::getCreatedTime, DateUtil.offsetMinute(new Date(), -60 * 24)).orderByAsc(MidjourneyUserConversation::getId).last(" limit 10"));
-            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);
-                }
-
-            }
     }
 
     @Override
@@ -1358,11 +1350,31 @@ public class MidjourneyServiceImpl implements MidjourneyService {
                 String subCategory = parts[2] + "-" + parts[3];
                 String key = mainCategory + "-" + subCategory;
                 MJ_VERSION_STATUS_MAP
-                        .putIfAbsent(key, mjVersionStatus);
+                        .put(key, mjVersionStatus);
             }
         }
     }
 
+    @Override
+    public void syncPicture() {
+        //上传图片到cos
+        List<MidjourneyUserConversation> imgConversations = conversationMapper.selectList(Wrappers.lambdaQuery(MidjourneyUserConversation.class)
+                .eq(MidjourneyUserConversation::getStatus, "SUCCESS").notLike(MidjourneyUserConversation::getImageUrl, "cdn.mj.galaxydvd.com")
+                .ge(MidjourneyUserConversation::getCreatedTime, DateUtil.offsetMinute(new Date(), -60 * 24)).orderByAsc(MidjourneyUserConversation::getId).last(" limit 10"));
+        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);
+            }
+
+        }
+    }
+
     public static int extractNumberOrDefault(String input) {
        try {
            Pattern pattern = Pattern.compile("\\d+");

+ 5 - 1
midjourney/src/main/java/com/yhlxj/service/task/Scheduler.java

@@ -22,7 +22,11 @@ public class Scheduler {
         midjourneyService.syscConversation();
     }
 
-    @Scheduled(cron = "0 0/15 * * * ?")
+    @Scheduled(cron = "30 0/5 * * * ?")
+    public void syncPicture() throws Exception {
+        midjourneyService.syncPicture();
+    }
+    @Scheduled(cron = "45 0/15 * * * ?")
     public void syncAccount() throws Exception {
         midjourneyAccountService.syncAccountByMJPlus();
     }

+ 1 - 0
midjourney/src/main/java/com/yhlxj/web/mirror/MidjourneyController.java

@@ -442,6 +442,7 @@ public class MidjourneyController {
         InputStream inputStream = request.getInputStream();
         byte[] bytes = IoKit.toBytes(inputStream);
         String json = new String(bytes, StandardCharsets.UTF_8);
+        inputStream.close();
         midjourneyService.notifyHook(json);
     }
 

+ 0 - 5
midjourney/src/main/java/com/yhlxj/web/wss/MidjourneyServerEndpoint.java

@@ -64,13 +64,8 @@ public class MidjourneyServerEndpoint {
             WssSession wsSession = new WssSession(session);
             if (conversation == null) {
                 wsSession.sendMessage(WsMessageTypeEnum.SERVER_TASK_NOT_FIND, "任务不存在.");
-                session.close();
             } else {
-                RedisService redisService = SpringCtxUtils.getBean(RedisService.class);
                 WSS_SESSION_MAP.put(user.getId(), wsSession);
-                TASK_EXECUTOR.execute(() -> {
-                    redisService.set(RedisService.key.MIDJOURNEY_CONVERSATION.getName() + taskId, conversation, RedisService.key.MIDJOURNEY_CONVERSATION.getTimeout());
-                });
                 wsSession.sendMessage(WsMessageTypeEnum.SERVER_TASK_INFO, new JSONObject(conversation));
             }