Search in sources :

Example 1 with WorkItemImpl

use of io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl in project automatiko-engine by automatiko-io.

the class AbstractProtobufProcessInstanceMarshaller method readWorkItem.

public static WorkItem readWorkItem(WorkflowProcessInstance processInstance, MarshallerReaderContext context, AutomatikoMessages.WorkItem _workItem) throws IOException {
    WorkItemImpl workItem = new WorkItemImpl();
    workItem.setId(_workItem.getId());
    workItem.setProcessInstanceId(_workItem.getProcessInstancesId());
    workItem.setName(_workItem.getName());
    workItem.setState(_workItem.getState());
    workItem.setDeploymentId(_workItem.getDeploymentId());
    workItem.setNodeId(_workItem.getNodeId());
    workItem.setNodeInstanceId(_workItem.getNodeInstanceId());
    workItem.setPhaseId(_workItem.getPhaseId());
    workItem.setPhaseStatus(_workItem.getPhaseStatus());
    workItem.setStartDate(new Date(_workItem.getStartDate()));
    workItem.setProcessInstance(processInstance);
    if (_workItem.getCompleteDate() > 0) {
        workItem.setCompleteDate(new Date(_workItem.getCompleteDate()));
    }
    for (AutomatikoMessages.Variable _variable : _workItem.getVariableList()) {
        try {
            Object value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
            workItem.setParameter(_variable.getName(), value);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(e);
        }
    }
    return workItem;
}
Also used : WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) HumanTaskWorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.humantask.HumanTaskWorkItemImpl) Date(java.util.Date)

Example 2 with WorkItemImpl

use of io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl in project automatiko-engine by automatiko-io.

the class TaskInputJqAssignmentAction method execute.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void execute(WorkItem workItem, ProcessContext context) throws Exception {
    Object sdata = context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY);
    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
    Map<String, Object> vars = new HashMap<>();
    vars.put("workflowdata", sdata);
    if (context.getVariable("$CONST") != null) {
        vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
    }
    if (inputFilterExpression != null) {
        sdata = evaluator.evaluate(inputFilterExpression, vars);
    }
    if (context.getNodeInstance() instanceof ContextableInstance) {
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ContextableInstance) context.getNodeInstance()).getContextInstance(VariableScope.VARIABLE_SCOPE);
        variableScopeInstance.setVariable(JsonVariableScope.WORKFLOWDATA_KEY, sdata);
    }
    if (paramNames != null && !paramNames.isEmpty()) {
        vars = new HashMap<>();
        vars.put("workflowdata", sdata);
        if (context.getVariable("$CONST") != null) {
            vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
        }
        for (String name : paramNames) {
            Object param = workItem.getParameter(name);
            if (param != null) {
                param = evaluator.evaluate(param.toString(), vars);
                ((WorkItemImpl) workItem).setParameter(name, param);
            }
        }
    } else {
        ((WorkItemImpl) workItem).setParameter("Parameter", sdata);
    }
}
Also used : ContextableInstance(io.automatiko.engine.workflow.base.instance.ContextableInstance) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 3 with WorkItemImpl

use of io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl in project automatiko-engine by automatiko-io.

the class LightWorkItemManager method internalExecuteWorkItem.

public void internalExecuteWorkItem(WorkItem workItem) {
    ((WorkItemImpl) workItem).setId(UUID.randomUUID().toString());
    internalAddWorkItem(workItem);
    WorkItemHandler handler = this.workItemHandlers.get(workItem.getName());
    if (handler != null) {
        ProcessInstance processInstance = workItem.getProcessInstance();
        Transition<?> transition = new TransitionToActive();
        eventSupport.fireBeforeWorkItemTransition(processInstance, workItem, transition, null);
        handler.executeWorkItem(workItem, this);
        eventSupport.fireAfterWorkItemTransition(processInstance, workItem, transition, null);
    } else
        throw new WorkItemHandlerNotFoundException(workItem.getName());
}
Also used : WorkItemHandler(io.automatiko.engine.api.runtime.process.WorkItemHandler) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) WorkItemHandlerNotFoundException(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemHandlerNotFoundException) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance)

Example 4 with WorkItemImpl

use of io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl in project automatiko-engine by automatiko-io.

the class LightWorkItemManager method abortWorkItem.

public void abortWorkItem(String id, Policy<?>... policies) {
    WorkItemImpl workItem = (WorkItemImpl) workItems.get(id);
    // work item may have been aborted
    if (workItem != null) {
        if (!workItem.enforce(policies)) {
            throw new NotAuthorizedException("Work item can be aborted as it does not fulfil policies (e.g. security)");
        }
        ProcessInstance processInstance = workItem.getProcessInstance();
        Transition<?> transition = new TransitionToAbort(Arrays.asList(policies));
        eventSupport.fireBeforeWorkItemTransition(processInstance, workItem, transition, null);
        workItem.setState(ABORTED);
        abortPhase.apply(workItem, transition);
        // process instance may have finished already
        if (processInstance != null) {
            processInstance.signalEvent("workItemAborted", workItem);
        }
        workItem.setPhaseId(ID);
        workItem.setPhaseStatus(STATUS);
        eventSupport.fireAfterWorkItemTransition(processInstance, workItem, transition, null);
        workItems.remove(id);
    }
}
Also used : WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) NotAuthorizedException(io.automatiko.engine.api.workflow.workitem.NotAuthorizedException)

Example 5 with WorkItemImpl

use of io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl 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)

Aggregations

WorkItemImpl (io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl)21 ProcessInstance (io.automatiko.engine.api.runtime.process.ProcessInstance)6 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)4 WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)4 Date (java.util.Date)4 ProcessContext (io.automatiko.engine.workflow.base.core.context.ProcessContext)3 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)3 AssignmentAction (io.automatiko.engine.workflow.base.instance.impl.AssignmentAction)3 DefaultWorkItemManager (io.automatiko.engine.workflow.base.instance.impl.workitem.DefaultWorkItemManager)3 WorkItemHandlerNotFoundException (io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemHandlerNotFoundException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Matcher (java.util.regex.Matcher)3 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)2 WorkItemHandler (io.automatiko.engine.api.runtime.process.WorkItemHandler)2 HumanTaskWorkItemImpl (io.automatiko.engine.workflow.base.instance.impl.humantask.HumanTaskWorkItemImpl)2 WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)2 NodeInstance (io.automatiko.engine.workflow.process.instance.NodeInstance)2 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)2 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)1