Search in sources :

Example 1 with WorkableTask

use of net.petafuel.styx.keepalive.contracts.WorkableTask 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 WorkableTask

use of net.petafuel.styx.keepalive.contracts.WorkableTask 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 WorkableTask

use of net.petafuel.styx.keepalive.contracts.WorkableTask 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)

Example 4 with WorkableTask

use of net.petafuel.styx.keepalive.contracts.WorkableTask in project styx by petafuel.

the class CoreWorker method run.

// try-catch required for error handling
@SuppressWarnings("java:S3776")
@Override
public void run() {
    Thread.currentThread().setName("KeepAlive-Worker-" + getType().toString() + "-" + getId().toString());
    LOG.info("Started CoreWorker id: {}", this.getId());
    setRunning(true);
    while (running.get()) {
        if (ThreadManager.getInstance().getCoreQueue().isEmpty()) {
            LOG.info("No polling from queue: Queue is empty -> ideling/waiting");
            try {
                synchronized (ThreadManager.getInstance().getCoreQueue()) {
                    ThreadManager.getInstance().getCoreQueue().wait();
                }
            } catch (InterruptedException e) {
                LOG.error("CoreWorker {} was interrupted", this.getId());
                Thread.currentThread().interrupt();
            }
        }
        WorkableTask task;
        synchronized (ThreadManager.getInstance().getCoreQueue()) {
            task = ThreadManager.getInstance().getCoreQueue().poll();
        }
        if (task == null) {
            continue;
        }
        currentTask.set(task);
        LOG.info("Task id:{} signature:{} polled from queue", task.getId(), task.getSignature());
        try {
            TaskRecoveryDB.updateState(task.getId(), TaskState.RUNNING);
            currentTaskStartTime.set(new Date().getTime());
            task.execute();
            LOG.info("Task id:{} signature: {} finished successfully", task.getId(), task.getSignature());
            currentTaskStartTime.set(0);
            TaskRecoveryDB.updateState(task.getId(), TaskState.DONE);
        } catch (TaskRetryFailureException retryFailure) {
            LOG.warn("Task id: {} signature: {} failed but will be requeued as RETRY_FAILURE -> {}", task.getId(), task.getSignature(), retryFailure.getMessage());
            TaskRecoveryDB.updateWorker(task.getId(), WorkerType.RETRY_FAILURE);
            ThreadManager.getInstance().queueTask(task, WorkerType.RETRY_FAILURE);
        } catch (TaskFinalFailureException finalFailure) {
            LOG.error("Task id: {} signature: {} finally failed with code:{} -> {}", task.getId(), task.getSignature(), finalFailure.getCode(), finalFailure.getMessage());
            TaskRecoveryDB.setFinallyFailed(task.getId(), finalFailure.getMessage(), finalFailure.getCode());
        } catch (Throwable throwable) {
            LOG.error("Task id: {} signature: {} encountered an unexpected error", task.getId(), task.getSignature(), throwable);
            TaskRecoveryDB.setFinallyFailed(task.getId(), throwable.getMessage(), TaskFinalFailureCode.UNKNOWN);
        }
    }
    LOG.info("Terminated CoreWorker id: {}", this.getId());
}
Also used : TaskFinalFailureException(net.petafuel.styx.keepalive.entities.TaskFinalFailureException) TaskRetryFailureException(net.petafuel.styx.keepalive.entities.TaskRetryFailureException) WorkableTask(net.petafuel.styx.keepalive.contracts.WorkableTask) Date(java.util.Date)

Example 5 with WorkableTask

use of net.petafuel.styx.keepalive.contracts.WorkableTask in project styx by petafuel.

the class TaskRecoveryFactory method factory.

// the full qualified classname is build by the Factory itself and only takes "input" from the database as trusted source
@SuppressWarnings("squid:S1523")
private static WorkableTask factory(String classname, JsonObject jsonGoal) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
    Class<?> clazz = Class.forName(classname);
    WorkableTask recoveredTask = (WorkableTask) clazz.getDeclaredConstructor().newInstance();
    recoveredTask = recoveredTask.buildFromRecovery(jsonGoal);
    return recoveredTask;
}
Also used : WorkableTask(net.petafuel.styx.keepalive.contracts.WorkableTask)

Aggregations

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