视频任务管理更新

This commit is contained in:
super
2026-07-19 22:23:26 +08:00
parent f189a5b4eb
commit 5e12e7ea52
62 changed files with 2164 additions and 280 deletions
@@ -100,7 +100,7 @@ public class ImageVideoController {
}
@GetMapping("/tasks/{taskId}")
@Operation(summary = "Query image video async task")
@Operation(summary = "查询图生视频异步任务")
public ApiResponse<ImageVideoAsyncTaskVo> getTask(
@PathVariable Long taskId,
@RequestParam("user_id") Long userId) {
@@ -35,6 +35,8 @@ public class ImageVideoArchiveService {
static final String TASK_TYPE = "IMAGE_VIDEO_WORKFLOW";
private static final int BATCH_SIZE = 20;
private static final int RETENTION_CLEANUP_BATCH_SIZE = 200;
private static final int RETENTION_DAYS = 3;
private static final Set<String> VIDEO_KEYS = Set.of(
"videourl", "video", "outputvideourl", "resultvideourl"
);
@@ -103,6 +105,47 @@ public class ImageVideoArchiveService {
}
}
@Scheduled(fixedDelayString = "${aiimage.image-video.retention-cleanup-delay-ms:600000}")
public void cleanupExpiredWorkflowTasks() {
LocalDateTime cutoff = LocalDateTime.now().minusDays(RETENTION_DAYS);
List<ImageVideoAsyncTaskEntity> expiredTasks = taskMapper.selectList(
new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
.eq(ImageVideoAsyncTaskEntity::getTaskType, TASK_TYPE)
.lt(ImageVideoAsyncTaskEntity::getSubmittedAt, cutoff)
.orderByAsc(ImageVideoAsyncTaskEntity::getId)
.last("LIMIT " + RETENTION_CLEANUP_BATCH_SIZE));
int deleted = 0;
for (ImageVideoAsyncTaskEntity task : expiredTasks) {
if (!deleteArchivedObjects(task)) {
continue;
}
deleted += taskMapper.deleteById(task.getId());
}
if (deleted > 0) {
log.info("[image-video] expired workflow task cleanup deleted={} cutoff={}", deleted, cutoff);
}
}
private boolean deleteArchivedObjects(ImageVideoAsyncTaskEntity task) {
Set<String> objectKeys = new LinkedHashSet<>();
for (Map<String, Object> archivedVideo : readArchivedVideos(task.getArchivedVideosJson())) {
Object objectKey = archivedVideo.get("objectKey");
if (objectKey != null && !String.valueOf(objectKey).isBlank()) {
objectKeys.add(String.valueOf(objectKey));
}
}
for (String objectKey : objectKeys) {
try {
ossStorageService.deleteObject(objectKey);
} catch (Exception ex) {
log.warn("[image-video] expired task OSS cleanup failed taskId={} objectKey={} error={}",
task.getId(), objectKey, ex.getMessage());
return false;
}
}
return true;
}
private void backfillDerivedFields(ImageVideoAsyncTaskEntity task) {
if (task.getArchiveStatus() != null) {
return;
@@ -32,6 +32,7 @@ public class ImageVideoAsyncTaskService {
private static final int DISPATCH_BATCH_SIZE = 20;
private static final int POLL_BATCH_SIZE = 50;
private static final int FAILED_TASK_RETENTION_MINUTES = 10;
private static final Set<String> TERMINAL_STATUSES = Set.of(
"SUCCESS", "SUCCEEDED", "COMPLETED", "DONE", "FINISHED",
"FAILED", "FAIL", "ERROR", "CANCELED", "CANCELLED"
@@ -102,7 +103,11 @@ public class ImageVideoAsyncTaskService {
throw new BusinessException("Image video task not found");
}
task = recoverFalseFailedTask(task);
return toVo(task);
ImageVideoAsyncTaskVo result = toVo(task);
if (TaskStatus.FAILED.name().equals(task.getStatus())) {
taskMapper.deleteById(task.getId());
}
return result;
}
@Scheduled(fixedDelayString = "${aiimage.image-video.async-task-dispatch-delay-ms:1000}")
@@ -123,6 +128,17 @@ public class ImageVideoAsyncTaskService {
tasks.forEach(task -> cozeTaskExecutor.execute(() -> pollTask(task.getId())));
}
@Scheduled(fixedDelayString = "${aiimage.image-video.failed-task-cleanup-delay-ms:60000}")
public void cleanupFailedTasks() {
LocalDateTime cutoff = LocalDateTime.now().minusMinutes(FAILED_TASK_RETENTION_MINUTES);
int deleted = taskMapper.delete(new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
.eq(ImageVideoAsyncTaskEntity::getStatus, TaskStatus.FAILED.name())
.lt(ImageVideoAsyncTaskEntity::getUpdatedAt, cutoff));
if (deleted > 0) {
log.info("[image-video] removed expired failed tasks count={}", deleted);
}
}
private ImageVideoAsyncTaskVo submit(TaskType type, Long userId, Object payload) {
if (userId == null || userId <= 0) {
throw new BusinessException("userId is required");