public String toString() {
return name;
}
public void run() {
Task task = null;
try {
while (!Thread.currentThread().isInterrupted()) {
task = inQueue.take();
try {
// :TODO: NEXT:
// If power fails, tasks can go MIA here.
// We should maintain a global map of tasks, check it periodically, and restart tasks that went MIA.
// Task update their status there, and clients could check the status using a GET /run/... call.
task = handler.handle(task);
}
catch (final Fail e) {
log.warn(String.format("%s %s: failed with message '%s'", name, task, e.getMessage()));
if (task.failures().size() >= NUM_TRIES) {
log.error(String.format("%s %s: Error details:", name, task), e);
log.error(String.format("%s %s: Retries exhausted. Failing.", name, task));
failQueue.put(task);
}
else {
log.warn(String.format("%s %s: recording failure & requeuing...", name, task));
inQueue.put(task.fail(e.getMessage()));
}
continue;
}
catch (final Exception e) {
log.error(String.format("%s %s: Exception while handling.", name, task));
log.error(String.format("%s %s: Error details:", name, task), e);
failQueue.put(task.fail(e.getMessage()));
continue;
}
if (outQueue != null) outQueue.put(task);
task = null;