Search in sources :

Example 11 with WorkflowProcessInstance

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

the class MongodbProcessInstances method disconnect.

protected void disconnect(ProcessInstance instance) {
    ((AbstractProcessInstance<?>) instance).internalRemoveProcessInstance(() -> {
        try {
            String resolvedId = instance.id();
            Document returnedItem = collection().find(eq(INSTANCE_ID_FIELD, resolvedId)).projection(Projections.fields(Projections.include(INSTANCE_ID_FIELD, CONTENT_FIELD, VERSION_FIELD, VARIABLES_FIELD))).first();
            if (returnedItem != null) {
                byte[] reloaded = returnedItem.get(CONTENT_FIELD, Binary.class).getData();
                WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(reloaded), process);
                String variablesJson = returnedItem.get(VARIABLES_FIELD, Document.class).toJson();
                Model model = process.createModel();
                Map<String, Object> loaded = marshallingStrategy.mapper().readValue(variablesJson, model.getClass()).toMap();
                model.fromMap(loaded);
                loaded.forEach((k, v) -> {
                    if (v != null) {
                        v.toString();
                        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
                        variableScopeInstance.internalSetVariable(k, v);
                    }
                });
                return wpi;
            } else {
                return null;
            }
        } catch (IOException e) {
            LOGGER.error("Unexpected exception thrown when reloading process instance {}", instance.id(), e);
            return null;
        }
    });
}
Also used : AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) Model(io.automatiko.engine.api.Model) Binary(org.bson.types.Binary) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Document(org.bson.Document) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 12 with WorkflowProcessInstance

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

the class MongodbProcessInstances method unmarshallInstance.

protected ProcessInstance unmarshallInstance(ProcessInstanceReadMode mode, Document entity) {
    try {
        ProcessInstance pi;
        if (mode == MUTABLE) {
            WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(entity.get(CONTENT_FIELD, Binary.class).getData()), process);
            String variablesJson = entity.get(VARIABLES_FIELD, Document.class).toJson();
            Model model = process.createModel();
            Map<String, Object> loaded = marshallingStrategy.mapper().readValue(variablesJson, model.getClass()).toMap();
            model.fromMap(loaded);
            loaded.forEach((k, v) -> {
                if (v != null) {
                    v.toString();
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
                    variableScopeInstance.internalSetVariable(k, v);
                }
            });
            pi = ((AbstractProcess) process).createInstance(wpi, model, entity.getLong(VERSION_FIELD));
        } else {
            WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(entity.get(CONTENT_FIELD, Binary.class).getData()), process);
            String variablesJson = entity.get(VARIABLES_FIELD, Document.class).toJson();
            Model model = process.createModel();
            Map<String, Object> loaded = marshallingStrategy.mapper().readValue(variablesJson, model.getClass()).toMap();
            model.fromMap(loaded);
            loaded.forEach((k, v) -> {
                if (v != null) {
                    v.toString();
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
                    variableScopeInstance.internalSetVariable(k, v);
                }
            });
            pi = ((AbstractProcess) process).createReadOnlyInstance(wpi, model);
        }
        return pi;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) 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) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance) UncheckedIOException(java.io.UncheckedIOException) Binary(org.bson.types.Binary) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Document(org.bson.Document) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 13 with WorkflowProcessInstance

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

the class FaultNodeInstance method internalTrigger.

