Search in sources :

Example 1 with WorkerType

use of net.petafuel.styx.keepalive.entities.WorkerType in project styx by petafuel.

the class TaskRecoveryDB method getQueuedTasks.

/**
 * Returns all persisted tasks that were previously marked as queued/waiting for execution and were not yet executed
 * by a Worker Thread within the previous application runtime
 *
 * @return Returns a map of K: Task Object | V: Assigned Worker Type
 */
public static Map<WorkableTask, WorkerType> getQueuedTasks() {
    Connection connection = Persistence.getInstance().getConnection();
    Map<WorkableTask, WorkerType> queuedTasks = new LinkedHashMap<>();
    try (CallableStatement query = connection.prepareCall("{call get_queued_tasks()}")) {
        try (ResultSet resultSet = query.executeQuery()) {
            if (!resultSet.isBeforeFirst()) {
                return queuedTasks;
            }
            queuedTasks = TaskRecoveryFactory.modelFromDatabase(resultSet);
        }
    } catch (SQLException e) {
        LOG.error("Unable to load queued tasks: SQLState: {} Error: {}", e.getSQLState(), e.getMessage());
    }
    return queuedTasks;
}
Also used : WorkerType(net.petafuel.styx.keepalive.entities.WorkerType) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) WorkableTask(net.petafuel.styx.keepalive.contracts.WorkableTask) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with WorkerType

use of net.petafuel.styx.keepalive.entities.WorkerType in project styx by petafuel.

the class TaskRecoveryFactory method modelFromDatabase.

static Map<WorkableTask, WorkerType> modelFromDatabase(ResultSet resultSet) throws SQLException {
    LinkedHashMap<WorkableTask, WorkerType> tasks = new LinkedHashMap<>();
    while (resultSet.next()) {
        UUID id = UUID.fromString(resultSet.getString("id"));
        String goal = resultSet.getString("goal");
        String classname = resultSet.getString("class");
        WorkerType type = WorkerType.valueOf(resultSet.getString("worker_type"));
        WorkableTask recoveredTask;
        try (Jsonb jsonb = JsonbBuilder.create()) {
            recoveredTask = TaskRecoveryFactory.factory(classname, jsonb.fromJson(goal, JsonObject.class));
            TaskRecoveryDB.setFinallyFailed(id, "Task was recovered and re-queued as new Task", TaskFinalFailureCode.RECOVERED_AND_QUEUED);
            tasks.put(recoveredTask, type);
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            LOG.error("Unable to recover Task {}: {}", id, e.getMessage());
        } catch (NoSuchMethodException unavailableConstructor) {
            LOG.error("Unable to recover Task {}: Seems as if there is no empty public constructor available for reflection -> {}", id, unavailableConstructor.getMessage());
        } catch (Exception unknown) {
            TaskRecoveryDB.setFinallyFailed(id, "Unable to recover Task: " + unknown.getMessage(), TaskFinalFailureCode.UNABLE_TO_RECOVER);
            LOG.error("Unable to recover Task {} due to an unexpected exception: {}", id, unknown.getMessage());
        }
    }
    return tasks;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) LinkedHashMap(java.util.LinkedHashMap) WorkerType(net.petafuel.styx.keepalive.entities.WorkerType) Jsonb(javax.json.bind.Jsonb) UUID(java.util.UUID) WorkableTask(net.petafuel.styx.keepalive.contracts.WorkableTask)

Example 3 with WorkerType

use of net.petafuel.styx.keepalive.entities.WorkerType in project styx by petafuel.

the class TaskRecoveryDB method getInterruptedTasks.

/**
 * Returns all persisted tasks that were previously marked as running/executing and were unable to be resolved
 * within the previous application runtime
 *
 * @return Returns a map of K: Task Object | V: Assigned Worker Type
 */
public static Map<WorkableTask, WorkerType> getInterruptedTasks() {
    Connection connection = Persistence.getInstance().getConnection();
    Map<WorkableTask, WorkerType> interruptedTasks = new LinkedHashMap<>();
    try (CallableStatement query = connection.prepareCall("{call get_interrupted_tasks()}")) {
        try (ResultSet resultSet = query.executeQuery()) {
            if (!resultSet.isBeforeFirst()) {
                return interruptedTasks;
            }
            interruptedTasks = TaskRecoveryFactory.modelFromDatabase(resultSet);
        }
    } catch (SQLException e) {
        LOG.error("Unable to load interrupted tasks: SQLState: {} Error: {}", e.getSQLState(), e.getMessage());
    }
    return interruptedTasks;
}
Also used : WorkerType(net.petafuel.styx.keepalive.entities.WorkerType) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) WorkableTask(net.petafuel.styx.keepalive.contracts.WorkableTask) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SQLException (java.sql.SQLException)3 LinkedHashMap (java.util.LinkedHashMap)3 WorkableTask (net.petafuel.styx.keepalive.contracts.WorkableTask)3 WorkerType (net.petafuel.styx.keepalive.entities.WorkerType)3 CallableStatement (java.sql.CallableStatement)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 UUID (java.util.UUID)1 Jsonb (javax.json.bind.Jsonb)1