use of io.serverlessworkflow.api.error.ErrorDefinition in project automatiko-engine by automatiko-io.
the class ServerlessWorkflowParser method addErrorHandlingToState.
protected void addErrorHandlingToState(Workflow workflow, State state, ServerlessWorkflowFactory factory, AtomicLong ids, WorkflowProcess process, CompositeContextNode subprocess) {
if (state.getOnErrors() != null) {
for (io.serverlessworkflow.api.error.Error error : state.getOnErrors()) {
List<ErrorDefinition> defs = new ArrayList<>();
if (error.getErrorRef() != null) {
workflow.getErrors().getErrorDefs().stream().filter(err -> err.getName().equals(error.getErrorRef())).forEach(err -> defs.add(err));
} else {
workflow.getErrors().getErrorDefs().stream().filter(err -> error.getErrorRefs().contains(err.getName())).forEach(err -> defs.add(err));
}
BoundaryEventNode errorNode = factory.errorBoundaryEventNode(ids.getAndIncrement(), defs, null, process, subprocess, workflow);
if (error.getEnd() != null) {
EndNode onErrorEnd = factory.endNode(ids.getAndIncrement(), state.getName() + "onErrorEnd", error.getEnd().isTerminate(), process);
if (error.getEnd().getProduceEvents() != null && !error.getEnd().getProduceEvents().isEmpty()) {
produceEvents(error.getEnd().getProduceEvents(), factory, workflow, ids, process, errorNode.getId(), onErrorEnd.getId());
} else {
factory.connect(errorNode.getId(), onErrorEnd.getId(), "connect_" + errorNode.getId() + "_" + onErrorEnd.getId(), process, false);
}
} else {
if (error.getTransition().getNextState() != null) {
for (io.automatiko.engine.api.definition.process.Node node : process.getNodes()) {
if (node.getName().equals(error.getTransition().getNextState())) {
if (error.getTransition().getProduceEvents() != null && !error.getTransition().getProduceEvents().isEmpty()) {
produceEvents(error.getTransition().getProduceEvents(), factory, workflow, ids, process, errorNode.getId(), node.getId());
} else {
factory.connect(errorNode.getId(), node.getId(), "connect_" + errorNode.getId() + "_" + node.getId(), process, false);
}
break;
}
}
}
}
}
}
}
use of io.serverlessworkflow.api.error.ErrorDefinition in project automatiko-engine by automatiko-io.
the class ServerlessWorkflowParser method buildActionsForState.
protected void buildActionsForState(Workflow workflow, List<Action> actions, NodeContainer embeddedSubProcess, ServerlessWorkflowFactory factory, AtomicLong ids, BiConsumer<Node, Node> firstLastNodeConsumer, BiConsumer<Node, Node> actionConsumer, boolean isParallel) {
Node firstNode = null;
Node lastNode = null;
Node prevNode = null;
for (Action action : actions) {
if (action.getFunctionRef() != null) {
// handle function based action
Optional<FunctionDefinition> functionDefinition = workflow.getFunctions().getFunctionDefs().stream().filter(functionDef -> functionDef.getName().equals(action.getFunctionRef().getRefName())).distinct().findFirst();
if (functionDefinition.get().getType() == FunctionDefinition.Type.EXPRESSION) {
ActionNode actionNode = factory.expressionActionStateNode(ids.getAndIncrement(), action.getName(), embeddedSubProcess, functionDefinition.get().getOperation(), action);
if (firstNode == null) {
firstNode = actionNode;
}
lastNode = actionNode;
} else if (functionDefinition.get().getType() == null || functionDefinition.get().getType() == FunctionDefinition.Type.REST) {
WorkItemNode serviceNode = factory.serviceNode(ids.getAndIncrement(), action, functionDefinition.get(), embeddedSubProcess);
if (firstNode == null) {
firstNode = serviceNode;
}
lastNode = serviceNode;
if (workflow.getTimeouts() != null && workflow.getTimeouts().getActionExecTimeout() != null) {
serviceNode.setMetaData("timeout", String.valueOf(DateTimeUtils.parseDuration(workflow.getTimeouts().getActionExecTimeout())));
}
if (action.getRetryableErrors() != null && !action.getRetryableErrors().isEmpty()) {
List<ErrorDefinition> defs = new ArrayList<>();
for (String errorRef : action.getRetryableErrors()) {
ErrorDefinition errorDef = workflow.getErrors().getErrorDefs().stream().filter(error -> error.getName().equals(errorRef)).findFirst().orElseThrow(() -> new IllegalStateException("Missing error definition for " + errorRef));
defs.add(errorDef);
}
RetryDefinition retry = null;
BoundaryEventNode errorNode = factory.errorBoundaryEventNode(ids.getAndIncrement(), defs, retry, embeddedSubProcess, serviceNode, workflow);
EndNode onErrorEnd = factory.endNode(ids.getAndIncrement(), action.getName() + "onErrorEnd", false, embeddedSubProcess);
factory.connect(errorNode.getId(), onErrorEnd.getId(), "connect_" + errorNode.getId() + "_" + onErrorEnd.getId(), embeddedSubProcess, false);
}
} else {
throw new UnsupportedOperationException(functionDefinition.get().getType() + " is not yet supported");
}
} else if (action.getSubFlowRef() != null) {
// handler sub workflow action definition
String workflowId = Objects.requireNonNull(action.getSubFlowRef().getWorkflowId(), "Workflow id for subworkflow is mandatory");
boolean independent = false;
if (action.getSubFlowRef().getOnParentComplete() != null && action.getSubFlowRef().getOnParentComplete().equals(OnParentComplete.CONTINUE)) {
independent = true;
}
boolean waitForCompletion = true;
if (action.getSubFlowRef().getInvoke().equals(Invoke.ASYNC)) {
waitForCompletion = false;
}
SubProcessNode callactivity = factory.callActivity(ids.getAndIncrement(), action.getName(), workflowId, waitForCompletion, embeddedSubProcess);
callactivity.setIndependent(independent);
callactivity.setProcessVersion(action.getSubFlowRef().getVersion());
if (firstNode == null) {
firstNode = callactivity;
}
lastNode = callactivity;
}
if (action.getSleep() != null && action.getSleep().getBefore() != null) {
TimerNode sleep = factory.timerNode(ids.getAndIncrement(), "sleep-before-" + action.getName(), action.getSleep().getBefore(), embeddedSubProcess);
factory.connect(sleep.getId(), firstNode.getId(), "connection_" + sleep.getId() + "_" + firstNode.getId(), embeddedSubProcess, false);
firstNode = sleep;
}
if (action.getSleep() != null && action.getSleep().getAfter() != null) {
TimerNode sleep = factory.timerNode(ids.getAndIncrement(), "sleep-after-" + action.getName(), action.getSleep().getAfter(), embeddedSubProcess);
factory.connect(lastNode.getId(), sleep.getId(), "connection_" + lastNode.getId() + "_" + sleep.getId(), embeddedSubProcess, false);
lastNode = sleep;
}
actionConsumer.accept(firstNode, lastNode);
if (isParallel && actions.size() > 1) {
// reset first node as all of action nodes will be first nodes
firstNode = null;
} else {
if (prevNode != null) {
factory.connect(prevNode.getId(), lastNode.getId(), "connect_" + prevNode.getId() + "_" + lastNode.getId(), embeddedSubProcess, false);
}
}
prevNode = lastNode;
}
firstLastNodeConsumer.accept(firstNode, lastNode);
}
use of io.serverlessworkflow.api.error.ErrorDefinition in project automatiko-engine by automatiko-io.
the class ServerlessWorkflowFactory method errorBoundaryEventNode.
public BoundaryEventNode errorBoundaryEventNode(long id, List<ErrorDefinition> defs, RetryDefinition retry, NodeContainer nodeContainer, Node attachedTo, Workflow workflow) {
BoundaryEventNode boundaryEventNode = new BoundaryEventNode();
boundaryEventNode.setId(id);
boundaryEventNode.setName(defs.stream().map(def -> def.getName()).collect(Collectors.joining("|")));
String errorCodes = defs.stream().map(def -> def.getCode()).collect(Collectors.joining(","));
String attachedToId = (String) attachedTo.getMetaData().getOrDefault(UNIQUE_ID_PARAM, Long.toString(attachedTo.getId()));
EventTypeFilter filter = new EventTypeFilter();
filter.setType("Error-" + attachedToId + "-" + errorCodes);
boundaryEventNode.addEventFilter(filter);
boundaryEventNode.setAttachedToNodeId(attachedToId);
boundaryEventNode.setMetaData(UNIQUE_ID_PARAM, Long.toString(id));
boundaryEventNode.setMetaData("EventType", "error");
boundaryEventNode.setMetaData("ErrorEvent", errorCodes);
boundaryEventNode.setMetaData("AttachedTo", attachedToId);
boundaryEventNode.setMetaData("HasErrorEvent", true);
if (retry != null) {
int delayAsInt = ((Long) DateTimeUtils.parseDuration(retry.getDelay())).intValue();
boundaryEventNode.setMetaData("ErrorRetry", retry.getDelay() == null ? DEFAULT_RETRY_AFTER : delayAsInt);
boundaryEventNode.setMetaData("ErrorRetryLimit", retry.getMaxAttempts() == null ? DEFAULT_RETRY_LIMIT : Integer.parseInt(retry.getMaxAttempts()));
if (retry.getMultiplier() != null) {
boundaryEventNode.setMetaData("ErrorRetryIncrementMultiplier", Float.parseFloat(retry.getMultiplier()));
}
}
nodeContainer.addNode(boundaryEventNode);
return boundaryEventNode;
}
use of io.serverlessworkflow.api.error.ErrorDefinition in project kogito-runtimes by kiegroup.
the class StateHandler method handleErrors.
protected void handleErrors(RuleFlowNodeContainerFactory<?, ?> factory) {
for (Error error : state.getOnErrors()) {
for (ErrorDefinition errorDef : getErrorDefinitions(error)) {
String eventType = "Error-" + node.getNode().getMetaData().get("UniqueId");
BoundaryEventNodeFactory<?> boundaryNode = factory.boundaryEventNode(parserContext.newId()).attachedTo(node.getNode().getId()).metaData("EventType", Metadata.EVENT_TYPE_ERROR).metaData("HasErrorEvent", true);
if (errorDef.getCode() != null) {
boundaryNode.metaData("ErrorEvent", errorDef.getCode());
eventType += "-" + errorDef.getCode();
}
boundaryNode.eventType(eventType).name("Error-" + node.getNode().getName() + "-" + errorDef.getCode());
factory.exceptionHandler(eventType, errorDef.getCode());
if (error.getEnd() != null) {
connect(boundaryNode, endNodeFactory(factory, error.getEnd().getProduceEvents()));
} else {
handleTransitions(factory, error.getTransition(), boundaryNode.getNode().getId());
}
}
}
}
Aggregations