聊聊PowerJob的Evaluator

2024-01-12 09:11:08 浏览数 (2)

本文主要研究一下PowerJob的Evaluator

Evaluator

tech/powerjob/server/core/evaluator/Evaluator.java

代码语言:javascript复制
public interface Evaluator {
    /**
     * 使用给定输入计算表达式
     *
     * @param expression 可执行的表达式
     * @param input      输入
     * @return 计算结果
     */
    Object evaluate(String expression, Object input);

}

Evaluator接口定义了evaluate方法,它有expression和input两个参数,返回计算结果

GroovyEvaluator

tech/powerjob/server/core/evaluator/GroovyEvaluator.java

代码语言:javascript复制
@Slf4j
@Component
public class GroovyEvaluator implements Evaluator {

    private static final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("groovy");


    @Override
    @SneakyThrows
    public Object evaluate(String expression, Object input) {
        Bindings bindings = ENGINE.createBindings();
        bindings.put("context", input);
        return ENGINE.eval(expression, bindings);
    }

}

GroovyEvaluator实现了Evaluator,它内置了一个groovy的ScriptEngine,其evaluate方法先创建bindings,然后传入参数,最后通过ENGINE.eval(expression, bindings)计算结果

DecisionNodeHandler

tech/powerjob/server/core/workflow/hanlder/impl/DecisionNodeHandler.java

代码语言:javascript复制
@Slf4j
@Component
public class DecisionNodeHandler implements ControlNodeHandler {

    private final GroovyEvaluator groovyEvaluator = new GroovyEvaluator();

    /**
     * 处理判断节点
     * 1. 执行脚本
     * 2. 根据返回值 disable 掉相应的边以及节点
     */
    @Override
    public void handle(PEWorkflowDAG.Node node, PEWorkflowDAG dag, WorkflowInstanceInfoDO wfInstanceInfo) {
        String script = node.getNodeParams();
        if (StringUtils.isBlank(script)) {
            log.error("[Workflow-{}|{}]decision node's param is blank! nodeId:{}", wfInstanceInfo.getWorkflowId(), wfInstanceInfo.getWfInstanceId(), node.getNodeId());
            throw new PowerJobException("decision node's param is blank!");
        }
        // wfContext must be a map
        HashMap<String, String> wfContext = JSON.parseObject(wfInstanceInfo.getWfContext(), new TypeReference<HashMap<String, String>>() {
        });
        Object result;
        try {
            result = groovyEvaluator.evaluate(script, wfContext);
        } catch (Exception e) {
            log.error("[Workflow-{}|{}]failed to evaluate decision node,nodeId:{}", wfInstanceInfo.getWorkflowId(), wfInstanceInfo.getWfInstanceId(), node.getNodeId(), e);
            throw new PowerJobException("can't evaluate decision node!");
        }
        boolean finalRes;
        if (result instanceof Boolean) {
            finalRes = ((Boolean) result);
        } else if (result instanceof Number) {
            finalRes = ((Number) result).doubleValue() > 0;
        } else {
            log.error("[Workflow-{}|{}]decision node's return value is illegal,nodeId:{},result:{}", wfInstanceInfo.getWorkflowId(), wfInstanceInfo.getWfInstanceId(), node.getNodeId(), JsonUtils.toJSONString(result));
            throw new PowerJobException("decision node's return value is illegal!");
        }
        handleDag(finalRes, node, dag);
    }

    //......
}    

DecisionNodeHandler实例化了groovyEvaluator,其handle方法通过groovyEvaluator.evaluate(script, wfContext)来计算node的结果

小结

PowerJob的Evaluator接口定义了evaluate方法,它有expression和input两个参数,返回计算结果;GroovyEvaluator实现了Evaluator,它内置了一个groovy的ScriptEngine,其evaluate方法先创建bindings,然后传入参数,最后通过ENGINE.eval(expression, bindings)计算结果。

0 人点赞