Search in sources :

Example 11 with ProcessInstanceDuplicatedException

use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.

the class ProcessInstanceDuplicatedExceptionMapper method toResponse.

@Override
public Response toResponse(ProcessInstanceDuplicatedException ex) {
    ProcessInstanceDuplicatedException exception = (ProcessInstanceDuplicatedException) ex;
    Map<String, String> response = new HashMap<>();
    response.put(MESSAGE, exception.getMessage());
    response.put(PROCESS_INSTANCE_ID, exception.getProcessInstanceId());
    return conflict(response);
}
Also used : ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) HashMap(java.util.HashMap)

Example 12 with ProcessInstanceDuplicatedException

use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.

the class MapProcessInstances method importInstance.

@Override
public ProcessInstance importInstance(ExportedProcessInstance instance, Process process) {
    ProcessInstance imported = marshaller.importProcessInstance(instance, process);
    if (exists(imported.id())) {
        throw new ProcessInstanceDuplicatedException(imported.id());
    }
    create(imported.id(), imported);
    return imported;
}
Also used : ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance)

Example 13 with ProcessInstanceDuplicatedException

use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.

the class $Type$MessageConsumer method consume.

public CompletionStage<Void> consume(Message<byte[]> msg) {
    final String trigger = "$Trigger$";
    try {
        IdentityProvider.set(new TrustedIdentityProvider("System<messaging>"));
        if (useCloudEvents.orElse(true)) {
            final $DataEventType$ eventData = json.readValue(msg.getPayload(), $DataEventType$.class);
            final $Type$ model = new $Type$();
            io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
                String correlation = correlationEvent(eventData, msg);
                if (correlation != null) {
                    LOGGER.debug("Correlation ({}) is set, attempting to find if there is matching instance already active", correlation);
                    Optional possiblyFound = process.instances().findById(correlation);
                    if (possiblyFound.isPresent()) {
                        ProcessInstance pInstance = (ProcessInstance) possiblyFound.get();
                        LOGGER.debug("Found process instance {} matching correlation {}, signaling instead of starting new instance", pInstance.id(), correlation);
                        pInstance.send(Sig.of(trigger, eventData.getData()));
                        return null;
                    }
                }
                LOGGER.debug("Received message without reference id and no correlation is set/matched, staring new process instance with trigger '{}'", trigger);
                try {
                    ProcessInstance<$Type$> pi = process.createInstance(correlation, model);
                    pi.start(trigger, null, eventData.getData());
                } catch (ProcessInstanceDuplicatedException e) {
                    ProcessInstance<$Type$> pi = process.instances().findById(correlation).get();
                    pi.send(Sig.of(trigger, eventData.getData()));
                }
                return null;
            });
        } else {
            final $DataType$ eventData = json.readValue(msg.getPayload(), $DataType$.class);
            final $Type$ model = new $Type$();
            io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
                String correlation = correlationPayload(eventData, msg);
                if (correlation != null) {
                    LOGGER.debug("Correlation ({}) is set, attempting to find if there is matching instance already active", correlation);
                    Optional possiblyFound = process.instances().findById(correlation);
                    if (possiblyFound.isPresent()) {
                        ProcessInstance pInstance = (ProcessInstance) possiblyFound.get();
                        LOGGER.debug("Found process instance {} matching correlation {}, signaling instead of starting new instance", pInstance.id(), correlation);
                        pInstance.send(Sig.of(trigger, eventData));
                        return null;
                    }
                }
                LOGGER.debug("Received message without reference id and no correlation is set/matched, staring new process instance with trigger '{}'", trigger);
                try {
                    ProcessInstance<$Type$> pi = process.createInstance(correlation, model);
                    pi.start(trigger, null, eventData);
                } catch (ProcessInstanceDuplicatedException e) {
                    ProcessInstance<$Type$> pi = process.instances().findById(correlation).get();
                    pi.send(Sig.of(trigger, eventData));
                }
                return null;
            });
        }
        return msg.ack();
    } catch (Exception e) {
        LOGGER.error("Error when consuming message for process {}", process.id(), e);
        return msg.nack(e);
    }
}
Also used : ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) Optional(java.util.Optional) TrustedIdentityProvider(io.automatiko.engine.api.auth.TrustedIdentityProvider) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException)

