Search in sources :

Example 1 with WorkItemExecutionError

use of io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError in project automatiko-engine by automatiko-io.

the class EmbeddedSubProcessTest method testEmbeddedSubprocessServiceProcessTaskWithRetry.

@Test
@Timeout(unit = TimeUnit.SECONDS, value = 10)
public void testEmbeddedSubprocessServiceProcessTaskWithRetry() throws Exception {
    Application app = generateCodeProcessesOnly("subprocess/EmbeddedSubprocessWihErrorRetry.bpmn2");
    assertThat(app).isNotNull();
    NodeLeftCountDownProcessEventListener listener = new NodeLeftCountDownProcessEventListener("Error happened", 1);
    ((DefaultProcessEventListenerConfig) app.config().process().processEventListeners()).register(listener);
    Process<? extends Model> p = app.processes().processById("subprocessRetry");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("name", "john");
    m.fromMap(parameters);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    listener.waitTillCompleted();
    assertThat(processInstance.startDate()).isNotNull();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    Model result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(2).containsKeys("name", "ErrorData");
    assertThat(result.toMap().get("name")).isNotNull().isEqualTo("john");
    assertThat(result.toMap().get("ErrorData")).isNotNull().isInstanceOf(WorkItemExecutionError.class);
    assertThat(((WorkItemExecutionError) result.toMap().get("ErrorData")).getErrorCode()).isEqualTo("500");
}
Also used : NodeLeftCountDownProcessEventListener(io.automatiko.engine.workflow.compiler.util.NodeLeftCountDownProcessEventListener) HashMap(java.util.HashMap) DefaultProcessEventListenerConfig(io.automatiko.engine.workflow.DefaultProcessEventListenerConfig) Model(io.automatiko.engine.api.Model) Application(io.automatiko.engine.api.Application) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 2 with WorkItemExecutionError

use of io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError in project automatiko-engine by automatiko-io.

the class SubProcessNode method internalSubProcess.

