Search in sources :

Example 1 with ProcessInstance

use of io.automatiko.engine.workflow.base.instance.ProcessInstance in project automatiko-engine by automatiko-io.

the class HandleEscalationAction method execute.

public void execute(ProcessContext context) throws Exception {
    ExceptionScopeInstance scopeInstance = (ExceptionScopeInstance) ((NodeInstance) context.getNodeInstance()).resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, faultName);
    if (scopeInstance != null) {
        Object tVariable = variableName == null ? null : context.getVariable(variableName);
        io.automatiko.engine.workflow.process.core.node.Transformation transformation = (io.automatiko.engine.workflow.process.core.node.Transformation) context.getNodeInstance().getNode().getMetaData().get("Transformation");
        if (transformation != null) {
            tVariable = new EventTransformerImpl(transformation).transformEvent(context.getProcessInstance().getVariables());
        }
        scopeInstance.handleException(context.getNodeInstance(), faultName, tVariable);
    } else {
        ((ProcessInstance) context.getProcessInstance()).setState(STATE_ABORTED);
    }
}
Also used : ProcessInstance(io.automatiko.engine.workflow.base.instance.ProcessInstance) EventTransformerImpl(io.automatiko.engine.workflow.base.core.event.EventTransformerImpl) ExceptionScopeInstance(io.automatiko.engine.workflow.base.instance.context.exception.ExceptionScopeInstance)

Example 2 with ProcessInstance

use of io.automatiko.engine.workflow.base.instance.ProcessInstance 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 3 with ProcessInstance

use of io.automatiko.engine.workflow.base.instance.ProcessInstance in project automatiko-engine by automatiko-io.

the class CompositeContextNodeInstance method getContextInstance.

public ContextInstance getContextInstance(final Context context) {
    ContextInstanceFactory conf = ContextInstanceFactoryRegistry.INSTANCE.getContextInstanceFactory(context);
    if (conf == null) {
        throw new IllegalArgumentException("Illegal context type (registry not found): " + context.getClass());
    }
    ContextInstance contextInstance = (ContextInstance) conf.getContextInstance(context, this, (ProcessInstance) getProcessInstance());
    if (contextInstance == null) {
        throw new IllegalArgumentException("Illegal context type (instance not found): " + context.getClass());
    }
    return contextInstance;
}
Also used : ContextInstance(io.automatiko.engine.workflow.base.instance.ContextInstance) ProcessInstance(io.automatiko.engine.workflow.base.instance.ProcessInstance) ContextInstanceFactory(io.automatiko.engine.workflow.base.instance.impl.ContextInstanceFactory)

Example 4 with ProcessInstance

use of io.automatiko.engine.workflow.base.instance.ProcessInstance in project automatiko-engine by automatiko-io.

the class EndNodeInstance method internalTrigger.

