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);
|
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.AggregationTaskCreateRequest;
|
||||||
import com.ruoyi.system.domain.vo.WalletOperationRequest;
|
import com.ruoyi.system.domain.vo.WalletOperationRequest;
|
||||||
import com.ruoyi.system.domain.vo.WalletResourceEstimateVo;
|
import com.ruoyi.system.domain.vo.WalletResourceEstimateVo;
|
||||||
|
import com.ruoyi.system.mapper.AppsMapper;
|
||||||
import com.ruoyi.system.mapper.UsdtAggregationTaskItemMapper;
|
import com.ruoyi.system.mapper.UsdtAggregationTaskItemMapper;
|
||||||
import com.ruoyi.system.mapper.UsdtAggregationTaskMapper;
|
import com.ruoyi.system.mapper.UsdtAggregationTaskMapper;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -34,6 +35,7 @@ public class UsdtAggregationWorkflowService {
|
|||||||
private final IWalletAddressService walletService;
|
private final IWalletAddressService walletService;
|
||||||
private final IUsdtAggregationTaskService taskService;
|
private final IUsdtAggregationTaskService taskService;
|
||||||
private final IUsdtAggregationTaskItemService itemService;
|
private final IUsdtAggregationTaskItemService itemService;
|
||||||
|
private final AppsMapper appsMapper;
|
||||||
private final UsdtAggregationTaskMapper taskMapper;
|
private final UsdtAggregationTaskMapper taskMapper;
|
||||||
private final UsdtAggregationTaskItemMapper itemMapper;
|
private final UsdtAggregationTaskItemMapper itemMapper;
|
||||||
private final WalletOperationEstimateService estimateService;
|
private final WalletOperationEstimateService estimateService;
|
||||||
@@ -45,6 +47,7 @@ public class UsdtAggregationWorkflowService {
|
|||||||
IWalletAddressService walletService,
|
IWalletAddressService walletService,
|
||||||
IUsdtAggregationTaskService taskService,
|
IUsdtAggregationTaskService taskService,
|
||||||
IUsdtAggregationTaskItemService itemService,
|
IUsdtAggregationTaskItemService itemService,
|
||||||
|
AppsMapper appsMapper,
|
||||||
UsdtAggregationTaskMapper taskMapper,
|
UsdtAggregationTaskMapper taskMapper,
|
||||||
UsdtAggregationTaskItemMapper itemMapper,
|
UsdtAggregationTaskItemMapper itemMapper,
|
||||||
WalletOperationEstimateService estimateService,
|
WalletOperationEstimateService estimateService,
|
||||||
@@ -54,6 +57,7 @@ public class UsdtAggregationWorkflowService {
|
|||||||
this.walletService = walletService;
|
this.walletService = walletService;
|
||||||
this.taskService = taskService;
|
this.taskService = taskService;
|
||||||
this.itemService = itemService;
|
this.itemService = itemService;
|
||||||
|
this.appsMapper = appsMapper;
|
||||||
this.taskMapper = taskMapper;
|
this.taskMapper = taskMapper;
|
||||||
this.itemMapper = itemMapper;
|
this.itemMapper = itemMapper;
|
||||||
this.estimateService = estimateService;
|
this.estimateService = estimateService;
|
||||||
@@ -65,6 +69,16 @@ public class UsdtAggregationWorkflowService {
|
|||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Long create(AggregationTaskCreateRequest request, boolean userMode) {
|
public Long create(AggregationTaskCreateRequest request, boolean userMode) {
|
||||||
AppsPlus app = resolveApp(request, 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())) {
|
if (StringUtils.isBlank(app.getDistTokenAddress())) {
|
||||||
throw new CustomException("应用未配置主账号归集地址");
|
throw new CustomException("应用未配置主账号归集地址");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
where id = #{id}
|
where id = #{id}
|
||||||
</select>
|
</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 id="insertUsdtAggregationTask" parameterType="UsdtAggregationTask" useGeneratedKeys="true" keyProperty="id">
|
||||||
insert into usdt_aggregation_task
|
insert into usdt_aggregation_task
|
||||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
|
|||||||
Reference in new issue
Block a user