Search in sources :

Example 1 with ActionExceptionHandler

use of io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler 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);
    }
}
Also used : NodeInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl) Action(io.automatiko.engine.workflow.base.instance.impl.Action) WorkItemNodeInstance(io.automatiko.engine.workflow.process.instance.node.WorkItemNodeInstance) ActionExceptionHandler(io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler) ProcessContext(io.automatiko.engine.workflow.base.core.context.ProcessContext) JobsService(io.automatiko.engine.api.jobs.JobsService) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ProcessInstance(io.automatiko.engine.workflow.base.instance.ProcessInstance) ContextInstanceContainer(io.automatiko.engine.workflow.base.instance.ContextInstanceContainer) DurationExpirationTime(io.automatiko.engine.api.jobs.DurationExpirationTime) WorkItemNodeInstance(io.automatiko.engine.workflow.process.instance.node.WorkItemNodeInstance) NodeInstance(io.automatiko.engine.workflow.process.instance.NodeInstance)

Example 2 with ActionExceptionHandler

use of io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler in project automatiko-engine by automatiko-io.

the class ExceptionHandlerHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    ContextContainer contextContainer = (ContextContainer) parser.getParent();
    final String type = element.getAttribute("type");
    emptyAttributeCheck(localName, "type", type, parser);
    final String faultName = element.getAttribute("faultName");
    emptyAttributeCheck(localName, "faultName", type, parser);
    final String faultVariable = element.getAttribute("faultVariable");
    ActionExceptionHandler exceptionHandler = null;
    if ("action".equals(type)) {
        exceptionHandler = new ActionExceptionHandler();
        org.w3c.dom.Node xmlNode = element.getFirstChild();
        if (xmlNode instanceof Element) {
            Element actionXml = (Element) xmlNode;
            ProcessAction action = ActionNodeHandler.extractAction(actionXml);
            ((ActionExceptionHandler) exceptionHandler).setAction(action);
        }
    } else {
        throw new SAXParseException("Unknown exception handler type " + type, parser.getLocator());
    }
    if (faultVariable != null && faultVariable.length() > 0) {
        exceptionHandler.setFaultVariable(faultVariable);
    }
    ExceptionScope exceptionScope = (ExceptionScope) contextContainer.getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
    if (exceptionScope == null) {
        exceptionScope = new ExceptionScope();
        contextContainer.addContext(exceptionScope);
        contextContainer.setDefaultContext(exceptionScope);
    }
    for (String error : faultName.split(",")) {
        exceptionScope.setExceptionHandler(error, exceptionHandler);
    }
    return null;
}
Also used : ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) ActionExceptionHandler(io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler) ExceptionScope(io.automatiko.engine.workflow.base.core.context.exception.ExceptionScope)

Example 3 with ActionExceptionHandler

use of io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler in project automatiko-engine by automatiko-io.

the class ExecutableProcessFactory method linkBoundaryErrorEvent.

