fix(调度): 双节点下 collect-data/publish/image-video 定时调度加 Redis 分布式锁互斥——双活原来各自扫描执行同一批任务,重复派发/重复终态判定
This commit is contained in:
+36
-17
@@ -16,6 +16,7 @@ import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoWorkflowResultRe
|
||||
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoWorkflowRunRequest;
|
||||
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoAsyncTaskEntity;
|
||||
import com.nanri.aiimage.modules.imagevideo.model.vo.ImageVideoAsyncTaskVo;
|
||||
import com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
@@ -66,6 +67,7 @@ public class ImageVideoAsyncTaskService {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final TaskExecutor taskQueueExecutor;
|
||||
private final InstanceMetadata instanceMetadata;
|
||||
private final com.nanri.aiimage.common.service.DistributedJobLockService distributedJobLockService;
|
||||
|
||||
public ImageVideoAsyncTaskService(
|
||||
ImageVideoAsyncTaskMapper taskMapper,
|
||||
@@ -74,7 +76,8 @@ public class ImageVideoAsyncTaskService {
|
||||
ImageVideoArchiveService archiveService,
|
||||
ObjectMapper objectMapper,
|
||||
@Qualifier("taskQueueExecutor") TaskExecutor taskQueueExecutor,
|
||||
InstanceMetadata instanceMetadata) {
|
||||
InstanceMetadata instanceMetadata,
|
||||
com.nanri.aiimage.common.service.DistributedJobLockService distributedJobLockService) {
|
||||
this.taskMapper = taskMapper;
|
||||
this.cozeService = cozeService;
|
||||
this.workflowConfigService = workflowConfigService;
|
||||
@@ -82,6 +85,7 @@ public class ImageVideoAsyncTaskService {
|
||||
this.objectMapper = objectMapper;
|
||||
this.taskQueueExecutor = taskQueueExecutor;
|
||||
this.instanceMetadata = instanceMetadata;
|
||||
this.distributedJobLockService = distributedJobLockService;
|
||||
}
|
||||
|
||||
public ImageVideoAsyncTaskVo submitDouyinCopy(DouyinCopyRequest request) {
|
||||
@@ -130,26 +134,41 @@ public class ImageVideoAsyncTaskService {
|
||||
|
||||
@Scheduled(fixedDelayString = "${aiimage.image-video.async-task-dispatch-delay-ms:1000}")
|
||||
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 -> taskQueueExecutor.execute(() -> executeTask(task.getId())));
|
||||
// 双节点互斥:只影响扫描入队动作(CAS claim 已兜底不重复执行),省掉另一节点的每秒空扫
|
||||
DistributedJobLockService.LockHandle jobLock =
|
||||
distributedJobLockService.tryLock("image-video:dispatch", java.time.Duration.ofSeconds(5));
|
||||
if (jobLock == null) {
|
||||
return;
|
||||
}
|
||||
try (jobLock) {
|
||||
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 -> taskQueueExecutor.execute(() -> executeTask(task.getId())));
|
||||
}
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "${aiimage.image-video.async-task-poll-delay-ms:5000}")
|
||||
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 -> taskQueueExecutor.execute(() -> pollTask(task.getId())));
|
||||
DistributedJobLockService.LockHandle jobLock =
|
||||
distributedJobLockService.tryLock("image-video:poll", java.time.Duration.ofSeconds(30));
|
||||
if (jobLock == null) {
|
||||
return;
|
||||
}
|
||||
try (jobLock) {
|
||||
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 -> taskQueueExecutor.execute(() -> pollTask(task.getId())));
|
||||
}
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
|
||||
Reference in New Issue
Block a user