完成上架相关优化、oss迁移
This commit is contained in:
+18
-6
@@ -3,18 +3,30 @@ package com.nanri.aiimage.modules.imagevideo.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoAsyncTaskEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
@Mapper
|
||||
public interface ImageVideoAsyncTaskMapper extends BaseMapper<ImageVideoAsyncTaskEntity> {
|
||||
|
||||
@Update("UPDATE biz_image_video_async_task "
|
||||
+ "SET status = 'RUNNING', updated_at = NOW(), attempt_count = attempt_count + 1 "
|
||||
+ "WHERE id = #{taskId} AND status = 'PENDING'")
|
||||
int claimPending(Long taskId);
|
||||
+ "SET status = 'RUNNING', owner_instance_id = CASE "
|
||||
+ "WHEN owner_instance_id IS NULL OR owner_instance_id = '' THEN #{ownerInstanceId} "
|
||||
+ "ELSE owner_instance_id END, updated_at = NOW(), attempt_count = attempt_count + 1 "
|
||||
+ "WHERE id = #{taskId} AND status = 'PENDING' "
|
||||
+ "AND (owner_instance_id = #{ownerInstanceId} OR owner_instance_id IS NULL OR owner_instance_id = '')")
|
||||
int claimPending(@Param("taskId") Long taskId, @Param("ownerInstanceId") String ownerInstanceId);
|
||||
|
||||
@Update("UPDATE biz_image_video_async_task "
|
||||
+ "SET status = 'POLLING', updated_at = NOW(), attempt_count = attempt_count + 1 "
|
||||
+ "WHERE id = #{taskId} AND status = 'WAITING'")
|
||||
int claimWaiting(Long taskId);
|
||||
+ "SET status = 'POLLING', owner_instance_id = CASE "
|
||||
+ "WHEN owner_instance_id IS NULL OR owner_instance_id = '' THEN #{ownerInstanceId} "
|
||||
+ "ELSE owner_instance_id END, updated_at = NOW(), attempt_count = attempt_count + 1 "
|
||||
+ "WHERE id = #{taskId} AND status = 'WAITING' "
|
||||
+ "AND (owner_instance_id = #{ownerInstanceId} OR owner_instance_id IS NULL OR owner_instance_id = '')")
|
||||
int claimWaiting(@Param("taskId") Long taskId, @Param("ownerInstanceId") String ownerInstanceId);
|
||||
|
||||
@Update("UPDATE biz_image_video_async_task SET status = 'WAITING', updated_at = NOW() "
|
||||
+ "WHERE status = 'POLLING' "
|
||||
+ "AND (owner_instance_id = #{ownerInstanceId} OR owner_instance_id IS NULL OR owner_instance_id = '')")
|
||||
int requeuePollingTasks(@Param("ownerInstanceId") String ownerInstanceId);
|
||||
}
|
||||
|
||||
+1
@@ -14,6 +14,7 @@ public class ImageVideoAsyncTaskEntity {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
private Long userId;
|
||||
private String ownerInstanceId;
|
||||
private String taskType;
|
||||
private String status;
|
||||
private String requestJson;
|
||||
|
||||
+58
-7
@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.exception.TaskOwnerMismatchException;
|
||||
import com.nanri.aiimage.config.InstanceMetadata;
|
||||
import com.nanri.aiimage.modules.imagevideo.mapper.ImageVideoAsyncTaskMapper;
|
||||
import com.nanri.aiimage.modules.imagevideo.model.dto.DouyinCopyRequest;
|
||||
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoVoiceCloneRequest;
|
||||
@@ -15,7 +17,9 @@ import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoWorkflowRunReque
|
||||
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoAsyncTaskEntity;
|
||||
import com.nanri.aiimage.modules.imagevideo.model.vo.ImageVideoAsyncTaskVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -24,6 +28,7 @@ import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
@@ -51,6 +56,7 @@ public class ImageVideoAsyncTaskService {
|
||||
private final ImageVideoArchiveService archiveService;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final TaskExecutor cozeTaskExecutor;
|
||||
private final InstanceMetadata instanceMetadata;
|
||||
|
||||
public ImageVideoAsyncTaskService(
|
||||
ImageVideoAsyncTaskMapper taskMapper,
|
||||
@@ -58,13 +64,15 @@ public class ImageVideoAsyncTaskService {
|
||||
ImageVideoWorkflowConfigService workflowConfigService,
|
||||
ImageVideoArchiveService archiveService,
|
||||
ObjectMapper objectMapper,
|
||||
@Qualifier("cozeTaskExecutor") TaskExecutor cozeTaskExecutor) {
|
||||
@Qualifier("cozeTaskExecutor") TaskExecutor cozeTaskExecutor,
|
||||
InstanceMetadata instanceMetadata) {
|
||||
this.taskMapper = taskMapper;
|
||||
this.cozeService = cozeService;
|
||||
this.workflowConfigService = workflowConfigService;
|
||||
this.archiveService = archiveService;
|
||||
this.objectMapper = objectMapper;
|
||||
this.cozeTaskExecutor = cozeTaskExecutor;
|
||||
this.instanceMetadata = instanceMetadata;
|
||||
}
|
||||
|
||||
public ImageVideoAsyncTaskVo submitDouyinCopy(DouyinCopyRequest request) {
|
||||
@@ -102,6 +110,7 @@ public class ImageVideoAsyncTaskService {
|
||||
if (task == null) {
|
||||
throw new BusinessException("Image video task not found");
|
||||
}
|
||||
ensureTaskOwnedByCurrentInstance(task, "query async task");
|
||||
task = recoverFalseFailedTask(task);
|
||||
ImageVideoAsyncTaskVo result = toVo(task);
|
||||
if (TaskStatus.FAILED.name().equals(task.getStatus())) {
|
||||
@@ -114,6 +123,9 @@ public class ImageVideoAsyncTaskService {
|
||||
public void dispatchPendingTasks() {
|
||||
List<ImageVideoAsyncTaskEntity> tasks = taskMapper.selectList(new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getStatus, TaskStatus.PENDING.name())
|
||||
.and(q -> q.eq(ImageVideoAsyncTaskEntity::getOwnerInstanceId, currentInstanceId())
|
||||
.or().isNull(ImageVideoAsyncTaskEntity::getOwnerInstanceId)
|
||||
.or().eq(ImageVideoAsyncTaskEntity::getOwnerInstanceId, ""))
|
||||
.orderByAsc(ImageVideoAsyncTaskEntity::getId)
|
||||
.last("LIMIT " + DISPATCH_BATCH_SIZE));
|
||||
tasks.forEach(task -> cozeTaskExecutor.execute(() -> executeTask(task.getId())));
|
||||
@@ -123,11 +135,23 @@ public class ImageVideoAsyncTaskService {
|
||||
public void pollWaitingTasks() {
|
||||
List<ImageVideoAsyncTaskEntity> tasks = taskMapper.selectList(new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getStatus, TaskStatus.WAITING.name())
|
||||
.and(q -> q.eq(ImageVideoAsyncTaskEntity::getOwnerInstanceId, currentInstanceId())
|
||||
.or().isNull(ImageVideoAsyncTaskEntity::getOwnerInstanceId)
|
||||
.or().eq(ImageVideoAsyncTaskEntity::getOwnerInstanceId, ""))
|
||||
.orderByAsc(ImageVideoAsyncTaskEntity::getUpdatedAt)
|
||||
.last("LIMIT " + POLL_BATCH_SIZE));
|
||||
tasks.forEach(task -> cozeTaskExecutor.execute(() -> pollTask(task.getId())));
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void recoverInterruptedPollingTasks() {
|
||||
int recovered = taskMapper.requeuePollingTasks(currentInstanceId());
|
||||
if (recovered > 0) {
|
||||
log.warn("[image-video] recovered interrupted polling tasks count={} owner={}",
|
||||
recovered, currentInstanceId());
|
||||
}
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "${aiimage.image-video.failed-task-cleanup-delay-ms:60000}")
|
||||
public void cleanupFailedTasks() {
|
||||
LocalDateTime cutoff = LocalDateTime.now().minusMinutes(FAILED_TASK_RETENTION_MINUTES);
|
||||
@@ -146,6 +170,7 @@ public class ImageVideoAsyncTaskService {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
ImageVideoAsyncTaskEntity task = new ImageVideoAsyncTaskEntity();
|
||||
task.setUserId(userId);
|
||||
task.setOwnerInstanceId(currentInstanceId());
|
||||
task.setTaskType(type.name());
|
||||
task.setStatus(TaskStatus.PENDING.name());
|
||||
task.setRequestJson(writeJson(payload));
|
||||
@@ -154,12 +179,14 @@ public class ImageVideoAsyncTaskService {
|
||||
task.setCreatedAt(now);
|
||||
task.setUpdatedAt(now);
|
||||
taskMapper.insert(task);
|
||||
log.info("[image-video] async task submitted taskId={} type={} owner={}",
|
||||
task.getId(), task.getTaskType(), task.getOwnerInstanceId());
|
||||
cozeTaskExecutor.execute(() -> executeTask(task.getId()));
|
||||
return toVo(task);
|
||||
}
|
||||
|
||||
private void executeTask(Long taskId) {
|
||||
if (taskMapper.claimPending(taskId) != 1) {
|
||||
if (taskMapper.claimPending(taskId, currentInstanceId()) != 1) {
|
||||
return;
|
||||
}
|
||||
ImageVideoAsyncTaskEntity task = taskMapper.selectById(taskId);
|
||||
@@ -189,17 +216,13 @@ public class ImageVideoAsyncTaskService {
|
||||
}
|
||||
|
||||
private void pollTask(Long taskId) {
|
||||
if (taskMapper.claimWaiting(taskId) != 1) {
|
||||
if (taskMapper.claimWaiting(taskId, currentInstanceId()) != 1) {
|
||||
return;
|
||||
}
|
||||
ImageVideoAsyncTaskEntity task = taskMapper.selectById(taskId);
|
||||
if (task == null) {
|
||||
return;
|
||||
}
|
||||
if (task.getSubmittedAt() != null && task.getSubmittedAt().plusHours(1).isBefore(LocalDateTime.now())) {
|
||||
failTask(task, new BusinessException("Coze task exceeded the one-hour polling limit"));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
TaskType type = TaskType.valueOf(task.getTaskType());
|
||||
String workflowId = workflowIdFor(type);
|
||||
@@ -216,12 +239,20 @@ public class ImageVideoAsyncTaskService {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (pollingDeadlineExceeded(task)) {
|
||||
failTask(task, new BusinessException("Coze task exceeded the one-hour polling limit"));
|
||||
return;
|
||||
}
|
||||
task.setStatus(TaskStatus.WAITING.name());
|
||||
task.setCozeStatus(cozeStatus);
|
||||
task.setUpdatedAt(LocalDateTime.now());
|
||||
taskMapper.updateById(task);
|
||||
} catch (Exception ex) {
|
||||
// Coze history calls are retried by the next polling cycle until the task's overall deadline.
|
||||
if (pollingDeadlineExceeded(task)) {
|
||||
failTask(task, new BusinessException("Coze task exceeded the one-hour polling limit", ex));
|
||||
return;
|
||||
}
|
||||
task.setStatus(TaskStatus.WAITING.name());
|
||||
task.setErrorMessage(truncate(messageOf(ex)));
|
||||
task.setUpdatedAt(LocalDateTime.now());
|
||||
@@ -493,6 +524,26 @@ public class ImageVideoAsyncTaskService {
|
||||
return value.length() <= 1000 ? value : value.substring(0, 1000);
|
||||
}
|
||||
|
||||
private boolean pollingDeadlineExceeded(ImageVideoAsyncTaskEntity task) {
|
||||
return task.getSubmittedAt() != null
|
||||
&& task.getSubmittedAt().plusHours(1).isBefore(LocalDateTime.now());
|
||||
}
|
||||
|
||||
private void ensureTaskOwnedByCurrentInstance(ImageVideoAsyncTaskEntity task, String operation) {
|
||||
String owner = task == null ? null : task.getOwnerInstanceId();
|
||||
if (owner == null || owner.isBlank() || Objects.equals(owner, currentInstanceId())) {
|
||||
return;
|
||||
}
|
||||
log.info("[image-video] route task operation to owner taskId={} operation={} owner={} current={}",
|
||||
task.getId(), operation, owner, currentInstanceId());
|
||||
throw new TaskOwnerMismatchException(task.getId(), operation, owner, currentInstanceId());
|
||||
}
|
||||
|
||||
private String currentInstanceId() {
|
||||
String instanceId = instanceMetadata == null ? null : instanceMetadata.getInstanceId();
|
||||
return instanceId == null || instanceId.isBlank() ? "unknown-instance" : instanceId;
|
||||
}
|
||||
|
||||
private enum TaskStatus {
|
||||
PENDING, RUNNING, WAITING, POLLING, SUCCESS, FAILED
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user