public void internalSubProcess(Collection<io.automatiko.engine.api.workflow.Process> availableProcesses) {
    io.automatiko.engine.api.workflow.Process process = (io.automatiko.engine.api.workflow.Process<Map<String, Object>>) availableProcesses.stream().filter(p -> p.id().equals(processId)).findFirst().orElse(null);
    if (process == null) {
        return;
    }
    this.subProcessFactory = new SubProcessFactory<Object>() {

        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public Map<String, Object> bind(ProcessContext ctx) {
            Map<String, Object> parameters = new HashMap<String, Object>();
            for (Iterator<DataAssociation> iterator = getInAssociations().iterator(); iterator.hasNext(); ) {
                DataAssociation mapping = iterator.next();
                Object parameterValue = null;
                if (mapping.getTransformation() != null) {
                    Transformation transformation = mapping.getTransformation();
                    DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                    if (transformer != null) {
                        parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(ctx, mapping));
                    }
                } else if (mapping.getAssignments() == null || mapping.getAssignments().isEmpty()) {
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getSources().get(0));
                    if (variableScopeInstance != null) {
                        parameterValue = variableScopeInstance.getVariable(mapping.getSources().get(0));
                    } else {
                        try {
                            ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) ctx.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                            parameterValue = evaluator.evaluate(mapping.getSources().get(0), new NodeInstanceResolverFactory((NodeInstance) ctx.getNodeInstance()));
                        } catch (Throwable t) {
                            parameterValue = VariableUtil.resolveVariable(mapping.getSources().get(0), ctx.getNodeInstance());
                            if (parameterValue != null) {
                                parameters.put(mapping.getTarget(), parameterValue);
                            }
                        }
                    }
                } else {
                    mapping.getAssignments().stream().forEach(a -> handleAssignment(ctx, parameters, a));
                }
                if (parameterValue != null) {
                    parameters.put(mapping.getTarget(), parameterValue);
                }
            }
            return parameters;
        }

        @Override
        public ProcessInstance createInstance(Object model) {
            Model data = (Model) process.createModel();
            data.fromMap((Map<String, Object>) model);
            return process.createInstance(data);
        }

        @Override
        public void unbind(ProcessContext ctx, Object model) {
            Map<String, Object> result = ((Model) model).toMap();
            for (Iterator<DataAssociation> iterator = getOutAssociations().iterator(); iterator.hasNext(); ) {
                DataAssociation mapping = iterator.next();
                if (mapping.getTransformation() != null) {
                    Transformation transformation = mapping.getTransformation();
                    DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                    if (transformer != null) {
                        Map<String, Object> dataSet = new HashMap<String, Object>();
                        if (ctx.getNodeInstance().getNodeInstanceContainer() instanceof CompositeContextNodeInstance) {
                            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) ctx.getNodeInstance().getNodeInstanceContainer()).getContextInstance(VariableScope.VARIABLE_SCOPE);
                            if (variableScopeInstance != null) {
                                dataSet.putAll(variableScopeInstance.getVariables());
                            }
                        }
                        dataSet.putAll(result);
                        Object parameterValue = transformer.transform(transformation.getCompiledExpression(), dataSet);
                        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
                        if (variableScopeInstance != null && parameterValue != null) {
                            variableScopeInstance.setVariable(ctx.getNodeInstance(), mapping.getTarget(), parameterValue);
                        }
                    }
                } else {
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
                    if (variableScopeInstance != null) {
                        Object value = result.get(mapping.getSources().get(0));
                        if (value == null) {
                            try {
                                value = MVEL.eval(mapping.getSources().get(0), result);
                            } catch (Throwable t) {
                            // do nothing
                            }
                        }
                        variableScopeInstance.setVariable(ctx.getNodeInstance(), mapping.getTarget(), value);
                    } else {
                        String output = mapping.getSources().get(0);
                        String target = mapping.getTarget();
                        Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(target);
                        if (matcher.find()) {
                            String paramName = matcher.group(1);
                            String expression = paramName + " = " + output;
                            Serializable compiled = MVEL.compileExpression(expression);
                            MVEL.executeExpression(compiled, result);
                        }
                    }
                }
            }
        }

        @SuppressWarnings("unchecked")
        @Override
        public void abortInstance(String instanceId) {
            process.instances().findById(instanceId).ifPresent(pi -> {
                try {
                    ((ProcessInstance<?>) pi).abort();
                } catch (IllegalArgumentException e) {
                // ignore it as this might be thrown in case of canceling already aborted instance
                }
            });
        }

        private void handleAssignment(ProcessContext ctx, Map<String, Object> output, Assignment assignment) {
            AssignmentAction action = (AssignmentAction) assignment.getMetaData("Action");
            try {
                WorkItemImpl workItem = new WorkItemImpl();
                action.execute(workItem, ctx);
                output.putAll(workItem.getParameters());
            } catch (WorkItemExecutionError e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException("unable to execute Assignment", e);
            }
        }
    };
}
Also used : VariableUtil(io.automatiko.engine.workflow.base.instance.impl.util.VariableUtil) Context(io.automatiko.engine.workflow.base.core.Context) HashMap(java.util.HashMap) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) ArrayList(java.util.ArrayList) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) ProcessContext(io.automatiko.engine.api.runtime.process.ProcessContext) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) Matcher(java.util.regex.Matcher) AbstractContext(io.automatiko.engine.workflow.base.core.context.AbstractContext) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) Map(java.util.Map) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess) CompositeContextNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance) LinkedList(java.util.LinkedList) Connection(io.automatiko.engine.api.definition.process.Connection) NodeInstance(io.automatiko.engine.workflow.process.instance.NodeInstance) Iterator(java.util.Iterator) Model(io.automatiko.engine.api.Model) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) Collection(java.util.Collection) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) Serializable(java.io.Serializable) DataTransformerRegistry(io.automatiko.engine.workflow.base.core.impl.DataTransformerRegistry) List(java.util.List) PatternConstants(io.automatiko.engine.workflow.util.PatternConstants) ContextContainerImpl(io.automatiko.engine.workflow.base.core.impl.ContextContainerImpl) Mappable(io.automatiko.engine.workflow.base.core.context.variable.Mappable) AssignmentAction(io.automatiko.engine.workflow.base.instance.impl.AssignmentAction) Collections(java.util.Collections) MVEL(org.mvel2.MVEL) Serializable(java.io.Serializable) Matcher(java.util.regex.Matcher) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) ProcessContext(io.automatiko.engine.api.runtime.process.ProcessContext) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) Iterator(java.util.Iterator) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) CompositeContextNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance) AssignmentAction(io.automatiko.engine.workflow.base.instance.impl.AssignmentAction) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) Model(io.automatiko.engine.api.Model) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) HashMap(java.util.HashMap) Map(java.util.Map) CompositeContextNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance) NodeInstance(io.automatiko.engine.workflow.process.instance.NodeInstance)

