Search in sources :

Example 1 with ScriptTaskStateImpl

use of io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl in project seata by seata.

the class ScriptTaskStateHandler method process.

@Override
public void process(ProcessContext context) throws EngineExecutionException {
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    ScriptTaskStateImpl state = (ScriptTaskStateImpl) instruction.getState(context);
    String scriptType = state.getScriptType();
    String scriptContent = state.getScriptContent();
    Object result;
    try {
        List<Object> input = (List<Object>) context.getVariable(DomainConstants.VAR_NAME_INPUT_PARAMS);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(">>>>>>>>>>>>>>>>>>>>>> Start to execute ScriptTaskState[{}], ScriptType[{}], Input:{}", state.getName(), scriptType, input);
        }
        StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
        ScriptEngine scriptEngine = getScriptEngineFromCache(scriptType, stateMachineConfig.getScriptEngineManager());
        if (scriptEngine == null) {
            throw new EngineExecutionException("No such ScriptType[" + scriptType + "]", FrameworkErrorCode.ObjectNotExists);
        }
        Bindings bindings = null;
        Map<String, Object> inputMap = null;
        if (CollectionUtils.isNotEmpty(input) && input.get(0) instanceof Map) {
            inputMap = (Map<String, Object>) input.get(0);
        }
        List<Object> inputExps = state.getInput();
        if (CollectionUtils.isNotEmpty(inputExps) && inputExps.get(0) instanceof Map) {
            Map<String, Object> inputExpMap = (Map<String, Object>) inputExps.get(0);
            if (inputExpMap.size() > 0) {
                bindings = new SimpleBindings();
                for (String property : inputExpMap.keySet()) {
                    if (inputMap != null && inputMap.containsKey(property)) {
                        bindings.put(property, inputMap.get(property));
                    } else {
                        // if we do not bind the null value property, groovy will throw MissingPropertyException
                        bindings.put(property, null);
                    }
                }
            }
        }
        if (bindings != null) {
            result = scriptEngine.eval(scriptContent, bindings);
        } else {
            result = scriptEngine.eval(scriptContent);
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("<<<<<<<<<<<<<<<<<<<<<< ScriptTaskState[{}], ScriptType[{}], Execute finish. result: {}", state.getName(), scriptType, result);
        }
        if (result != null) {
            ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_OUTPUT_PARAMS, result);
        }
    } catch (Throwable e) {
        LOGGER.error("<<<<<<<<<<<<<<<<<<<<<< ScriptTaskState[{}], ScriptTaskState[{}] Execute failed.", state.getName(), scriptType, e);
        ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_CURRENT_EXCEPTION, e);
        EngineUtils.handleException(context, state, e);
    }
}
Also used : StateInstruction(io.seata.saga.engine.pcext.StateInstruction) HierarchicalProcessContext(io.seata.saga.proctrl.HierarchicalProcessContext) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine) ScriptTaskStateImpl(io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl) SimpleBindings(javax.script.SimpleBindings) ArrayList(java.util.ArrayList) List(java.util.List) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 2 with ScriptTaskStateImpl

use of io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl in project seata by seata.

the class ScriptTaskStateParser method parse.

@Override
public ScriptTaskState parse(Object node) {
    ScriptTaskStateImpl scriptTaskState = new ScriptTaskStateImpl();
    parseTaskAttributes(scriptTaskState, node);
    Map<String, Object> nodeMap = (Map<String, Object>) node;
    String scriptType = (String) nodeMap.get("ScriptType");
    if (StringUtils.isNotBlank(scriptType)) {
        scriptTaskState.setScriptType(scriptType);
    }
    scriptTaskState.setScriptContent((String) nodeMap.get("ScriptContent"));
    scriptTaskState.setForCompensation(false);
    scriptTaskState.setForUpdate(false);
    scriptTaskState.setPersist(false);
    return scriptTaskState;
}
Also used : ScriptTaskStateImpl(io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl) Map(java.util.Map)

Example 3 with ScriptTaskStateImpl

use of io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl in project seata by seata.

the class ScriptTaskHandlerInterceptor method postProcess.

