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;
}
});
}
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);
}
}
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());
}
}
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);
}
}
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);
}
}
Aggregations