private static void linkBoundaryErrorEvent(NodeContainer nodeContainer, Node node, String attachedTo, Node attachedNode) {
    ContextContainer compositeNode = (ContextContainer) attachedNode;
    ExceptionScope exceptionScope = (ExceptionScope) compositeNode.getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
    if (exceptionScope == null) {
        exceptionScope = new ExceptionScope();
        compositeNode.addContext(exceptionScope);
        compositeNode.setDefaultContext(exceptionScope);
    }
    String errorCode = (String) node.getMetaData().get("ErrorEvent");
    boolean hasErrorCode = (Boolean) node.getMetaData().get("HasErrorEvent");
    String errorStructureRef = (String) node.getMetaData().get("ErrorStructureRef");
    ActionExceptionHandler exceptionHandler = new ActionExceptionHandler();
    String variable = ((EventNode) node).getVariableName();
    ConsequenceAction action = new ConsequenceAction("java", null);
    action.setMetaData(ACTION, new SignalProcessInstanceAction("Error-" + attachedTo + "-" + errorCode, variable, SignalProcessInstanceAction.PROCESS_INSTANCE_SCOPE));
    exceptionHandler.setAction(action);
    exceptionHandler.setFaultVariable(variable);
    exceptionHandler.setRetryAfter((Integer) node.getMetaData().get("ErrorRetry"));
    exceptionHandler.setRetryIncrement((Integer) node.getMetaData().get("ErrorRetryIncrement"));
    if (node.getMetaData().get("ErrorRetryIncrementMultiplier") != null) {
        exceptionHandler.setRetryIncrementMultiplier(((Number) node.getMetaData().get("ErrorRetryIncrementMultiplier")).floatValue());
    }
    exceptionHandler.setRetryLimit((Integer) node.getMetaData().get("ErrorRetryLimit"));
    if (hasErrorCode) {
        for (String error : errorCode.split(",")) {
            exceptionScope.setExceptionHandler(error, exceptionHandler);
        }
    } else {
        exceptionScope.setExceptionHandler(null, exceptionHandler);
    }
    if (errorStructureRef != null) {
        exceptionScope.setExceptionHandler(errorStructureRef, exceptionHandler);
    }
    List<ProcessAction> actions = ((EventNode) node).getActions(EndNode.EVENT_NODE_EXIT);
    if (actions == null) {
        actions = new ArrayList<ProcessAction>();
    }
    ConsequenceAction cancelAction = new ConsequenceAction("java", null);
    cancelAction.setMetaData("Action", new CancelNodeInstanceAction(attachedTo));
    actions.add(cancelAction);
    ((EventNode) node).setActions(EndNode.EVENT_NODE_EXIT, actions);
}
Also used : ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) SignalProcessInstanceAction(io.automatiko.engine.workflow.base.instance.impl.actions.SignalProcessInstanceAction) CancelNodeInstanceAction(io.automatiko.engine.workflow.base.instance.impl.actions.CancelNodeInstanceAction) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) ExceptionScope(io.automatiko.engine.workflow.base.core.context.exception.ExceptionScope) ActionExceptionHandler(io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode)

Example 4 with ActionExceptionHandler

use of io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler in project automatiko-engine by automatiko-io.

the class ExecutableProcessFactory method processEventSubprocessStartNode.

private void processEventSubprocessStartNode(ExecutableProcess process, StartNode subNode, EventSubProcessNode eventSubProcessNode, List<String> eventSubProcessHandlers) {
    List<Trigger> triggers = subNode.getTriggers();
    if (triggers != null) {
        for (Trigger trigger : triggers) {
            if (trigger instanceof EventTrigger) {
                final List<EventFilter> filters = ((EventTrigger) trigger).getEventFilters();
                for (EventFilter filter : filters) {
                    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 (trigger instanceof ConstraintTrigger) {
                        ConstraintTrigger constraintTrigger = (ConstraintTrigger) trigger;
                        if (constraintTrigger.getConstraint() != null) {
                            EventTypeFilter eventTypeFilter = new EventTypeFilter();
                            eventTypeFilter.setType(type);
                            eventSubProcessNode.addEvent(eventTypeFilter);
                            eventSubProcessNode.addEvent("variableChanged");
                        }
                    }
                }
            }
        }
    }
}
Also used : ConstraintTrigger(io.automatiko.engine.workflow.process.core.node.ConstraintTrigger) SignalProcessInstanceAction(io.automatiko.engine.workflow.base.instance.impl.actions.SignalProcessInstanceAction) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) EventFilter(io.automatiko.engine.workflow.base.core.event.EventFilter) ExceptionScope(io.automatiko.engine.workflow.base.core.context.exception.ExceptionScope) ActionExceptionHandler(io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler) EventTypeFilter(io.automatiko.engine.workflow.base.core.event.EventTypeFilter) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) Trigger(io.automatiko.engine.workflow.process.core.node.Trigger) ConstraintTrigger(io.automatiko.engine.workflow.process.core.node.ConstraintTrigger) EventTrigger(io.automatiko.engine.workflow.process.core.node.EventTrigger) EventTrigger(io.automatiko.engine.workflow.process.core.node.EventTrigger)

Example 5 with ActionExceptionHandler

use of io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler in project automatiko-engine by automatiko-io.

the class ProcessHandler method linkBoundaryEscalationEvent.

