Skip to content

Commit

Permalink
bugfix: 作业模板更新后,调试时使用的变量不是最新模板中的值 TencentBlueKing#1519
Browse files Browse the repository at this point in the history
获取调试执行方案时使用最新模板中的变量覆盖调试执行方案中的变量
  • Loading branch information
jsonwan committed Dec 5, 2022
1 parent 1a268fb commit d56c566
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public interface TaskVariableDAO {
*/
boolean deleteVariableById(long parentId, long id);

/**
* 根据 父资源ID 删除变量信息
*
* @param parentId 父资源ID
* @return 是否删除成功
*/
int deleteVariableByParentId(long parentId);

/**
* 保留变量 ID 批量插入变量信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ public boolean deleteVariableById(long parentId, long id) {
return 1 == context.deleteFrom(TABLE).where(conditions).limit(1).execute();
}

@Override
public int deleteVariableByParentId(long parentId) {
List<Condition> conditions = new ArrayList<>();
conditions.add(TABLE.PLAN_ID.eq(ULong.valueOf(parentId)));
return context.deleteFrom(TABLE).where(conditions).execute();
}

@Override
public boolean batchInsertVariableWithId(List<TaskVariableDTO> variableList) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ public boolean deleteVariableById(long parentId, long id) {
return 1 == context.deleteFrom(TABLE).where(conditions).limit(1).execute();
}

@Override
public int deleteVariableByParentId(long parentId) {
List<Condition> conditions = new ArrayList<>();
conditions.add(TABLE.TEMPLATE_ID.eq(ULong.valueOf(parentId)));
return context.deleteFrom(TABLE).where(conditions).execute();
}

@Override
public boolean batchInsertVariableWithId(List<TaskVariableDTO> variableList) {
InsertValuesStep8<TaskTemplateVariableRecord, ULong, ULong, String, UByte, String, String, UByte,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public boolean deleteVariableById(Long parentId, Long id) {
return taskVariableDAO.deleteVariableById(parentId, id);
}

/**
* 根据 父资源ID 删除变量
*
* @param parentId 父资源 ID
* @return 删除的数据条数
*/
public int deleteVariableByParentId(Long parentId) {
return taskVariableDAO.deleteVariableByParentId(parentId);
}

/**
* 根据变量 ID 拉取变量信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public class TaskPlanServiceImpl implements TaskPlanService {
private final TaskPlanDAO taskPlanDAO;
private final MessageI18nService i18nService;
private final CronJobService cronJobService;
private AbstractTaskStepService taskPlanStepService;
private AbstractTaskVariableService taskPlanVariableService;
private final AbstractTaskStepService taskPlanStepService;
private final AbstractTaskVariableService taskTemplateVariableService;
private final AbstractTaskVariableService taskPlanVariableService;

@Autowired
private TaskTemplateService taskTemplateService;
Expand All @@ -92,11 +93,13 @@ public class TaskPlanServiceImpl implements TaskPlanService {
public TaskPlanServiceImpl(
TaskPlanDAO taskPlanDAO,
@Qualifier("TaskPlanStepServiceImpl") AbstractTaskStepService taskPlanStepService,
@Qualifier("TaskTemplateVariableServiceImpl") AbstractTaskVariableService taskTemplateVariableService,
@Qualifier("TaskPlanVariableServiceImpl") AbstractTaskVariableService taskPlanVariableService,
MessageI18nService i18nService, CronJobService cronJobService
) {
this.taskPlanDAO = taskPlanDAO;
this.taskPlanStepService = taskPlanStepService;
this.taskTemplateVariableService = taskTemplateVariableService;
this.taskPlanVariableService = taskPlanVariableService;
this.i18nService = i18nService;
this.cronJobService = cronJobService;
Expand Down Expand Up @@ -296,9 +299,22 @@ public TaskPlanInfoDTO getDebugTaskPlan(String username, Long appId, Long templa
}
if (taskPlan != null) {
if (!taskTemplateInfo.getVersion().equals(taskPlan.getVersion())) {
// 作业模板有更新,需要同步到调试执行方案(不含变量)
taskPlanService.sync(appId, templateId, taskPlan.getId(), taskTemplateInfo.getVersion());
taskPlan = taskPlanDAO.getDebugTaskPlan(appId, templateId);
}
List<TaskVariableDTO> templateVariableList =
taskTemplateVariableService.listVariablesByParentId(taskPlan.getTemplateId());
// 始终使用模板最新变量作为执行方案变量并更新到DB
setPlanIdForVariables(taskPlan.getId(), templateVariableList);
int deletedVarNum = taskPlanVariableService.deleteVariableByParentId(taskPlan.getId());
List<Long> insertedVarIds = taskPlanVariableService.batchInsertVariable(templateVariableList);
log.debug(
"sync template variable to debug plan:{} taskPlan variable deleted, {} variable inserted:{}",
deletedVarNum,
insertedVarIds.size(),
insertedVarIds
);
taskPlan.setStepList(taskPlanStepService.listStepsByParentId(taskPlan.getId()));
taskPlan.setVariableList(taskPlanVariableService.listVariablesByParentId(taskPlan.getId()));
return taskPlan;
Expand Down Expand Up @@ -330,6 +346,13 @@ public TaskPlanInfoDTO getDebugTaskPlan(String username, Long appId, Long templa
return taskPlan;
}

private void setPlanIdForVariables(Long planId, List<TaskVariableDTO> variableList) {
if (CollectionUtils.isEmpty(variableList)) {
return;
}
variableList.forEach(variable -> variable.setPlanId(planId));
}

@Override
public List<TaskPlanInfoDTO> listPlanBasicInfoByIds(Long appId, List<Long> planIdList) {
List<TaskPlanInfoDTO> taskPlanInfoList = taskPlanDAO.listTaskPlanByIds(
Expand Down

0 comments on commit d56c566

Please sign in to comment.