public void internalTrigger(final NodeInstance from, String type) {
    super.internalTrigger(from, type);
    if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
        throw new IllegalArgumentException("An EndNode only accepts default incoming connections!");
    }
    leaveTime = new Date();
    boolean hidden = false;
    if (getNode().getMetaData().get(HIDDEN) != null) {
        hidden = true;
    }
    if (getProcessInstance().isFunctionFlow(this) && getNodeInstanceContainer() instanceof ProcessInstance) {
        // only when running as function flow and node is in the top level node container meaning process instance
        // and not subprocesses
        getProcessInstance().getMetaData().compute("ATK_FUNC_FLOW_NEXT", (k, v) -> {
            if (v == null) {
                v = new ArrayList<String>();
            }
            Process process = getProcessInstance().getProcess();
            String version = "";
            if (process.getVersion() != null && !process.getVersion().trim().isEmpty()) {
                version = ".v" + process.getVersion().replaceAll("\\.", "_");
            }
            String defaultNextNode = process.getPackageName() + "." + process.getId() + version + "." + getNodeName().toLowerCase();
            ((List<String>) v).add((String) getNode().getMetaData().getOrDefault("functionType", defaultNextNode));
            return v;
        });
    }
    InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
    if (!hidden) {
        runtime.getProcessEventSupport().fireBeforeNodeLeft(this, runtime);
    }
    ((NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
    if (getEndNode().isTerminate()) {
        if (getNodeInstanceContainer() instanceof CompositeNodeInstance) {
            if (getEndNode().getScope() == PROCESS_SCOPE) {
                getProcessInstance().setState(STATE_COMPLETED);
            } else {
                while (!getNodeInstanceContainer().getNodeInstances().isEmpty()) {
                    ((io.automatiko.engine.workflow.process.instance.NodeInstance) getNodeInstanceContainer().getNodeInstances().iterator().next()).cancel();
                }
                ((NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, null);
            }
        } else {
            ((NodeInstanceContainer) getNodeInstanceContainer()).setState(STATE_COMPLETED);
        }
    } else {
        ((NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, null);
    }
    if (!hidden) {
        runtime.getProcessEventSupport().fireAfterNodeLeft(this, runtime);
    }
    String uniqueId = (String) getNode().getMetaData().get(UNIQUE_ID);
    if (uniqueId == null) {
        uniqueId = ((NodeImpl) getNode()).getUniqueId();
    }
    ((WorkflowProcessInstanceImpl) getProcessInstance()).addCompletedNodeId(uniqueId);
}
Also used : NodeInstanceContainer(io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) Process(io.automatiko.engine.api.definition.process.Process) Date(java.util.Date) ProcessInstance(io.automatiko.engine.workflow.base.instance.ProcessInstance) ArrayList(java.util.ArrayList) List(java.util.List) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance)

Example 5 with ProcessInstance

use of io.automatiko.engine.workflow.base.instance.ProcessInstance in project automatiko-engine by automatiko-io.

the class WorkItemNodeInstance method cancel.

@Override
public void cancel() {
    WorkItem item = getWorkItem();
    if (item != null && item.getState() != COMPLETED && item.getState() != ABORTED) {
        try {
            ((DefaultWorkItemManager) getProcessInstance().getProcessRuntime().getWorkItemManager()).internalAbortWorkItem(item.getId());
        } catch (WorkItemHandlerNotFoundException wihnfe) {
            getProcessInstance().setState(STATE_ABORTED);
            throw wihnfe;
        }
    }
    if (exceptionHandlingProcessInstanceId != null) {
        ProcessInstance processInstance = (ProcessInstance) getProcessInstance().getProcessRuntime().getProcessInstance(exceptionHandlingProcessInstanceId);
        if (processInstance != null) {
            processInstance.setState(STATE_ABORTED);
        }
    }
    super.cancel();
}
Also used : DefaultWorkItemManager(io.automatiko.engine.workflow.base.instance.impl.workitem.DefaultWorkItemManager) WorkItemHandlerNotFoundException(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemHandlerNotFoundException) WorkflowProcessInstance(io.automatiko.engine.workflow.process.instance.WorkflowProcessInstance) ProcessInstance(io.automatiko.engine.workflow.base.instance.ProcessInstance) HumanTaskWorkItem(io.automatiko.engine.api.runtime.process.HumanTaskWorkItem) WorkItem(io.automatiko.engine.api.runtime.process.WorkItem)

Aggregations

ProcessInstance (io.automatiko.engine.workflow.base.instance.ProcessInstance)13 Process (io.automatiko.engine.api.definition.process.Process)4 InternalProcessRuntime (io.automatiko.engine.workflow.base.instance.InternalProcessRuntime)4 WorkflowProcessInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)4 ArrayList (java.util.ArrayList)4 ContextInstance (io.automatiko.engine.workflow.base.instance.ContextInstance)3 ContextInstanceFactory (io.automatiko.engine.workflow.base.instance.impl.ContextInstanceFactory)3 ProcessInstanceImpl (io.automatiko.engine.workflow.base.instance.impl.ProcessInstanceImpl)3 NodeInstanceContainer (io.automatiko.engine.workflow.process.instance.NodeInstanceContainer)3 WorkflowProcessInstance (io.automatiko.engine.workflow.process.instance.WorkflowProcessInstance)3 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)2 CorrelationKey (io.automatiko.engine.services.correlation.CorrelationKey)2 StringCorrelationKey (io.automatiko.engine.services.correlation.StringCorrelationKey)2 ExceptionScopeInstance (io.automatiko.engine.workflow.base.instance.context.exception.ExceptionScopeInstance)2 WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)1 DurationExpirationTime (io.automatiko.engine.api.jobs.DurationExpirationTime)1