protected void linkBoundaryEscalationEvent(NodeContainer nodeContainer, Node node, String attachedTo, Node attachedNode) {
    boolean cancelActivity = (Boolean) node.getMetaData().get("CancelActivity");
    String escalationCode = (String) node.getMetaData().get("EscalationEvent");
    String escalationStructureRef = (String) node.getMetaData().get("EscalationStructureRef");
    ContextContainer compositeNode = (ContextContainer) attachedNode;
    ExceptionScope exceptionScope = (ExceptionScope) compositeNode.getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
    if (exceptionScope == null) {
        exceptionScope = new ExceptionScope();
        compositeNode.addContext(exceptionScope);
        compositeNode.setDefaultContext(exceptionScope);
    }
    String variable = ((EventNode) node).getVariableName();
    ActionExceptionHandler exceptionHandler = new ActionExceptionHandler();
    ConsequenceAction action = createJavaAction(new SignalProcessInstanceAction("Escalation-" + attachedTo + "-" + escalationCode, variable, SignalProcessInstanceAction.PROCESS_INSTANCE_SCOPE));
    exceptionHandler.setAction(action);
    exceptionHandler.setFaultVariable(variable);
    exceptionScope.setExceptionHandler(escalationCode, exceptionHandler);
    if (escalationStructureRef != null) {
        exceptionScope.setExceptionHandler(escalationStructureRef, exceptionHandler);
    }
    if (cancelActivity) {
        List<ProcessAction> actions = ((EventNode) node).getActions(EndNode.EVENT_NODE_EXIT);
        if (actions == null) {
            actions = new ArrayList<ProcessAction>();
        }
        ConsequenceAction cancelAction = new ConsequenceAction("java", "");
        cancelAction.setMetaData("Action", new CancelNodeInstanceAction(attachedTo));
        actions.add(cancelAction);
        ((EventNode) node).setActions(EndNode.EVENT_NODE_EXIT, actions);
    }
}
Also used : ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) SignalProcessInstanceAction(io.automatiko.engine.workflow.base.instance.impl.actions.SignalProcessInstanceAction) CancelNodeInstanceAction(io.automatiko.engine.workflow.base.instance.impl.actions.CancelNodeInstanceAction) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) ExceptionScope(io.automatiko.engine.workflow.base.core.context.exception.ExceptionScope) ActionExceptionHandler(io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) BoundaryEventNode(io.automatiko.engine.workflow.process.core.node.BoundaryEventNode) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode)

Aggregations

ActionExceptionHandler (io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler)10 ConsequenceAction (io.automatiko.engine.workflow.process.core.impl.ConsequenceAction)7 ContextContainer (io.automatiko.engine.workflow.base.core.ContextContainer)6 ExceptionScope (io.automatiko.engine.workflow.base.core.context.exception.ExceptionScope)6 ProcessAction (io.automatiko.engine.workflow.process.core.ProcessAction)6 SignalProcessInstanceAction (io.automatiko.engine.workflow.base.instance.impl.actions.SignalProcessInstanceAction)5 CancelNodeInstanceAction (io.automatiko.engine.workflow.base.instance.impl.actions.CancelNodeInstanceAction)4 EventNode (io.automatiko.engine.workflow.process.core.node.EventNode)4 BoundaryEventNode (io.automatiko.engine.workflow.process.core.node.BoundaryEventNode)3 EventFilter (io.automatiko.engine.workflow.base.core.event.EventFilter)2 EventTypeFilter (io.automatiko.engine.workflow.base.core.event.EventTypeFilter)2 Action (io.automatiko.engine.workflow.base.instance.impl.Action)2 ConstraintTrigger (io.automatiko.engine.workflow.process.core.node.ConstraintTrigger)2 EventTrigger (io.automatiko.engine.workflow.process.core.node.EventTrigger)2 Trigger (io.automatiko.engine.workflow.process.core.node.Trigger)2 Node (io.automatiko.engine.api.definition.process.Node)1 NodeContainer (io.automatiko.engine.api.definition.process.NodeContainer)1 WorkflowProcess (io.automatiko.engine.api.definition.process.WorkflowProcess)1 DurationExpirationTime (io.automatiko.engine.api.jobs.DurationExpirationTime)1 JobsService (io.automatiko.engine.api.jobs.JobsService)1