public void internalTrigger(final NodeInstance from, String type) {
    if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
        throw new IllegalArgumentException("A FaultNode only accepts default incoming connections!");
    }
    triggerTime = new Date();
    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;
        });
    }
    String faultName = getFaultName();
    ExceptionScopeInstance exceptionScopeInstance = getExceptionScopeInstance(faultName);
    NodeInstanceContainer nodeInstanceContainer = (NodeInstanceContainer) getNodeInstanceContainer();
    nodeInstanceContainer.removeNodeInstance(this);
    boolean exceptionHandled = false;
    if (getFaultNode().isTerminateParent()) {
        // events
        if (exceptionScopeInstance != null) {
            exceptionHandled = true;
            handleException(faultName, exceptionScopeInstance);
        }
        if (nodeInstanceContainer instanceof CompositeNodeInstance) {
            ((CompositeNodeInstance) nodeInstanceContainer).cancel();
        } else if (nodeInstanceContainer instanceof WorkflowProcessInstance) {
            Collection<NodeInstance> nodeInstances = ((WorkflowProcessInstance) nodeInstanceContainer).getNodeInstances();
            for (NodeInstance nodeInstance : nodeInstances) {
                ((io.automatiko.engine.workflow.process.instance.NodeInstance) nodeInstance).cancel();
            }
        }
    }
    String uniqueId = (String) getNode().getMetaData().get(UNIQUE_ID);
    if (uniqueId == null) {
        uniqueId = ((NodeImpl) getNode()).getUniqueId();
    }
    ((WorkflowProcessInstanceImpl) getProcessInstance()).addCompletedNodeId(uniqueId);
    if (exceptionScopeInstance != null) {
        if (!exceptionHandled) {
            handleException(faultName, exceptionScopeInstance);
        }
        boolean hidden = false;
        if (getNode().getMetaData().get("hidden") != null) {
            hidden = true;
        }
        if (!hidden) {
            InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
            runtime.getProcessEventSupport().fireBeforeNodeLeft(this, runtime);
        }
        ((NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, null);
        if (!hidden) {
            InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
            runtime.getProcessEventSupport().fireAfterNodeLeft(this, runtime);
        }
    } else {
        ((ProcessInstance) getProcessInstance()).setState(ProcessInstance.STATE_ABORTED, faultName, getFaultData());
    }
}
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) Collection(java.util.Collection) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) 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) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) ExceptionScopeInstance(io.automatiko.engine.workflow.base.instance.context.exception.ExceptionScopeInstance)

Example 14 with WorkflowProcessInstance

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

the class ProcessInstanceMarshaller method unmarshallWorkflowProcessInstance.

public WorkflowProcessInstance unmarshallWorkflowProcessInstance(byte[] data, Process<?> process) {
    Map<String, io.automatiko.engine.api.definition.process.Process> processes = new HashMap<String, io.automatiko.engine.api.definition.process.Process>();
    io.automatiko.engine.api.definition.process.Process p = ((AbstractProcess<?>) process).process();
    // this can include version number in the id
    processes.put(process.id(), p);
    // this is raw process id as defined in bpmn or so
    processes.put(p.getId(), p);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
        MarshallerReaderContext context = new MarshallerReaderContext(bais, null, processes, this.env);
        ObjectInputStream stream = context.stream;
        String processInstanceType = stream.readUTF();
        io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processInstanceType);
        WorkflowProcessInstance pi = (WorkflowProcessInstance) marshaller.readProcessInstance(context);
        context.close();
        return pi;
    } catch (Exception e) {
        throw new RuntimeException("Error while unmarshalling process instance", e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) Process(io.automatiko.engine.api.workflow.Process) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) MarshallerReaderContext(io.automatiko.engine.workflow.marshalling.impl.MarshallerReaderContext) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) ObjectInputStream(java.io.ObjectInputStream)

Example 15 with WorkflowProcessInstance

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

the class ProcessInstanceMarshaller method unmarshallProcessInstance.

@SuppressWarnings({ "rawtypes", "unchecked" })
public ProcessInstance unmarshallProcessInstance(byte[] data, Process process, long versionracker) {
    WorkflowProcessInstance wpi = unmarshallWorkflowProcessInstance(data, process);
    Model model = ((AbstractProcess) process).createModel();
    model.fromMap(wpi.getVariables());
    if (wpi.getState() == ProcessInstance.STATE_ACTIVE || wpi.getState() == ProcessInstance.STATE_ERROR) {
        return ((AbstractProcess) process).createInstance(wpi, model, versionracker);
    } else {
        return ((AbstractProcess) process).createReadOnlyInstance(wpi, model);
    }
}
Also used : AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) Model(io.automatiko.engine.api.Model) 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