use of org.bf2.srs.fleetmanager.execution.manager.impl.RetryExecutionControlException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class JobWrapper method execute.
@Override
@SneakyThrows
@ActivateRequestContext
public void execute(JobExecutionContext quartzJobContext) {
Task task = loadTask(quartzJobContext);
List<Worker> selectedWorkers = workers.stream().filter(w -> w.supports(task) && !workerExclusions.contains(w.getClass())).collect(toList());
for (Worker worker : selectedWorkers) {
WorkerContextImpl wCtx = loadWorkerContext(quartzJobContext, worker, task);
Instant next = null;
Exception lastException = null;
try {
log.debug("Task Manager (task = {}, worker = {}, workerContext = {}): Executing task.", task, worker, wCtx);
worker.execute(task, wCtx);
wCtx.getDelayedActions().forEach(Runnable::run);
// OK vvv
// Reset retry counter
wCtx.setRetryAttempts(0);
// Reset min retry counter
wCtx.setMinRetries(task.getSchedule().getMinRetries());
// Normal rescheduling
next = nextExecution(task);
} catch (Exception anEx) {
// TODO Throwable?
lastException = anEx;
if (anEx instanceof RetryExecutionControlException) {
log.debug("Task Manager (task = {}, worker = {}, workerContext = {}): Task requested a retry.", task, worker, wCtx, anEx);
RetryExecutionControlException ex = (RetryExecutionControlException) anEx;
if (ex.isForce() && wCtx.getMinRetries() < Integer.MAX_VALUE) {
// Make space for forced retry, no more than Integer.MAX_VALUE
wCtx.setMinRetries(wCtx.getMinRetries() + 1);
next = Instant.now().plus(Duration.ofSeconds(1));
}
if (ex.getMinRetries() > wCtx.getMinRetries()) {
wCtx.setMinRetries(ex.getMinRetries());
}
lastException = null;
}
if (wCtx.getRetryAttempts() < wCtx.getMinRetries() && (next == null)) {
// Reschedule if the minRetries is not reached
next = Instant.now().plus(backoff(wCtx.getRetryAttempts()));
}
if (anEx instanceof StopExecutionControlException) {
log.debug("Task Manager (task = {}, worker = {}, workerContext = {}): Task requested a stop.", task, worker, wCtx, anEx);
// Unschedule
next = null;
lastException = null;
}
if (lastException != null) {
log.warn("Task Manager (task = {}, worker = {}, workerContext = {}, nextExecution = {}): Task threw an exception during execution: {}", task, worker, wCtx, next, anEx);
}
wCtx.setRetryAttempts(wCtx.getRetryAttempts() + 1);
} finally {
// Unlikely used
wCtx.setDelayedActions(new ArrayList<>(0));
saveWorkerContext(quartzJobContext, wCtx, worker);
saveTask(quartzJobContext, task);
// Scheduling
if (next != null) {
if (wCtx.getRetryAttempts() == wCtx.getMinRetries()) {
log.info("Task Manager (task = {}, worker = {}, workerContext = {}): Last rescheduling at {}.", task, worker, wCtx, next);
} else {
log.debug("Task Manager (task = {}, worker = {}, workerContext = {}): Rescheduling task at {}.", task, worker, wCtx, next);
}
taskManager.rerigger(task, next);
} else {
try {
log.debug("Task Manager (task = {}, worker = {}, workerContext = {}): Executing finallyExecute. Last exception = {}", task, worker, wCtx, lastException);
worker.finallyExecute(task, wCtx, ofNullable(lastException));
wCtx.getDelayedActions().forEach(Runnable::run);
} catch (Exception ex) {
log.warn("Task Manager (task = {}, worker = {}, workerContext = {}): Ignoring an exception thrown in finallyExecute: {}", task, worker, wCtx, ex);
} finally {
log.debug("Task Manager (task = {}, worker = {}, workerContext = {}): Removing task.", task, worker, wCtx);
taskManager.remove(task);
}
}
}
}
}
use of org.bf2.srs.fleetmanager.execution.manager.impl.RetryExecutionControlException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class DataCollectingWorker method execute.
@Override
public void execute(Task aTask, WorkerContext ctl) {
boolean finished = false;
try {
TestTask task = (TestTask) aTask;
Command command = task.getCommands().peekFirst();
if (command != null && command.done()) {
task.getCommands().removeFirst();
command = task.getCommands().peekFirst();
}
if (command != null) {
command.execute(ctl, task);
} else {
// Do not record stop caused by end of commands
finished = true;
ctl.stop();
}
data.recordSuccess();
} catch (RetryExecutionControlException ex) {
if (ex.isForce())
data.recordForceRetry();
else
data.recordRetry();
throw ex;
} catch (StopExecutionControlException ex) {
if (!finished)
data.recordStop();
throw ex;
} catch (Exception ex) {
data.recordException();
throw ex;
} finally {
if (!finished)
data.recordExecution();
}
}
Aggregations