@Override
public void postProcess(ProcessContext context, Exception exp) throws EngineExecutionException {
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    ScriptTaskStateImpl state = (ScriptTaskStateImpl) instruction.getState(context);
    StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
    StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    if (exp == null) {
        exp = (Exception) context.getVariable(DomainConstants.VAR_NAME_CURRENT_EXCEPTION);
    }
    Map<String, Object> contextVariables = (Map<String, Object>) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT);
    Object serviceOutputParams = context.getVariable(DomainConstants.VAR_NAME_OUTPUT_PARAMS);
    if (serviceOutputParams != null) {
        try {
            Map<String, Object> outputVariablesToContext = ParameterUtils.createOutputParams(stateMachineConfig.getExpressionFactoryManager(), state, serviceOutputParams);
            if (CollectionUtils.isNotEmpty(outputVariablesToContext)) {
                contextVariables.putAll(outputVariablesToContext);
            }
        } catch (Exception e) {
            String message = "Task [" + state.getName() + "] output parameters assign failed, please check 'Output' expression:" + e.getMessage();
            EngineExecutionException exception = ExceptionUtils.createEngineExecutionException(e, FrameworkErrorCode.VariablesAssignError, message, stateMachineInstance, state.getName());
            EngineUtils.failStateMachine(context, exception);
            throw exception;
        }
    }
    context.removeVariable(DomainConstants.VAR_NAME_OUTPUT_PARAMS);
    context.removeVariable(DomainConstants.VAR_NAME_INPUT_PARAMS);
    if (exp != null && context.getVariable(DomainConstants.VAR_NAME_IS_EXCEPTION_NOT_CATCH) != null && (Boolean) context.getVariable(DomainConstants.VAR_NAME_IS_EXCEPTION_NOT_CATCH)) {
        // If there is an exception and there is no catch, need to exit the state machine to execute.
        context.removeVariable(DomainConstants.VAR_NAME_IS_EXCEPTION_NOT_CATCH);
        EngineUtils.failStateMachine(context, exp);
    }
}
Also used : StateInstruction(io.seata.saga.engine.pcext.StateInstruction) ScriptTaskStateImpl(io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) Map(java.util.Map) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Example 4 with ScriptTaskStateImpl

use of io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl in project seata by seata.

the class ScriptTaskHandlerInterceptor method preProcess.

@Override
public void preProcess(ProcessContext context) throws EngineExecutionException {
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
    StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    Map<String, Object> contextVariables = (Map<String, Object>) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT);
    ScriptTaskStateImpl state = (ScriptTaskStateImpl) instruction.getState(context);
    List<Object> serviceInputParams = null;
    if (contextVariables != null) {
        try {
            serviceInputParams = ParameterUtils.createInputParams(stateMachineConfig.getExpressionFactoryManager(), null, state, contextVariables);
        } catch (Exception e) {
            String message = "Task [" + state.getName() + "] input parameters assign failed, please check 'Input' expression:" + e.getMessage();
            EngineExecutionException exception = ExceptionUtils.createEngineExecutionException(e, FrameworkErrorCode.VariablesAssignError, message, stateMachineInstance, state.getName());
            EngineUtils.failStateMachine(context, exception);
            throw exception;
        }
    }
    ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_INPUT_PARAMS, serviceInputParams);
}
Also used : StateInstruction(io.seata.saga.engine.pcext.StateInstruction) ScriptTaskStateImpl(io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) HierarchicalProcessContext(io.seata.saga.proctrl.HierarchicalProcessContext) Map(java.util.Map) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Aggregations

ScriptTaskStateImpl (io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl)4 Map (java.util.Map)4 StateMachineConfig (io.seata.saga.engine.StateMachineConfig)3 EngineExecutionException (io.seata.saga.engine.exception.EngineExecutionException)3 StateInstruction (io.seata.saga.engine.pcext.StateInstruction)3 HierarchicalProcessContext (io.seata.saga.proctrl.HierarchicalProcessContext)2 StateMachineInstance (io.seata.saga.statelang.domain.StateMachineInstance)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Bindings (javax.script.Bindings)1 ScriptEngine (javax.script.ScriptEngine)1 SimpleBindings (javax.script.SimpleBindings)1