|
@@ -1116,24 +1116,26 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void handleCallback(MidjourneyUserConversation conversation) {
|
|
public void handleCallback(MidjourneyUserConversation conversation) {
|
|
|
- String key = RedisService.key.MIDJOURNEY_PROGRESS.getName() + conversation.getTaskId();
|
|
|
|
|
-
|
|
|
|
|
- synchronized (conversation.getTaskId()) {
|
|
|
|
|
- String currentProgressStr = redisService.getStr(key);
|
|
|
|
|
- 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 {
|
|
|
|
|
- if (redisService.setNx(key, 0, RedisService.key.MIDJOURNEY_PROGRESS.getTimeout())) {
|
|
|
|
|
|
|
+ String key = RedisService.key.MIDJOURNEY_PROGRESS.getName() + conversation.getTaskId();
|
|
|
|
|
+ String currentProgressStr = redisService.getStr(key);
|
|
|
|
|
+
|
|
|
|
|
+ // 如果 key 不存在,使用 setNx 原子性操作确保初始化为 0
|
|
|
|
|
+ if (StringUtils.isBlank(currentProgressStr)) {
|
|
|
|
|
+ if (redisService.setNx(key, String.valueOf(0), RedisService.key.MIDJOURNEY_PROGRESS.getTimeout())) {
|
|
|
|
|
+ sendProgressToClient(conversation);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (StringUtils.isNumeric(currentProgressStr)) {
|
|
|
|
|
+ int currentProgress = Integer.parseInt(currentProgressStr);
|
|
|
|
|
+
|
|
|
|
|
+ // 原子性更新:获取当前值并更新为新的进度
|
|
|
|
|
+ if (conversation.getProgressNum() > currentProgress) {
|
|
|
|
|
+ String oldProgressStr = redisService.getSet(key, String.valueOf(conversation.getProgressNum()));
|
|
|
|
|
+
|
|
|
|
|
+ // 判断如果旧的进度仍然比当前进度低,才发送更新通知
|
|
|
|
|
+ if (StringUtils.isNumeric(oldProgressStr) && Integer.parseInt(oldProgressStr) < conversation.getProgressNum()) {
|
|
|
sendProgressToClient(conversation);
|
|
sendProgressToClient(conversation);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|