Search in sources :

Example 6 with WorkflowProcessInstance

use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.

the class ProcessInstanceMarshaller method importProcessInstance.

@SuppressWarnings({ "rawtypes", "unchecked" })
public ProcessInstance importProcessInstance(ExportedProcessInstance<?> instance, Process process) {
    List<Map<String, String>> timers = instance.convertTimers();
    WorkflowProcessInstance wpi = importWorkflowProcessInstance((String) instance.getHeader(), (String) instance.getInstance(), timers, process);
    Model model = ((AbstractProcess) process).createModel();
    model.fromMap(wpi.getVariables());
    ProcessInstance processInstance = ((AbstractProcess) process).createInstance(wpi, model, 1);
    if (timers != null && !timers.isEmpty()) {
        String parentProcessInstanceId = wpi.getParentProcessInstanceId();
        if (parentProcessInstanceId != null && !parentProcessInstanceId.isEmpty()) {
            parentProcessInstanceId += ":";
        } else {
            parentProcessInstanceId = "";
        }
        JobsService jobService = ((WorkflowProcessInstanceImpl) wpi).getProcessRuntime().getJobsService();
        Collection<io.automatiko.engine.workflow.process.instance.NodeInstance> nodes = ((WorkflowProcessInstanceImpl) wpi).getNodeInstances(true);
        // keeps iterator for statebased node instance timers
        Map<String, Iterator<Timer>> nodeInstanceTimers = new LinkedHashMap<>();
        for (Map<String, String> timer : timers) {
            String nodeInstanceId = timer.get("nodeInstanceId");
            for (io.automatiko.engine.workflow.process.instance.NodeInstance ni : nodes) {
                if (ni.getId().equals(nodeInstanceId)) {
                    ExpirationTime expirationTime = null;
                    long timerNodeId = 0;
                    if (((NodeInstanceImpl) ni).getRetryJobId() != null && ((NodeInstanceImpl) ni).getRetryJobId().equals(timer.get("timerId"))) {
                        jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), ni.getNodeId(), "retry:" + ni.getId(), expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), ni.getProcessInstance().getRootProcessInstanceId(), ni.getProcessInstance().getProcessId(), ni.getProcessInstance().getProcess().getVersion(), ni.getProcessInstance().getRootProcessId()));
                        break;
                    } else if (ni instanceof TimerNodeInstance) {
                        TimerNodeInstance nodeInstance = (TimerNodeInstance) ni;
                        timerNodeId = nodeInstance.getTimerNode().getTimer().getId();
                        expirationTime = nodeInstance.createTimerInstance(nodeInstance.getTimerNode().getTimer());
                        expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
                    } else if (ni instanceof StateBasedNodeInstance) {
                        StateBasedNodeInstance nodeInstance = (StateBasedNodeInstance) ni;
                        if (nodeInstance.getTimerInstances().contains(timer.get("timerId"))) {
                            Iterator<Timer> it = nodeInstanceTimers.computeIfAbsent(nodeInstanceId, k -> nodeInstance.getEventBasedNode().getTimers().keySet().iterator());
                            expirationTime = nodeInstance.createTimerInstance(it.next());
                            expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
                        }
                    }
                    // lastly schedule timer on calculated expiration time
                    jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), timerNodeId, expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), wpi.getRootProcessInstanceId(), wpi.getProcessId(), wpi.getProcess().getVersion(), wpi.getRootProcessId()));
                    break;
                }
            }
        }
    }
    return processInstance;
}
Also used : WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ExpirationTime(io.automatiko.engine.api.jobs.ExpirationTime) LinkedHashMap(java.util.LinkedHashMap) JobsService(io.automatiko.engine.api.jobs.JobsService) Iterator(java.util.Iterator) NodeInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl) StateBasedNodeInstance(io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) Model(io.automatiko.engine.api.Model) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) StringExportedProcessInstance(io.automatiko.engine.workflow.StringExportedProcessInstance) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StateBasedNodeInstance(io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance) TimerNodeInstance(io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance) TimerNodeInstance(io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 7 with WorkflowProcessInstance

use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.

the class CancelNodeInstanceAction method execute.

public void execute(ProcessContext context) throws Exception {
    WorkflowProcessInstance pi = context.getNodeInstance().getProcessInstance();
    NodeInstance nodeInstance = findNodeByUniqueId(pi.getNodeInstances(), attachedToNodeId);
    if (nodeInstance != null) {
        ((io.automatiko.engine.workflow.process.instance.NodeInstance) nodeInstance).cancel();
    }
}
Also used : NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) CompositeNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeNodeInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 8 with WorkflowProcessInstance

use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.

the class ProcessInstanceEventBatch method createUserTask.

