use of io.automatiko.engine.workflow.AbstractProcessInstance in project automatiko-engine by automatiko-io.
the class MongodbProcessInstances method update.
@Override
public void update(String id, ProcessInstance instance) {
String resolvedId = resolveId(id, instance);
try {
if (isActive(instance)) {
byte[] data = codec.encode(marshaller.marhsallProcessInstance(instance));
if (data == null) {
return;
}
Model entity = (Model) instance.variables();
String variablesJson = marshallingStrategy.mapper().writeValueAsString(entity);
Document variables = Document.parse(variablesJson);
removeTransientVariables(variables, instance);
Collection<String> tags = new LinkedHashSet<>(instance.tags().values());
tags.add(resolvedId);
if (instance.businessKey() != null) {
tags.add(instance.businessKey());
}
Document item = new Document(INSTANCE_ID_FIELD, resolvedId).append(CONTENT_FIELD, data).append(STATUS_FIELD, instance.status()).append(TAGS_FIELD, tags).append(VERSION_FIELD, ((AbstractProcessInstance<?>) instance).getVersionTracker()).append(VARIABLES_FIELD, variables);
try {
Document replaced = collection().findOneAndReplace(and(eq(INSTANCE_ID_FIELD, resolvedId), eq(VERSION_FIELD, ((AbstractProcessInstance<?>) instance).getVersionTracker())), item);
if (replaced == null) {
throw new ConflictingVersionException("Process instance with id '" + instance.id() + "' has older version than the stored one");
}
} finally {
cachedInstances.remove(resolvedId);
cachedInstances.remove(id);
disconnect(instance);
}
} else if (isPending(instance)) {
if (cachedInstances.putIfAbsent(resolvedId, instance) != null) {
throw new ProcessInstanceDuplicatedException(id);
}
} else {
cachedInstances.remove(resolvedId);
cachedInstances.remove(id);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of io.automatiko.engine.workflow.AbstractProcessInstance in project automatiko-engine by automatiko-io.
the class MongodbProcessInstances method create.
@Override
public void create(String id, ProcessInstance instance) {
String resolvedId = resolveId(id, instance);
try {
if (isActive(instance)) {
byte[] data = codec.encode(marshaller.marhsallProcessInstance(instance));
if (data == null) {
return;
}
Model entity = (Model) instance.variables();
String variablesJson = marshallingStrategy.mapper().writeValueAsString(entity);
Document variables = Document.parse(variablesJson);
removeTransientVariables(variables, instance);
Collection<String> tags = new LinkedHashSet<>(instance.tags().values());
tags.add(resolvedId);
if (instance.businessKey() != null) {
tags.add(instance.businessKey());
}
Document item = new Document(INSTANCE_ID_FIELD, resolvedId).append(CONTENT_FIELD, data).append(STATUS_FIELD, instance.status()).append(TAGS_FIELD, tags).append(VERSION_FIELD, ((AbstractProcessInstance<?>) instance).getVersionTracker()).append(VARIABLES_FIELD, variables);
try {
collection().insertOne(item);
} finally {
cachedInstances.remove(resolvedId);
cachedInstances.remove(id);
disconnect(instance);
}
} else if (isPending(instance)) {
if (cachedInstances.putIfAbsent(resolvedId, instance) != null) {
throw new ProcessInstanceDuplicatedException(id);
}
} else {
cachedInstances.remove(resolvedId);
cachedInstances.remove(id);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of io.automatiko.engine.workflow.AbstractProcessInstance 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.workflow.AbstractProcessInstance in project automatiko-engine by automatiko-io.
the class DynamoDBProcessInstances method disconnect.
protected void disconnect(ProcessInstance instance) {
((AbstractProcessInstance<?>) instance).internalRemoveProcessInstance(() -> {
try {
Map<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
keyToGet.put(INSTANCE_ID_FIELD, AttributeValue.builder().s(instance.id()).build());
GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName(tableName).build();
Map<String, AttributeValue> returnedItem = dynamodb.getItem(request).item();
if (returnedItem != null) {
byte[] reloaded = returnedItem.get(CONTENT_FIELD).b().asByteArray();
return marshaller.unmarshallWorkflowProcessInstance(codec.decode(reloaded), process);
} else {
return null;
}
} catch (RuntimeException e) {
LOGGER.error("Unexpected exception thrown when reloading process instance {}", instance.id(), e);
return null;
}
});
}
use of io.automatiko.engine.workflow.AbstractProcessInstance in project automatiko-engine by automatiko-io.
the class MilestoneTest method testSimpleMilestone.
@Test
void testSimpleMilestone() throws Exception {
Application app = generateCodeProcessesOnly("cases/milestones/SimpleMilestone.bpmn");
assertThat(app).isNotNull();
Process<? extends Model> p = app.processes().processById("TestCase.SimpleMilestone_1_0");
ProcessInstance<?> processInstance = p.createInstance(p.createModel());
assertState(processInstance, ProcessInstance.STATE_PENDING);
Collection<Milestone> expected = new ArrayList<>();
expected.add(Milestone.builder().withName("AutoStartMilestone").withStatus(AVAILABLE).build());
expected.add(Milestone.builder().withName("SimpleMilestone").withStatus(AVAILABLE).build());
assertMilestones(expected, processInstance.milestones());
processInstance.start();
assertState(processInstance, ProcessInstance.STATE_COMPLETED);
expected = expected.stream().map(m -> Milestone.builder().withId(m.getId()).withName(m.getName()).withStatus(COMPLETED).build()).collect(Collectors.toList());
assertMilestones(expected, processInstance.milestones());
ExecutableProcessInstance legacyProcessInstance = (ExecutableProcessInstance) ((AbstractProcessInstance<?>) processInstance).processInstance();
assertThat(legacyProcessInstance.getNodeInstances()).isEmpty();
Optional<String> milestoneId = Stream.of(legacyProcessInstance.getNodeContainer().getNodes()).filter(node -> node.getName().equals("SimpleMilestone")).map(n -> (String) n.getMetaData().get(Metadata.UNIQUE_ID)).findFirst();
assertTrue(milestoneId.isPresent());
assertThat(legacyProcessInstance.getCompletedNodeIds()).contains(milestoneId.get());
}
Aggregations