use of io.automatiko.engine.api.workflow.ConflictingVersionException in project automatiko-engine by automatiko-io.
the class CollectingUnitOfWork method end.
@Override
public void end() {
checkStarted();
Collection<WorkUnit<?>> units = sorted();
EventBatch batch = eventManager.newBatch();
batch.append(units);
for (WorkUnit<?> work : units) {
LOGGER.debug("Performing work unit {}", work);
try {
work.perform();
} catch (ConflictingVersionException e) {
throw e;
} catch (Exception e) {
LOGGER.error("Error during performing work unit {} error message {}", work, e.getMessage(), e);
}
}
eventManager.publish(batch);
done();
}
use of io.automatiko.engine.api.workflow.ConflictingVersionException in project automatiko-engine by automatiko-io.
the class UnitOfWorkExecutor method executeInUnitOfWork.
public static <T> T executeInUnitOfWork(UnitOfWorkManager uowManager, Supplier<T> supplier) {
T result = null;
UnitOfWork uow = uowManager.newUnitOfWork();
try {
uow.start();
result = supplier.get();
uow.end();
return result;
} catch (ProcessInstanceExecutionException e) {
uow.end();
throw e;
} catch (ConflictingVersionException e) {
LOGGER.warn("A conflict was identified for current unit of work with message '{}', aborting current unit of work and retrying", e.getMessage());
uow.abort();
return executeInUnitOfWork(uowManager, supplier);
} catch (Exception e) {
e.printStackTrace();
uow.abort();
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
} finally {
// reset identity provider upon completion of the unit of work
IdentityProvider.set(null);
}
}
use of io.automatiko.engine.api.workflow.ConflictingVersionException in project automatiko-engine by automatiko-io.
the class DatabaseProcessInstances method store.
protected void store(String id, ProcessInstance<ProcessInstanceEntity> instance) {
String resolvedId = resolveId(id, instance);
if (isActive(instance)) {
ProcessInstanceEntity entity = instance.variables();
byte[] data = codec.encode(marshaller.marhsallProcessInstance(instance));
if (data == null) {
return;
}
entity.content = data;
entity.entityId = resolvedId;
entity.name = instance.description();
entity.businessKey = instance.businessKey();
entity.processId = instance.process().id();
entity.processName = instance.process().name();
entity.processVersion = instance.process().version();
entity.startDate = instance.startDate();
entity.state = instance.status();
entity.tags = new HashSet<>(instance.tags().values());
try {
JpaOperations.INSTANCE.persist(entity);
} catch (OptimisticLockException | StaleObjectStateException e) {
throw new ConflictingVersionException("Process instance with id '" + instance.id() + "' has older version than tha stored one");
} finally {
disconnect(instance);
}
}
}
use of io.automatiko.engine.api.workflow.ConflictingVersionException in project automatiko-engine by automatiko-io.
the class DynamoDBProcessInstances method update.
@Override
public void update(String id, ProcessInstance instance) {
String resolvedId = resolveId(id, instance);
if (isActive(instance)) {
LOGGER.debug("update() called for instance {}", resolvedId);
byte[] data = codec.encode(marshaller.marhsallProcessInstance(instance));
if (data == null) {
return;
}
HashMap<String, AttributeValue> itemKey = new HashMap<String, AttributeValue>();
itemKey.put(INSTANCE_ID_FIELD, AttributeValue.builder().s(resolvedId).build());
Map<String, AttributeValueUpdate> updatedValues = new HashMap<String, AttributeValueUpdate>();
updatedValues.put(CONTENT_FIELD, AttributeValueUpdate.builder().value(AttributeValue.builder().b(SdkBytes.fromByteArray(data)).build()).action(AttributeAction.PUT).build());
updatedValues.put(VERSION_FIELD, AttributeValueUpdate.builder().value(AttributeValue.builder().n(String.valueOf(((AbstractProcessInstance<?>) instance).getVersionTracker() + 1)).build()).action(AttributeAction.PUT).build());
updatedValues.put(STATUS_FIELD, AttributeValueUpdate.builder().value(AttributeValue.builder().n(String.valueOf(((AbstractProcessInstance<?>) instance).status())).build()).action(AttributeAction.PUT).build());
Collection<String> tags = new ArrayList(instance.tags().values());
tags.add(resolvedId);
if (instance.businessKey() != null) {
tags.add(instance.businessKey());
}
updatedValues.put(TAGS_FIELD, AttributeValueUpdate.builder().value(AttributeValue.builder().ss(tags).build()).action(AttributeAction.PUT).build());
UpdateItemRequest request = UpdateItemRequest.builder().tableName(tableName).key(itemKey).attributeUpdates(updatedValues).conditionExpression(VERSION_FIELD + " = " + ((AbstractProcessInstance<?>) instance).getVersionTracker()).build();
try {
dynamodb.updateItem(request);
} catch (ConditionalCheckFailedException e) {
throw new ConflictingVersionException("Process instance with id '" + instance.id() + "' has older version than the stored one");
} finally {
disconnect(instance);
}
}
cachedInstances.remove(resolvedId);
}
use of io.automatiko.engine.api.workflow.ConflictingVersionException 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);
}
}
Aggregations