protected UserTaskInstanceEventBody createUserTask(ProcessWorkItemTransitionEvent workItemTransitionEvent) {
    WorkflowProcessInstance pi = (WorkflowProcessInstance) workItemTransitionEvent.getProcessInstance();
    HumanTaskWorkItem workItem = (HumanTaskWorkItem) workItemTransitionEvent.getWorkItem();
    String parentProcessInstanceId = pi.getParentProcessInstanceId();
    if (parentProcessInstanceId != null && !parentProcessInstanceId.isEmpty()) {
        parentProcessInstanceId += ":";
    } else {
        parentProcessInstanceId = "";
    }
    String version = version(pi.getProcess().getVersion());
    String encoded = Base64.getEncoder().encodeToString((pi.getProcessId() + version + "|" + parentProcessInstanceId + pi.getId() + "|" + workItem.getId() + "|").getBytes(StandardCharsets.UTF_8));
    String link = "/management/tasks/link/" + encoded;
    return UserTaskInstanceEventBody.create().id(workItem.getId()).state(workItem.getPhaseStatus()).taskName(workItem.getTaskName()).taskDescription(workItem.getTaskDescription()).taskPriority(workItem.getTaskPriority()).referenceName(workItem.getReferenceName()).referenceId(workItem.getReferenceId()).formLink(link).actualOwner(workItem.getActualOwner()).startDate(workItem.getStartDate()).completeDate(workItem.getCompleteDate()).adminGroups(workItem.getAdminGroups()).adminUsers(workItem.getAdminUsers()).excludedUsers(workItem.getExcludedUsers()).potentialGroups(workItem.getPotentialGroups()).potentialUsers(workItem.getPotentialUsers()).processInstanceId(pi.getId()).rootProcessInstanceId(pi.getRootProcessInstanceId()).processId(pi.getProcessId()).rootProcessId(pi.getRootProcessId()).inputs(workItem.getParameters()).outputs(workItem.getResults()).instance(workItem).build();
}
Also used : HumanTaskWorkItem(io.automatiko.engine.api.runtime.process.HumanTaskWorkItem) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 9 with WorkflowProcessInstance

use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.

the class TracingAdds method addTags.

public void addTags(io.automatiko.engine.api.runtime.process.ProcessInstance instance) {
    if (tracer.isResolvable()) {
        Tracer tracerInstance = tracer.get();
        tracerInstance.activeSpan().setTag("workflow.instance.id", instance.getId());
        tracerInstance.activeSpan().setTag("workflow.root.instance.id", instance.getRootProcessInstanceId() == null ? instance.getId() : instance.getRootProcessInstanceId());
        if (instance.getCorrelationKey() != null) {
            tracerInstance.activeSpan().setTag("workflow.business.key", instance.getCorrelationKey());
        }
        Collection<Tag> tags = ((WorkflowProcessInstance) instance).getTags();
        if (!tags.isEmpty()) {
            tracerInstance.activeSpan().setTag("workflow.instance.tags", tags.stream().map(t -> t.getValue()).collect(Collectors.joining(",")));
        }
    }
}
Also used : Tracer(io.opentracing.Tracer) Tag(io.automatiko.engine.api.workflow.Tag) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 10 with WorkflowProcessInstance

use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.

the class DatabaseProcessInstances method disconnect.

protected void disconnect(ProcessInstance<ProcessInstanceEntity> instance) {
    ((AbstractProcessInstance<?>) instance).internalRemoveProcessInstance(() -> {
        try {
            ProcessInstanceEntity entity = (ProcessInstanceEntity) JpaOperations.INSTANCE.findById(type, resolveId(instance.id(), instance));
            byte[] reloaded = codec.decode(entity.content);
            WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(reloaded, process);
            entity.toMap().forEach((k, v) -> {
                if (v != null) {
                    v.toString();
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
                    variableScopeInstance.internalSetVariable(k, v);
                }
            });
            return wpi;
        } catch (RuntimeException e) {
            LOGGER.error("Unexpected exception thrown when reloading process instance {}", instance.id(), e);
            return null;
        }
    });
}
Also used : AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) ProcessInstanceEntity(io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Aggregations

WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)18 Model (io.automatiko.engine.api.Model)5 AbstractProcess (io.automatiko.engine.workflow.AbstractProcess)5 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)5 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)5 ProcessInstance (io.automatiko.engine.api.runtime.process.ProcessInstance)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)3 ExportedProcessInstance (io.automatiko.engine.api.workflow.ExportedProcessInstance)3 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)3 NodeInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl)3 TimerNodeInstance (io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 ProcessInstanceEntity (io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity)2 Process (io.automatiko.engine.api.definition.process.Process)2 Process (io.automatiko.engine.api.workflow.Process)2 StringExportedProcessInstance (io.automatiko.engine.workflow.StringExportedProcessInstance)2 WorkflowProcessInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)2