Search in sources :

Example 31 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class PostgresBaseDAO method getWithTransaction.

/**
 * Initialize a new transactional {@link Connection} from {@link #dataSource} and pass it to {@literal function}.
 * <p>
 * Successful executions of {@literal function} will result in a commit and return of
 * {@link TransactionalFunction#apply(Connection)}.
 * <p>
 * If any {@link Throwable} thrown from {@code TransactionalFunction#apply(Connection)} will result in a rollback
 * of the transaction
 * and will be wrapped in an {@link ApplicationException} if it is not already one.
 * <p>
 * Generally this is used to wrap multiple {@link #execute(Connection, String, ExecuteFunction)} or
 * {@link #query(Connection, String, QueryFunction)} invocations that produce some expected return value.
 *
 * @param function The function to apply with a new transactional {@link Connection}
 * @param <R>      The return type.
 * @return The result of {@code TransactionalFunction#apply(Connection)}
 * @throws ApplicationException If any errors occur.
 */
private <R> R getWithTransaction(final TransactionalFunction<R> function) {
    final Instant start = Instant.now();
    LazyToString callingMethod = getCallingMethod();
    logger.trace("{} : starting transaction", callingMethod);
    try (Connection tx = dataSource.getConnection()) {
        boolean previousAutoCommitMode = tx.getAutoCommit();
        tx.setAutoCommit(false);
        try {
            R result = function.apply(tx);
            tx.commit();
            return result;
        } catch (Throwable th) {
            tx.rollback();
            if (th instanceof ApplicationException) {
                throw th;
            }
            throw new ApplicationException(BACKEND_ERROR, th.getMessage(), th);
        } finally {
            tx.setAutoCommit(previousAutoCommitMode);
        }
    } catch (SQLException ex) {
        throw new ApplicationException(BACKEND_ERROR, ex.getMessage(), ex);
    } finally {
        logger.trace("{} : took {}ms", callingMethod, Duration.between(start, Instant.now()).toMillis());
    }
}
Also used : BACKEND_ERROR(com.netflix.conductor.core.execution.ApplicationException.Code.BACKEND_ERROR) INTERNAL_ERROR(com.netflix.conductor.core.execution.ApplicationException.Code.INTERNAL_ERROR) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SQLException(java.sql.SQLException) Instant(java.time.Instant) Connection(java.sql.Connection)

Example 32 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class PostgresBaseDAO method getWithTransactionWithOutErrorPropagation.

protected <R> R getWithTransactionWithOutErrorPropagation(TransactionalFunction<R> function) {
    Instant start = Instant.now();
    LazyToString callingMethod = getCallingMethod();
    logger.trace("{} : starting transaction", callingMethod);
    try (Connection tx = dataSource.getConnection()) {
        boolean previousAutoCommitMode = tx.getAutoCommit();
        tx.setAutoCommit(false);
        try {
            R result = function.apply(tx);
            tx.commit();
            return result;
        } catch (Throwable th) {
            tx.rollback();
            logger.info(CONFLICT + " " + th.getMessage());
            return null;
        } finally {
            tx.setAutoCommit(previousAutoCommitMode);
        }
    } catch (SQLException ex) {
        throw new ApplicationException(BACKEND_ERROR, ex.getMessage(), ex);
    } finally {
        logger.trace("{} : took {}ms", callingMethod, Duration.between(start, Instant.now()).toMillis());
    }
}
Also used : BACKEND_ERROR(com.netflix.conductor.core.execution.ApplicationException.Code.BACKEND_ERROR) INTERNAL_ERROR(com.netflix.conductor.core.execution.ApplicationException.Code.INTERNAL_ERROR) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SQLException(java.sql.SQLException) Instant(java.time.Instant) Connection(java.sql.Connection)

Example 33 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class PostgresExecutionDAO method getEventExecutions.

public List<EventExecution> getEventExecutions(String eventHandlerName, String eventName, String messageId, int max) {
    try {
        List<EventExecution> executions = Lists.newLinkedList();
        withTransaction(tx -> {
            for (int i = 0; i < max; i++) {
                // see SimpleEventProcessor.handle to understand how the
                String executionId = messageId + "_" + i;
                // execution id is set
                EventExecution ee = readEventExecution(tx, eventHandlerName, eventName, messageId, executionId);
                if (ee == null) {
                    break;
                }
                executions.add(ee);
            }
        });
        return executions;
    } catch (Exception e) {
        String message = String.format("Unable to get event executions for eventHandlerName=%s, eventName=%s, messageId=%s", eventHandlerName, eventName, messageId);
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, message, e);
    }
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) SQLException(java.sql.SQLException) ApplicationException(com.netflix.conductor.core.execution.ApplicationException)

Example 34 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class MySQLExecutionDAO method getAllPollData.

@Override
public List<PollData> getAllPollData() {
    try (Connection tx = dataSource.getConnection()) {
        boolean previousAutoCommitMode = tx.getAutoCommit();
        tx.setAutoCommit(true);
        try {
            String GET_ALL_POLL_DATA = "SELECT json_data FROM poll_data ORDER BY queue_name";
            return query(tx, GET_ALL_POLL_DATA, q -> q.executeAndFetch(PollData.class));
        } catch (Throwable th) {
            throw new ApplicationException(BACKEND_ERROR, th.getMessage(), th);
        } finally {
            tx.setAutoCommit(previousAutoCommitMode);
        }
    } catch (SQLException ex) {
        throw new ApplicationException(BACKEND_ERROR, ex.getMessage(), ex);
    }
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SQLException(java.sql.SQLException) PollData(com.netflix.conductor.common.metadata.tasks.PollData) Connection(java.sql.Connection)

Example 35 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class MySQLExecutionDAO method getEventExecutions.

public List<EventExecution> getEventExecutions(String eventHandlerName, String eventName, String messageId, int max) {
    try {
        List<EventExecution> executions = Lists.newLinkedList();
        withTransaction(tx -> {
            for (int i = 0; i < max; i++) {
                // see SimpleEventProcessor.handle to understand how the
                String executionId = messageId + "_" + i;
                // execution id is set
                EventExecution ee = readEventExecution(tx, eventHandlerName, eventName, messageId, executionId);
                if (ee == null) {
                    break;
                }
                executions.add(ee);
            }
        });
        return executions;
    } catch (Exception e) {
        String message = String.format("Unable to get event executions for eventHandlerName=%s, eventName=%s, messageId=%s", eventHandlerName, eventName, messageId);
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, message, e);
    }
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) SQLException(java.sql.SQLException) ApplicationException(com.netflix.conductor.core.execution.ApplicationException)

Aggregations

ApplicationException (com.netflix.conductor.core.execution.ApplicationException)93 Task (com.netflix.conductor.common.metadata.tasks.Task)22 Workflow (com.netflix.conductor.common.run.Workflow)22 HashMap (java.util.HashMap)19 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)16 IOException (java.io.IOException)16 Map (java.util.Map)15 ArrayList (java.util.ArrayList)14 List (java.util.List)14 Inject (javax.inject.Inject)14 Singleton (javax.inject.Singleton)14 Logger (org.slf4j.Logger)14 LoggerFactory (org.slf4j.LoggerFactory)14 Trace (com.netflix.conductor.annotations.Trace)13 EventExecution (com.netflix.conductor.common.metadata.events.EventExecution)13 Monitors (com.netflix.conductor.metrics.Monitors)13 Collectors (java.util.stream.Collectors)13 ResultSet (com.datastax.driver.core.ResultSet)12 EventHandler (com.netflix.conductor.common.metadata.events.EventHandler)12 TimeUnit (java.util.concurrent.TimeUnit)12