Search in sources :

Example 11 with AbstractProcessInstance

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

Example 12 with AbstractProcessInstance

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) 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)

Example 13 with AbstractProcessInstance

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;
        }
    });
}
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 14 with AbstractProcessInstance

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

Example 15 with AbstractProcessInstance

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());
}
Also used : AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Metadata(io.automatiko.engine.workflow.process.executable.core.Metadata) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) COMPLETED(io.automatiko.engine.api.workflow.flexible.ItemDescription.Status.COMPLETED) ArrayList(java.util.ArrayList) Application(io.automatiko.engine.api.Application) Map(java.util.Map) Process(io.automatiko.engine.api.workflow.Process) Model(io.automatiko.engine.api.Model) Collection(java.util.Collection) ExecutableProcessInstance(io.automatiko.engine.workflow.process.executable.instance.ExecutableProcessInstance) ProcessTestUtils.assertState(io.automatiko.engine.codegen.tests.ProcessTestUtils.assertState) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) AVAILABLE(io.automatiko.engine.api.workflow.flexible.ItemDescription.Status.AVAILABLE) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Milestone(io.automatiko.engine.api.workflow.flexible.Milestone) WorkItem(io.automatiko.engine.api.workflow.WorkItem) Milestone(io.automatiko.engine.api.workflow.flexible.Milestone) ArrayList(java.util.ArrayList) ExecutableProcessInstance(io.automatiko.engine.workflow.process.executable.instance.ExecutableProcessInstance) Application(io.automatiko.engine.api.Application) AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)18 Model (io.automatiko.engine.api.Model)7 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)6 HashMap (java.util.HashMap)6 IOException (java.io.IOException)5 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)4 ProcessInstanceDuplicatedException (io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException)4 WorkflowProcessInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)4 Application (io.automatiko.engine.api.Application)3 Process (io.automatiko.engine.api.workflow.Process)3 UncheckedIOException (java.io.UncheckedIOException)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Document (org.bson.Document)3 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)3 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)2 IdentityProvider (io.automatiko.engine.api.auth.IdentityProvider)2 SecurityPolicy (io.automatiko.engine.api.auth.SecurityPolicy)2