From dd28f3c0cbcd395d77fbed64e0e7c4660155cb72 Mon Sep 17 00:00:00 2001 From: luqingjie <544309509@qq.com> Date: Wed, 14 Oct 2026 22:49:52 -0400 Subject: [PATCH] fix: prevent duplicate pending aggregation tasks --- .../system/mapper/UsdtAggregationTaskMapper.java | 10 ++++++++++ .../service/UsdtAggregationWorkflowService.java | 14 ++++++++++++++ .../mapper/system/UsdtAggregationTaskMapper.xml | 12 +++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/UsdtAggregationTaskMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/UsdtAggregationTaskMapper.java index 40f56a0..ff6f8d9 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/UsdtAggregationTaskMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/UsdtAggregationTaskMapper.java @@ -31,6 +31,16 @@ public interface UsdtAggregationTaskMapper extends BaseMapper selectUsdtAggregationTaskList(UsdtAggregationTask usdtAggregationTask); + /** + * 查询指定应用当前有效的待执行任务。 + * + *

待审核和审核通过但尚未执行的任务均会阻止重复创建;审核拒绝的任务不参与判断。

+ * + * @param appId 应用ID + * @return 最早创建的有效待执行任务,不存在时返回null + */ + UsdtAggregationTask selectPendingTaskByAppId(@Param("appId") Long appId); + /** * 新增归集任务 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java index 5985bbc..7164c08 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java @@ -14,6 +14,7 @@ import com.ruoyi.system.domain.WalletAddressPlus; import com.ruoyi.system.domain.vo.AggregationTaskCreateRequest; import com.ruoyi.system.domain.vo.WalletOperationRequest; import com.ruoyi.system.domain.vo.WalletResourceEstimateVo; +import com.ruoyi.system.mapper.AppsMapper; import com.ruoyi.system.mapper.UsdtAggregationTaskItemMapper; import com.ruoyi.system.mapper.UsdtAggregationTaskMapper; import org.springframework.stereotype.Service; @@ -34,6 +35,7 @@ public class UsdtAggregationWorkflowService { private final IWalletAddressService walletService; private final IUsdtAggregationTaskService taskService; private final IUsdtAggregationTaskItemService itemService; + private final AppsMapper appsMapper; private final UsdtAggregationTaskMapper taskMapper; private final UsdtAggregationTaskItemMapper itemMapper; private final WalletOperationEstimateService estimateService; @@ -45,6 +47,7 @@ public class UsdtAggregationWorkflowService { IWalletAddressService walletService, IUsdtAggregationTaskService taskService, IUsdtAggregationTaskItemService itemService, + AppsMapper appsMapper, UsdtAggregationTaskMapper taskMapper, UsdtAggregationTaskItemMapper itemMapper, WalletOperationEstimateService estimateService, @@ -54,6 +57,7 @@ public class UsdtAggregationWorkflowService { this.walletService = walletService; this.taskService = taskService; this.itemService = itemService; + this.appsMapper = appsMapper; this.taskMapper = taskMapper; this.itemMapper = itemMapper; this.estimateService = estimateService; @@ -65,6 +69,16 @@ public class UsdtAggregationWorkflowService { @Transactional(rollbackFor = Exception.class) public Long create(AggregationTaskCreateRequest request, boolean userMode) { AppsPlus app = resolveApp(request, userMode); + /* + * 锁定应用行,使同一应用的归集任务创建串行化。仅执行“先查询再插入”无法阻止 + * 两个并发事务同时通过检查,应用行锁可以在不改表结构的前提下关闭该竞态窗口。 + */ + appsMapper.selectAppsByIdForUpdate(app.getId()); + UsdtAggregationTask pendingTask = taskMapper.selectPendingTaskByAppId(app.getId()); + if (pendingTask != null) { + throw new CustomException("该应用已存在待执行的归集任务:" + + pendingTask.getTaskName() + "(任务ID:" + pendingTask.getId() + ")"); + } if (StringUtils.isBlank(app.getDistTokenAddress())) { throw new CustomException("应用未配置主账号归集地址"); } diff --git a/ruoyi-system/src/main/resources/mapper/system/UsdtAggregationTaskMapper.xml b/ruoyi-system/src/main/resources/mapper/system/UsdtAggregationTaskMapper.xml index 92d15e6..2ed35c1 100644 --- a/ruoyi-system/src/main/resources/mapper/system/UsdtAggregationTaskMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/UsdtAggregationTaskMapper.xml @@ -39,6 +39,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where id = #{id} + + insert into usdt_aggregation_task @@ -98,4 +108,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{id} - \ No newline at end of file +