Example 3 with WorkItemExecutionError

use of io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError in project automatiko-engine by automatiko-io.

the class WorkItemNodeInstance method handleException.

protected void handleException(String exceptionName, Exception e) {
    ExceptionScopeInstance exceptionScopeInstance = (ExceptionScopeInstance) resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, exceptionName);
    if (exceptionScopeInstance == null) {
        if (e instanceof WorkItemExecutionError) {
            throw (WorkItemExecutionError) e;
        }
        throw new WorkflowRuntimeException(this, getProcessInstance(), "Unable to execute Action: " + e.getMessage(), e);
    }
    // workItemId must be set otherwise cancel activity will not find the right work
    // item
    this.workItemId = workItem.getId();
    Object param = e;
    if (e instanceof WorkItemExecutionError) {
        param = ((WorkItemExecutionError) e).getErrorData();
    }
    exceptionScopeInstance.handleException(this, exceptionName, param != null ? param : e);
}
Also used : WorkflowRuntimeException(io.automatiko.engine.workflow.process.instance.WorkflowRuntimeException) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) ExceptionScopeInstance(io.automatiko.engine.workflow.base.instance.context.exception.ExceptionScopeInstance)

Example 4 with WorkItemExecutionError

use of io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError in project automatiko-engine by automatiko-io.

the class WorkItemNodeInstance method handleAssignment.

private void handleAssignment(Assignment assignment) {
    AssignmentAction action = (AssignmentAction) assignment.getMetaData("Action");
    try {
        ProcessContext context = new ProcessContext(getProcessInstance().getProcessRuntime());
        context.setNodeInstance(this);
        action.execute(getWorkItem(), context);
    } catch (WorkItemExecutionError e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("unable to execute Assignment", e);
    }
}
Also used : WorkflowRuntimeException(io.automatiko.engine.workflow.process.instance.WorkflowRuntimeException) AssignmentAction(io.automatiko.engine.workflow.base.instance.impl.AssignmentAction) ProcessContext(io.automatiko.engine.workflow.base.core.context.ProcessContext) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) WorkItemHandlerNotFoundException(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemHandlerNotFoundException) ProcessWorkItemHandlerException(io.automatiko.engine.api.runtime.process.ProcessWorkItemHandlerException) WorkflowRuntimeException(io.automatiko.engine.workflow.process.instance.WorkflowRuntimeException)

Example 5 with WorkItemExecutionError

use of io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError in project automatiko-engine by automatiko-io.

the class StateBasedNodeInstance method configureTimerInstance.

