Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.weibo.rill.flow.olympicene.traversal.runners.SwitchTaskRunner Maven / Gradle / Ivy
package com.weibo.rill.flow.olympicene.traversal.runners;
import com.jayway.jsonpath.JsonPath;
import com.weibo.rill.flow.interfaces.model.task.TaskInfo;
import com.weibo.rill.flow.interfaces.model.task.TaskInvokeMsg;
import com.weibo.rill.flow.interfaces.model.task.TaskStatus;
import com.weibo.rill.flow.olympicene.core.helper.DAGWalkHelper;
import com.weibo.rill.flow.olympicene.core.model.task.ExecutionResult;
import com.weibo.rill.flow.olympicene.core.model.task.Switch;
import com.weibo.rill.flow.olympicene.core.model.task.SwitchTask;
import com.weibo.rill.flow.olympicene.core.model.task.TaskCategory;
import com.weibo.rill.flow.olympicene.core.runtime.DAGContextStorage;
import com.weibo.rill.flow.olympicene.core.runtime.DAGInfoStorage;
import com.weibo.rill.flow.olympicene.core.runtime.DAGStorageProcedure;
import com.weibo.rill.flow.olympicene.core.switcher.SwitcherManager;
import com.weibo.rill.flow.olympicene.traversal.mappings.InputOutputMapping;
import com.weibo.rill.flow.olympicene.traversal.utils.ConditionsUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
@Slf 4j
public class SwitchTaskRunner extends AbstractTaskRunner {
private static final String DEFAULT_CONDITION = "default" ;
public SwitchTaskRunner (InputOutputMapping inputOutputMapping, DAGInfoStorage dagInfoStorage,
DAGContextStorage dagContextStorage, DAGStorageProcedure dagStorageProcedure,
SwitcherManager switcherManager) {
super (inputOutputMapping, dagInfoStorage, dagContextStorage, dagStorageProcedure, switcherManager);
}
@Override
public TaskCategory getCategory () {
return TaskCategory.SWITCH;
}
@Override
public String getIcon () {
return "ant-design:branches-outlined" ;
}
@Override
protected ExecutionResult doRun (String executionId, TaskInfo taskInfo, Map input) {
log.info("switch task begin to run executionId:{}, taskInfoName:{}" , executionId, taskInfo.getName());
SwitchTask switchTask = (SwitchTask) taskInfo.getTask();
List switches = switchTask.getSwitches();
if (CollectionUtils.isEmpty(switches)) {
TaskInvokeMsg taskInvokeMsg = TaskInvokeMsg.builder().msg("switches collection empty" ).build();
taskInfo.updateInvokeMsg(taskInvokeMsg);
updateTaskInvokeEndTime(taskInfo);
taskInfo.setTaskStatus(TaskStatus.SUCCEED);
dagInfoStorage.saveTaskInfos(executionId, Set.of(taskInfo));
return ExecutionResult.builder().taskStatus(taskInfo.getTaskStatus()).build();
}
Set skipTaskNames = calculateConditions(taskInfo, input, switches);
taskInfo.getSkipNextTaskNames().addAll(skipTaskNames);
taskInfo.setTaskStatus(TaskStatus.SUCCEED);
updateTaskInvokeEndTime(taskInfo);
dagInfoStorage.saveTaskInfos(executionId, Set.of(taskInfo));
log.info("run switch task completed, executionId:{}, taskInfoName:{}" , executionId, taskInfo.getName());
return ExecutionResult.builder().taskStatus(taskInfo.getTaskStatus()).build();
}
private static Set calculateConditions (TaskInfo taskInfo, Map input, List switches) {
Set skipTaskNames = new HashSet<>();
Set runTaskNames = new HashSet<>();
DefaultSwitch defaultSwitch = new DefaultSwitch();
switches.forEach(it -> {
if (DEFAULT_CONDITION.equals(it.getCondition())) {
defaultSwitch.getDefaultConditions().add(it);
} else {
boolean condition = calculateCondition(taskInfo, input, it, skipTaskNames, runTaskNames, defaultSwitch);
if (condition) {
defaultSwitch.setNeedDefault(false );
}
if (condition && it.isBreak()) {
defaultSwitch.setBroken(true );
}
}
});
defaultSwitch.getDefaultConditions().forEach(it -> calculateCondition(taskInfo, input, it, skipTaskNames, runTaskNames, defaultSwitch));
runTaskNames.forEach(skipTaskNames::remove);
return skipTaskNames;
}
@Data
private static class DefaultSwitch {
private List defaultConditions = new ArrayList<>();
private boolean needDefault = true ;
private boolean isBroken = false ;
}
private static boolean calculateCondition (TaskInfo taskInfo, Map input, Switch switchObj,
Set skipTaskNames, Set runTaskNames, DefaultSwitch defaultSwitch) {
DAGWalkHelper dagWalkHelper = DAGWalkHelper.getInstance();
boolean isAncestorTask = dagWalkHelper.isAncestorTask(taskInfo.getName());
String rootName = isAncestorTask? null : dagWalkHelper.getRootName(taskInfo.getName());
Set nextTaskNames = Arrays.stream(switchObj.getNext().split("," )).map(String::trim).filter(StringUtils::isNotBlank)
.map(it -> isAncestorTask ? it: dagWalkHelper.buildTaskInfoName(rootName, it))
.collect(Collectors.toSet());
boolean condition = judgeCondition(taskInfo, input, switchObj, defaultSwitch);
if (!condition) {
skipTaskNames.addAll(nextTaskNames);
} else {
runTaskNames.addAll(nextTaskNames);
}
return condition;
}
private static boolean judgeCondition (TaskInfo taskInfo, Map input, Switch switchObj, DefaultSwitch defaultSwitch) {
if (defaultSwitch.isBroken()) {
return false ;
}
if (DEFAULT_CONDITION.equals(switchObj.getCondition())) {
return defaultSwitch.isNeedDefault();
}
try {
List result = JsonPath.using(ConditionsUtil.valuePathConf)
.parse(Map.of("input" , input)).read(switchObj.getCondition());
return !result.isEmpty();
} catch (Exception e) {
log.warn("switchTask {} evaluation condition expression {} exception. " ,
taskInfo.getName(), switchObj.getCondition(), e);
return false ;
}
}
}