Example 14 with ProcessInstanceDuplicatedException

use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.

the class $Type$MessageConsumer method consume.

public CompletionStage<Void> consume(Message<String> msg) {
    metrics.messageReceived(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
    final String trigger = "$Trigger$";
    try {
        IdentityProvider.set(new TrustedIdentityProvider("System<messaging>"));
        final $DataType$ eventData;
        final $Type$ model;
        final String correlation;
        LOGGER.debug("Received message with payload '{}'", msg.getPayload());
        boolean accepted;
        if (useCloudEvents.orElse(false)) {
            $DataEventType$ event;
            String contentType = appProperty(msg, "contentType");
            model = new $Type$();
            if (contentType != null && contentType.startsWith("application/cloudevents+json")) {
                // structured
                event = json.readValue(msg.getPayload(), $DataEventType$.class);
                eventData = event.getData();
            } else {
                // binary
                eventData = convert(msg, $DataType$.class);
                event = new $DataEventType$(appProperty(msg, "ce_specversion"), appProperty(msg, "ce_id"), appProperty(msg, "ce_source"), appProperty(msg, "ce_type"), appProperty(msg, "ce_subject"), appProperty(msg, "ce_time"), eventData);
                cloudEventsExtensions(msg, event);
            }
            correlation = correlationEvent(event, msg);
            accepted = acceptedEvent(event, msg);
        } else {
            eventData = convert(msg, $DataType$.class);
            model = new $Type$();
            correlation = correlationPayload(eventData, msg);
            accepted = acceptedPayload(eventData, msg);
        }
        if (!accepted) {
            metrics.messageRejected(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
            LOGGER.debug("Message has been rejected by filter expression");
            return msg.ack();
        }
        io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
            if (correlation != null) {
                LOGGER.debug("Correlation ({}) is set, attempting to find if there is matching instance already active", correlation);
                Collection possiblyFound = process.instances().findByIdOrTag(io.automatiko.engine.api.workflow.ProcessInstanceReadMode.MUTABLE, correlation);
                if (!possiblyFound.isEmpty()) {
                    metrics.messageConsumed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
                    possiblyFound.forEach(pi -> {
                        ProcessInstance pInstance = (ProcessInstance) pi;
                        LOGGER.debug("Found process instance {} matching correlation {}, signaling instead of starting new instance", pInstance.id(), correlation);
                        pInstance.send(Sig.of(canStartInstance() ? trigger : "Message-" + trigger, eventData));
                    });
                    return null;
                }
            }
            if (canStartInstance()) {
                LOGGER.debug("Received message without reference id and no correlation is set/matched, staring new process instance with trigger '{}'", trigger);
                metrics.messageConsumed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
                try {
                    ProcessInstance<$Type$> pi = process.createInstance(correlation, model);
                    pi.start(trigger, null, eventData);
                } catch (ProcessInstanceDuplicatedException e) {
                    ProcessInstance<$Type$> pi = process.instances().findById(correlation).get();
                    pi.send(Sig.of(trigger, eventData));
                }
            } else {
                metrics.messageMissed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
                LOGGER.warn("Received message without reference id and no correlation is set/matched, for trigger not capable of starting new instance '{}'", trigger);
            }
            return null;
        });
        return msg.ack();
    } catch (Exception e) {
        metrics.messageFailed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
        LOGGER.error("Error when consuming message for process {}", process.id(), e);
        return msg.nack(e);
    }
}
Also used : ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) TrustedIdentityProvider(io.automatiko.engine.api.auth.TrustedIdentityProvider) Collection(java.util.Collection) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance)

Example 15 with ProcessInstanceDuplicatedException

use of io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException in project automatiko-engine by automatiko-io.

the class $Type$MessageConsumer method consume.

