fix: prevent duplicate pending aggregation tasks
This commit is contained in:
1 parent
0bad1e2a40
commit
dd28f3c0cb
3 files changed
+34
No files matched your search
@@ -31,6 +31,16 @@ public interface UsdtAggregationTaskMapper extends BaseMapper<UsdtAggregationTas
|
||||
*/
|
||||
public List<UsdtAggregationTask> selectUsdtAggregationTaskList(UsdtAggregationTask usdtAggregationTask);
|
||||
|
||||
/**
|
||||
* 查询指定应用当前有效的待执行任务。
|
||||
*
|
||||
* <p>待审核和审核通过但尚未执行的任务均会阻止重复创建;审核拒绝的任务不参与判断。</p>
|
||||
*
|
||||
* @param appId 应用ID
|
||||
* @return 最早创建的有效待执行任务,不存在时返回null
|
||||
*/
|
||||
UsdtAggregationTask selectPendingTaskByAppId(@Param("appId") Long appId);
|
||||
|
||||
/**
|
||||
* 新增归集任务
|
||||
*
|
||||
|
||||
+14
@@ -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("应用未配置主账号归集地址");
|
||||
}
|
||||
|
||||
@@ -40,6 +40,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="selectPendingTaskByAppId" resultMap="UsdtAggregationTaskResult">
|
||||
<include refid="selectUsdtAggregationTaskVo"/>
|
||||
where app_id = #{appId}
|
||||
and status = 0
|
||||
and verify_status in (0, 1)
|
||||
and deleted_at is null
|
||||
order by created_at asc, id asc
|
||||
limit 1
|
||||
</select>
|
||||
|
||||
<insert id="insertUsdtAggregationTask" parameterType="UsdtAggregationTask" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into usdt_aggregation_task
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
|
||||
Reference in new issue
Block a user