Search in sources :

Example 1 with ConflictingVersionException

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();
}
Also used : ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException) WorkUnit(io.automatiko.engine.api.uow.WorkUnit) EventBatch(io.automatiko.engine.api.event.EventBatch) ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException)

Example 2 with ConflictingVersionException

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);
    }
}
Also used : UnitOfWork(io.automatiko.engine.api.uow.UnitOfWork) ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException) ProcessInstanceExecutionException(io.automatiko.engine.api.workflow.ProcessInstanceExecutionException) ProcessInstanceExecutionException(io.automatiko.engine.api.workflow.ProcessInstanceExecutionException) ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException)

Example 3 with ConflictingVersionException

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);
        }
    }
}
Also used : ProcessInstanceEntity(io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity) ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException) OptimisticLockException(javax.persistence.OptimisticLockException) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 4 with ConflictingVersionException

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);
}
Also used : AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException)

Example 5 with ConflictingVersionException

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException) ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) Model(io.automatiko.engine.api.Model) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Document(org.bson.Document)

Aggregations

ConflictingVersionException (io.automatiko.engine.api.workflow.ConflictingVersionException)6 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)2 LinkedHashSet (java.util.LinkedHashSet)2 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 ProcessInstanceEntity (io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity)1 Model (io.automatiko.engine.api.Model)1 EventBatch (io.automatiko.engine.api.event.EventBatch)1 UnitOfWork (io.automatiko.engine.api.uow.UnitOfWork)1 WorkUnit (io.automatiko.engine.api.uow.WorkUnit)1 ProcessInstanceDuplicatedException (io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException)1 ProcessInstanceExecutionException (io.automatiko.engine.api.workflow.ProcessInstanceExecutionException)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 Document (org.bson.Document)1 StaleObjectStateException (org.hibernate.StaleObjectStateException)1