protected ExpirationTime configureTimerInstance(Timer timer) {
    String s = null;
    long duration = -1;
    switch(timer.getTimeType()) {
        case Timer.TIME_CYCLE:
            if (CronExpirationTime.isCronExpression(timer.getDelay())) {
                return CronExpirationTime.of(timer.getDelay());
            } else {
                if (timer.getPeriod() != null) {
                    long actualDelay = DateTimeUtils.parseDuration(resolveVariable(timer.getDelay()));
                    if (timer.getPeriod() == null) {
                        return DurationExpirationTime.repeat(actualDelay, actualDelay, Integer.MAX_VALUE);
                    } else {
                        return DurationExpirationTime.repeat(actualDelay, DateTimeUtils.parseDuration(resolveVariable(timer.getPeriod())), Integer.MAX_VALUE);
                    }
                } else {
                    String resolvedDelay = resolveVariable(timer.getDelay());
                    // when using ISO date/time period is not set
                    long[] repeatValues = null;
                    try {
                        repeatValues = DateTimeUtils.parseRepeatableDateTime(resolvedDelay);
                    } catch (RuntimeException e) {
                        // cannot parse delay, trying to interpret it
                        repeatValues = DateTimeUtils.parseRepeatableDateTime(resolvedDelay);
                    }
                    if (repeatValues.length == 3) {
                        int parsedReapedCount = (int) repeatValues[0];
                        if (parsedReapedCount <= -1) {
                            parsedReapedCount = Integer.MAX_VALUE;
                        }
                        return DurationExpirationTime.repeat(repeatValues[1], repeatValues[2], parsedReapedCount);
                    } else if (repeatValues.length == 2) {
                        return DurationExpirationTime.repeat(repeatValues[0], repeatValues[1], Integer.MAX_VALUE);
                    } else {
                        return DurationExpirationTime.repeat(repeatValues[0], repeatValues[0], Integer.MAX_VALUE);
                    }
                }
            }
        case Timer.TIME_DURATION:
            try {
                duration = DateTimeUtils.parseDuration(resolveVariable(timer.getDelay()));
            } catch (RuntimeException e) {
                // cannot parse delay, trying to interpret it
                s = resolveVariable(timer.getDelay());
                duration = DateTimeUtils.parseDuration(s);
            }
            return DurationExpirationTime.after(duration);
        case Timer.TIME_DATE:
            String timeDate = timer.getDate();
            try {
                try {
                    return ExactExpirationTime.of(timer.getDate());
                } catch (RuntimeException e) {
                    // cannot parse delay, trying to interpret it
                    timeDate = resolveVariable(timer.getDate());
                    return ExactExpirationTime.of(timeDate);
                }
            } catch (DateTimeParseException e) {
                throw new WorkItemExecutionError("Parsing of date and time for timer failed", "DateTimeParseFailure", "Unable to parse '" + timeDate + "' as valid ISO date and time format", e);
            }
    }
    throw new UnsupportedOperationException("Not supported timer definition");
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)

Aggregations

WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)14 AssignmentAction (io.automatiko.engine.workflow.base.instance.impl.AssignmentAction)5 WorkflowRuntimeException (io.automatiko.engine.workflow.process.instance.WorkflowRuntimeException)5 ProcessContext (io.automatiko.engine.workflow.base.core.context.ProcessContext)4 WorkItemImpl (io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl)4 DateTimeParseException (java.time.format.DateTimeParseException)3 Model (io.automatiko.engine.api.Model)2 ProcessWorkItemHandlerException (io.automatiko.engine.api.runtime.process.ProcessWorkItemHandlerException)2 ExceptionScopeInstance (io.automatiko.engine.workflow.base.instance.context.exception.ExceptionScopeInstance)2 WorkItemHandlerNotFoundException (io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemHandlerNotFoundException)2 HashMap (java.util.HashMap)2 Application (io.automatiko.engine.api.Application)1 DecisionModel (io.automatiko.engine.api.decision.DecisionModel)1 Connection (io.automatiko.engine.api.definition.process.Connection)1 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)1 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)1 NodeInstanceContainer (io.automatiko.engine.api.runtime.process.NodeInstanceContainer)1 ProcessContext (io.automatiko.engine.api.runtime.process.ProcessContext)1 ExecutionsErrorInfo (io.automatiko.engine.api.workflow.ExecutionsErrorInfo)1 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)1