|
|
@@ -116,10 +116,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
|
|
|
private static final int MIN_PROCESSING_TIME = 10;
|
|
|
- private static final int MAX_PROCESSING_TIME = 30;
|
|
|
- private static final int AVG_PROCESSING_TIME = 20;
|
|
|
+ private static final int MAX_PROCESSING_TIME = 60;
|
|
|
|
|
|
- private final MidjourneyUserQueueMapper midjourneyUserQueueMapper;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
@@ -1383,9 +1381,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
MidjourneyUserQueue queue = MidjourneyUserQueue.builder()
|
|
|
.queueId(queueId).userId(userId).prompt(submitImagineDTO.getPrompt())
|
|
|
.base64Array(JSONUtil.toJsonStr(submitImagineDTO.getBase64Array())).build();
|
|
|
- if (redisService.hasKey(queueKey)) {
|
|
|
- throw new BusinessRuntimeException("正在排队中,请耐心等待或切换Fast模式");
|
|
|
- }
|
|
|
+
|
|
|
redisService.lSet(queueKey, JSONUtil.toJsonStr(queue),key.getTimeout());
|
|
|
|
|
|
// 计算当前时段需要的额外等待时间(分钟)
|
|
|
@@ -1394,6 +1390,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
cleanupExpiredTasks(userId);
|
|
|
// 获取队列中最后一个任务的过期时间
|
|
|
long taskExpireTime;
|
|
|
+ long now = System.currentTimeMillis() / 1000;
|
|
|
String TASK_QUEUE = RedisService.key.MIDJOURNEY_QUEUE_LIMIT.getName() + userId;
|
|
|
Set<ZSetOperations.TypedTuple<Object>> lastTask = redisService.zRangeWithScores(TASK_QUEUE, -1, -1);
|
|
|
if (lastTask != null && !lastTask.isEmpty()) {
|
|
|
@@ -1401,34 +1398,34 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
taskExpireTime = lastTask.iterator().next().getScore().longValue();
|
|
|
} else {
|
|
|
// 队列为空,使用当前时间
|
|
|
- taskExpireTime = System.currentTimeMillis() / 1000;
|
|
|
+ taskExpireTime = now;
|
|
|
}
|
|
|
- // 计算队列中应该存在的假任务总数
|
|
|
- int requiredDummyTaskCount = (additionalWaitMinutes * 60) / AVG_PROCESSING_TIME;
|
|
|
|
|
|
- // 获取当前队列中的假任务数量
|
|
|
- long existingDummyTaskCount = countExistingDummyTasks(userId);
|
|
|
+ // 现有任务所需时间
|
|
|
+ long existingTaskTime = taskExpireTime - now;
|
|
|
+ // 剩余可生成的假任务时间
|
|
|
+ long dummyTaskTime = (additionalWaitMinutes * 60L) - existingTaskTime;
|
|
|
|
|
|
- // 计算需要添加的假任务数量
|
|
|
- int dummyTasksToAdd = requiredDummyTaskCount - (int)existingDummyTaskCount;
|
|
|
|
|
|
- if (dummyTasksToAdd > 0) {
|
|
|
- // 添加所需数量的假任务
|
|
|
- for (int i = 0; i < dummyTasksToAdd; i++) {
|
|
|
- String dummyTaskId = "dummy-" + UUID.randomUUID().toString();
|
|
|
+ if (dummyTaskTime > 0) {
|
|
|
+ // 添加所需时间的假任务
|
|
|
+ while (true) {
|
|
|
+ String dummyTaskId = "dummy-" + UUID.randomUUID();
|
|
|
// 均匀分布在当前时间到目标等待时间之间
|
|
|
- int processingTime = MIN_PROCESSING_TIME +
|
|
|
- (int)(Math.random() * (MAX_PROCESSING_TIME - MIN_PROCESSING_TIME));
|
|
|
-
|
|
|
+ long processingTime = MIN_PROCESSING_TIME +
|
|
|
+ (long)(Math.random() * (MAX_PROCESSING_TIME - MIN_PROCESSING_TIME));
|
|
|
+ dummyTaskTime -= processingTime;
|
|
|
taskExpireTime += processingTime;
|
|
|
- System.out.println("currentTime"+taskExpireTime);
|
|
|
redisService.zAdd(TASK_QUEUE, taskExpireTime, dummyTaskId);
|
|
|
+ if (dummyTaskTime <= 0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 真实任务的时间戳设置为"当前时间+额外等待时间"
|
|
|
- int processingTime = MIN_PROCESSING_TIME +
|
|
|
- (int)(Math.random() * (MAX_PROCESSING_TIME - MIN_PROCESSING_TIME));
|
|
|
+ long processingTime = MIN_PROCESSING_TIME +
|
|
|
+ (long)(Math.random() * (MAX_PROCESSING_TIME - MIN_PROCESSING_TIME));
|
|
|
taskExpireTime += processingTime;
|
|
|
System.out.println("taskExpireTime"+taskExpireTime);
|
|
|
redisService.zAdd(TASK_QUEUE, taskExpireTime, queueId);
|
|
|
@@ -1465,6 +1462,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
// 计算当前队列中假任务的数量
|
|
|
private long countExistingDummyTasks(Long userId) {
|
|
|
Set<Object> dummyTasks = redisService.zRangeByScore(RedisService.key.MIDJOURNEY_QUEUE_LIMIT.getName() + userId, 0, Double.POSITIVE_INFINITY);
|
|
|
+ Set<ZSetOperations.TypedTuple<Object>> lastTask = redisService.zRangeWithScores(RedisService.key.MIDJOURNEY_QUEUE_LIMIT.getName() + userId, 0, -1);
|
|
|
+ lastTask.forEach(tuple -> System.out.println(tuple.getScore().longValue()));
|
|
|
if (dummyTasks == null) return 0;
|
|
|
|
|
|
return dummyTasks.stream()
|