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);
}
}
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;
}
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);
}
}
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);
}
Aggregations