use of io.automatiko.engine.workflow.base.instance.impl.Action in project automatiko-engine by automatiko-io.
the class DefaultExceptionScopeInstance method handleException.
public void handleException(io.automatiko.engine.api.runtime.process.NodeInstance nodeInstance, ExceptionHandler handler, String exception, Object params) {
if (handler instanceof ActionExceptionHandler) {
ActionExceptionHandler exceptionHandler = (ActionExceptionHandler) handler;
if (retryAvailable(nodeInstance, exceptionHandler)) {
Integer retryAttempts = ((NodeInstanceImpl) nodeInstance).getRetryAttempts();
if (retryAttempts == null) {
retryAttempts = 1;
} else {
retryAttempts = retryAttempts + 1;
}
long delay = calculateDelay(exceptionHandler.getRetryAfter().longValue(), retryAttempts, exceptionHandler.getRetryIncrement(), exceptionHandler.getRetryIncrementMultiplier());
DurationExpirationTime expirationTime = DurationExpirationTime.after(delay);
JobsService jobService = getProcessInstance().getProcessRuntime().getJobsService();
String jobId = jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(nodeInstance.getNodeId(), "retry:" + nodeInstance.getId(), expirationTime, ((NodeInstanceImpl) nodeInstance).getProcessInstanceIdWithParent(), getProcessInstance().getRootProcessInstanceId(), getProcessInstance().getProcessId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getRootProcessId()));
((NodeInstanceImpl) nodeInstance).internalSetRetryJobId(jobId);
((NodeInstanceImpl) nodeInstance).internalSetRetryAttempts(retryAttempts);
((NodeInstanceImpl) nodeInstance).registerRetryEventListener();
if (nodeInstance instanceof WorkItemNodeInstance) {
((WorkItemImpl) ((WorkItemNodeInstance) nodeInstance).getWorkItem()).setState(WorkItem.RETRYING);
}
} else {
Action action = (Action) exceptionHandler.getAction().getMetaData("Action");
try {
ProcessInstance processInstance = getProcessInstance();
ProcessContext processContext = new ProcessContext(processInstance.getProcessRuntime());
ContextInstanceContainer contextInstanceContainer = getContextInstanceContainer();
if (contextInstanceContainer instanceof NodeInstance) {
processContext.setNodeInstance((NodeInstance) contextInstanceContainer);
} else {
processContext.setProcessInstance(processInstance);
}
String faultVariable = exceptionHandler.getFaultVariable();
if (faultVariable != null) {
processContext.setVariable(faultVariable, params);
}
action.execute(processContext);
} catch (Exception e) {
throw new RuntimeException("unable to execute Action", e);
}
}
} else {
throw new IllegalArgumentException("Unknown exception handler " + handler);
}
}
use of io.automatiko.engine.workflow.base.instance.impl.Action in project automatiko-engine by automatiko-io.
the class ActionNodeInstance method internalTrigger.
public void internalTrigger(final NodeInstance from, String type) {
triggerTime = new Date();
if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
throw new IllegalArgumentException("An ActionNode only accepts default incoming connections!");
}
Action action = (Action) getActionNode().getAction().getMetaData("Action");
try {
ProcessContext context = new ProcessContext(getProcessInstance().getProcessRuntime());
context.setNodeInstance(this);
executeAction(action);
} catch (WorkflowRuntimeException wre) {
throw wre;
} catch (Exception e) {
// - or context.setNodeInstance(this)
throw new WorkflowRuntimeException(this, getProcessInstance(), "Unable to execute Action: " + e.getMessage(), e);
}
triggerCompleted();
}
use of io.automatiko.engine.workflow.base.instance.impl.Action in project automatiko-engine by automatiko-io.
the class ExecutableProcessFactory method timerAction.
protected ProcessAction timerAction(String type) {
ProcessAction signal = new ProcessAction();
Action action = kcontext -> kcontext.getProcessInstance().signalEvent(type, kcontext.getNodeInstance().getId());
signal.wire(action);
return signal;
}
use of io.automatiko.engine.workflow.base.instance.impl.Action in project automatiko-engine by automatiko-io.
the class ProcessHandler method postProcessNodes.
private void postProcessNodes(ExecutableProcess process, NodeContainer container) {
List<String> eventSubProcessHandlers = new ArrayList<String>();
for (Node node : container.getNodes()) {
if (node instanceof StartNode) {
List<DataAssociation> associations = ((StartNode) node).getOutAssociations();
if (associations != null) {
for (DataAssociation da : associations) {
VariableScope scope = (VariableScope) process.getDefaultContext(VariableScope.VARIABLE_SCOPE);
Variable variable = scope.findVariable(da.getTarget());
if (variable != null) {
da.setTarget(variable.getName());
}
}
}
} else if (node instanceof StateNode) {
StateNode stateNode = (StateNode) node;
String condition = (String) stateNode.getMetaData("Condition");
stateNode.setCondition(context -> {
return (boolean) MVEL.executeExpression(condition, context.getProcessInstance().getVariables());
});
} else if (node instanceof NodeContainer) {
// prepare event sub process
if (node instanceof EventSubProcessNode) {
EventSubProcessNode eventSubProcessNode = (EventSubProcessNode) node;
Node[] nodes = eventSubProcessNode.getNodes();
for (Node subNode : nodes) {
// avoids cyclomatic complexity
if (subNode == null || !(subNode instanceof StartNode)) {
continue;
}
List<Trigger> triggers = ((StartNode) subNode).getTriggers();
if (triggers == null) {
continue;
}
for (Trigger trigger : triggers) {
if (trigger instanceof EventTrigger) {
final List<EventFilter> filters = ((EventTrigger) trigger).getEventFilters();
for (EventFilter filter : filters) {
if (filter instanceof EventTypeFilter) {
eventSubProcessNode.addEvent((EventTypeFilter) filter);
String type = ((EventTypeFilter) filter).getType();
if (type.startsWith("Error-") || type.startsWith("Escalation")) {
String faultCode = (String) subNode.getMetaData().get("FaultCode");
String replaceRegExp = "Error-|Escalation-";
final String signalType = type;
ExceptionScope exceptionScope = (ExceptionScope) ((ContextContainer) eventSubProcessNode.getParentContainer()).getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
if (exceptionScope == null) {
exceptionScope = new ExceptionScope();
((ContextContainer) eventSubProcessNode.getParentContainer()).addContext(exceptionScope);
((ContextContainer) eventSubProcessNode.getParentContainer()).setDefaultContext(exceptionScope);
}
String faultVariable = null;
if (trigger.getInAssociations() != null && !trigger.getInAssociations().isEmpty()) {
faultVariable = findVariable(trigger.getInAssociations().get(0).getSources().get(0), process.getVariableScope());
}
ActionExceptionHandler exceptionHandler = new ActionExceptionHandler();
ConsequenceAction action = new ConsequenceAction("java", "");
action.setMetaData("Action", new SignalProcessInstanceAction(signalType, faultVariable, SignalProcessInstanceAction.PROCESS_INSTANCE_SCOPE));
exceptionHandler.setAction(action);
exceptionHandler.setFaultVariable(faultVariable);
exceptionHandler.setRetryAfter((Integer) subNode.getMetaData().get("ErrorRetry"));
exceptionHandler.setRetryIncrement((Integer) subNode.getMetaData().get("ErrorRetryIncrement"));
if (subNode.getMetaData().get("ErrorRetryIncrementMultiplier") != null) {
exceptionHandler.setRetryIncrementMultiplier(((Number) subNode.getMetaData().get("ErrorRetryIncrementMultiplier")).floatValue());
}
exceptionHandler.setRetryLimit((Integer) subNode.getMetaData().get("ErrorRetryLimit"));
if (faultCode != null) {
String trimmedType = type.replaceFirst(replaceRegExp, "");
for (String error : trimmedType.split(",")) {
exceptionScope.setExceptionHandler(error, exceptionHandler);
eventSubProcessHandlers.add(error);
}
} else {
exceptionScope.setExceptionHandler(faultCode, exceptionHandler);
}
} else if (type.equals("Compensation")) {
// 1. Find the parent sub-process to this event sub-process
NodeContainer parentSubProcess;
NodeContainer subProcess = eventSubProcessNode.getParentContainer();
Object isForCompensationObj = eventSubProcessNode.getMetaData("isForCompensation");
if (isForCompensationObj == null) {
eventSubProcessNode.setMetaData("isForCompensation", true);
logger.warn("Overriding empty or false value of \"isForCompensation\" attribute on Event Sub-Process [" + eventSubProcessNode.getMetaData("UniqueId") + "] and setting it to true.");
}
if (subProcess instanceof ExecutableProcess) {
// (instance)?!?
throw new IllegalArgumentException("Compensation Event Sub-Processes at the process level are not supported.");
}
parentSubProcess = ((Node) subProcess).getParentContainer();
// 2. The event filter (never fires, purely for dumping purposes) has
// already been added
// 3. Add compensation scope
String compensationHandlerId = (String) ((CompositeNode) subProcess).getMetaData("UniqueId");
addCompensationScope(process, eventSubProcessNode, parentSubProcess, compensationHandlerId);
}
}
}
} else if (trigger instanceof ConstraintTrigger) {
ConstraintTrigger constraintTrigger = (ConstraintTrigger) trigger;
if (constraintTrigger.getConstraint() != null) {
String processId = ((ExecutableProcess) container).getId();
String type = "RuleFlowStateEventSubProcess-Event-" + processId + "-" + eventSubProcessNode.getUniqueId();
EventTypeFilter eventTypeFilter = new EventTypeFilter();
eventTypeFilter.setType(type);
eventSubProcessNode.addEvent(eventTypeFilter);
eventSubProcessNode.addEvent("variableChanged");
((StartNode) subNode).setCondition(context -> {
return (boolean) MVEL.executeExpression(constraintTrigger.getConstraint(), context.getProcessInstance().getVariables());
});
}
}
}
}
// for( Node subNode : nodes)
}
postProcessNodes(process, (NodeContainer) node);
} else if (node instanceof EndNode) {
handleIntermediateOrEndThrowCompensationEvent((EndNode) node);
} else if (node instanceof ActionNode) {
handleIntermediateOrEndThrowCompensationEvent((ActionNode) node);
} else if (node instanceof EventNode) {
final EventNode eventNode = (EventNode) node;
if (!(eventNode instanceof BoundaryEventNode) && eventNode.getDefaultIncomingConnections().size() == 0) {
throw new IllegalArgumentException("Event node '" + node.getName() + "' [" + node.getId() + "] has no incoming connection");
}
}
}
// handler
for (Node node : container.getNodes()) {
if (node instanceof FaultNode) {
FaultNode faultNode = (FaultNode) node;
if (eventSubProcessHandlers.contains(faultNode.getFaultName())) {
faultNode.setTerminateParent(false);
}
}
}
}
use of io.automatiko.engine.workflow.base.instance.impl.Action in project automatiko-engine by automatiko-io.
the class CompensationTest method createNestedCompensationEventSubProcessProcess.
private ExecutableProcess createNestedCompensationEventSubProcessProcess(String processId, String[] workItemNames, final List<String> eventList) throws Exception {
ExecutableProcess process = new ExecutableProcess();
process.setAutoComplete(true);
process.setId(processId);
process.setName("CESP Process");
process.setMetaData("Compensation", true);
NodeCreator<StartNode> startNodeCreator = new NodeCreator<StartNode>(process, StartNode.class);
NodeCreator<WorkItemNode> workItemNodeCreator = new NodeCreator<WorkItemNode>(process, WorkItemNode.class);
NodeCreator<CompositeContextNode> compNodeCreator = new NodeCreator<CompositeContextNode>(process, CompositeContextNode.class);
NodeCreator<EndNode> endNodeCreator = new NodeCreator<EndNode>(process, EndNode.class);
// outer process
CompositeContextNode compositeNode = compNodeCreator.createNode("sub0");
{
StartNode startNode = startNodeCreator.createNode("start0");
WorkItemNode workItemNode = workItemNodeCreator.createNode("work0-pre");
workItemNode.getWork().setName(workItemNames[0]);
connect(startNode, workItemNode);
connect(workItemNode, compositeNode);
EndNode endNode = endNodeCreator.createNode("end0");
connect(compositeNode, endNode);
}
// 1rst level nested subprocess (contains compensation visibility scope)
CompositeContextNode compensationScopeContainerNode = compositeNode;
{
startNodeCreator.setNodeContainer(compositeNode);
workItemNodeCreator.setNodeContainer(compositeNode);
compNodeCreator.setNodeContainer(compositeNode);
endNodeCreator.setNodeContainer(compositeNode);
StartNode startNode = startNodeCreator.createNode("start1");
CompositeContextNode subCompNode = compNodeCreator.createNode("sub1");
connect(startNode, subCompNode);
WorkItemNode workItemNode = workItemNodeCreator.createNode("work1-post");
workItemNode.getWork().setName(workItemNames[2]);
connect(subCompNode, workItemNode);
EndNode endNode = endNodeCreator.createNode("end1");
connect(workItemNode, endNode);
compositeNode = subCompNode;
}
// 2nd level nested subprocess
{
startNodeCreator.setNodeContainer(compositeNode);
workItemNodeCreator.setNodeContainer(compositeNode);
endNodeCreator.setNodeContainer(compositeNode);
StartNode startNode = startNodeCreator.createNode("start2");
WorkItemNode workItemNode = workItemNodeCreator.createNode("work2");
workItemNode.getWork().setName(workItemNames[1]);
connect(startNode, workItemNode);
EndNode endNode = endNodeCreator.createNode("end2");
connect(workItemNode, endNode);
}
// 3nd level nested event subprocess in 2nd level subprocess
{
NodeCreator<EventSubProcessNode> espNodeCreator = new NodeCreator<EventSubProcessNode>(compositeNode, EventSubProcessNode.class);
EventSubProcessNode espNode = espNodeCreator.createNode("eventSub2");
startNodeCreator.setNodeContainer(espNode);
endNodeCreator.setNodeContainer(espNode);
NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(espNode, ActionNode.class);
EventTypeFilter eventFilter = new NonAcceptingEventTypeFilter();
eventFilter.setType("Compensation");
espNode.addEvent(eventFilter);
addCompensationScope(espNode, compensationScopeContainerNode, (String) compositeNode.getMetaData("UniqueId"));
StartNode startNode = startNodeCreator.createNode("start3*");
ActionNode actionNode = actionNodeCreator.createNode("action3*");
actionNode.setName("Execute");
ProcessAction action = new ConsequenceAction("java", null);
action.setMetaData("Action", new Action() {
public void execute(ProcessContext context) throws Exception {
eventList.add("Executed action");
}
});
actionNode.setAction(action);
connect(startNode, actionNode);
EndNode endNode = endNodeCreator.createNode("end3*");
connect(actionNode, endNode);
}
return process;
}
Aggregations