public CompletionStage<Void> consume(Message<?> msg) {
    metrics.messageReceived(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
    final String trigger = "$Trigger$";
    try {
        io.smallrye.reactive.messaging.kafka.KafkaRecord<?, ?> record = (io.smallrye.reactive.messaging.kafka.KafkaRecord<?, ?>) msg;
        final $DataType$ eventData;
        final $Type$ model;
        final String correlation;
        LOGGER.debug("Received message with key '{}' and payload '{}'", record.getKey(), msg.getPayload());
        boolean accepted;
        if (useCloudEvents.orElse(false)) {
            $DataEventType$ event;
            String contentType = header(record, "content-type");
            model = new $Type$();
            if (contentType != null && contentType.startsWith("application/cloudevents+json")) {
                // structured
                event = json.readValue(record.getPayload().toString(), $DataEventType$.class);
                eventData = event.getData();
            } else {
                // binary
                eventData = convert(record, $DataType$.class);
                event = new $DataEventType$(header(record, "ce_specversion"), header(record, "ce_id"), header(record, "ce_source"), header(record, "ce_type"), header(record, "ce_subject"), header(record, "ce_time"), eventData);
                cloudEventsExtensions(msg, event);
            }
            correlation = correlation(event, msg);
            accepted = acceptedEvent(event, msg);
        } else {
            eventData = convert(msg, $DataType$.class);
            model = new $Type$();
            correlation = correlation(eventData, msg);
            accepted = acceptedPayload(eventData, msg);
        }
        if (!accepted) {
            metrics.messageRejected(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
            LOGGER.debug("Message has been rejected by filter expression");
            return msg.ack();
        }
        IdentityProvider.set(new TrustedIdentityProvider("System<messaging>"));
        io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
            if (correlation != null) {
                LOGGER.debug("Correlation ({}) is set, attempting to find if there is matching instance already active", correlation);
                Collection possiblyFound = process.instances().findByIdOrTag(io.automatiko.engine.api.workflow.ProcessInstanceReadMode.MUTABLE, correlation);
                if (!possiblyFound.isEmpty()) {
                    metrics.messageConsumed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
                    possiblyFound.forEach(pi -> {
                        ProcessInstance pInstance = (ProcessInstance) pi;
                        LOGGER.debug("Found process instance {} matching correlation {}, signaling instead of starting new instance", pInstance.id(), correlation);
                        pInstance.send(Sig.of(canStartInstance() ? trigger : "Message-" + trigger, eventData));
                    });
                    return null;
                }
            }
            if (canStartInstance()) {
                LOGGER.debug("Received message without reference id and no correlation is set/matched, staring new process instance with trigger '{}'", trigger);
                metrics.messageConsumed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
                try {
                    ProcessInstance<$Type$> pi = process.createInstance(correlation, model);
                    pi.start(trigger, null, eventData);
                } catch (ProcessInstanceDuplicatedException e) {
                    ProcessInstance<$Type$> pi = process.instances().findById(correlation).get();
                    pi.send(Sig.of(trigger, eventData));
                }
            } else {
                metrics.messageMissed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
                LOGGER.warn("Received message without reference id and no correlation is set/matched, for trigger not capable of starting new instance '{}'", trigger);
            }
            return null;
        });
        return msg.ack();
    } catch (Exception e) {
        metrics.messageFailed(CONNECTOR, MESSAGE, ((io.automatiko.engine.workflow.AbstractProcess<?>) process).process());
        LOGGER.error("Error when consuming message for process {}", process.id(), e);
        return msg.nack(e);
    }
}
Also used : ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) TrustedIdentityProvider(io.automatiko.engine.api.auth.TrustedIdentityProvider) Collection(java.util.Collection) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance)

Aggregations

ProcessInstanceDuplicatedException (io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException)16 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)9 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)9 ExportedProcessInstance (io.automatiko.engine.api.workflow.ExportedProcessInstance)6 TrustedIdentityProvider (io.automatiko.engine.api.auth.TrustedIdentityProvider)3 LinkedHashSet (java.util.LinkedHashSet)3 Model (io.automatiko.engine.api.Model)2 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Document (org.bson.Document)2 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 QueryExecutionException (com.datastax.oss.driver.api.core.servererrors.QueryExecutionException)1 Insert (com.datastax.oss.driver.api.querybuilder.insert.Insert)1 ConflictingVersionException (io.automatiko.engine.api.workflow.ConflictingVersionException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1