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