use of cz.metacentrum.perun.dispatcher.scheduling.impl.TaskScheduled in project perun by CESNET.
the class TaskScheduler method run.
// ----- methods -------------------------------------
/**
* This method runs in separate thread perpetually trying to take tasks from delay queue, blocking if none are available.
* If there is Task ready, we check if it source was updated. If it was, we put the task back to the queue (This
* can happen only limited number of times). If on the other hand it was not updated we perform additional checks using
* method sendToEngine.
*/
@Override
public void run() {
try {
initPerunSession();
} catch (InternalErrorException e1) {
String message = "Dispatcher was unable to initialize Perun session.";
log.error(message, e1);
throw new RuntimeException(message, e1);
}
log.debug("Pool contains {} tasks in total", schedulingPool.getSize());
TaskSchedule schedule;
while (!shouldStop()) {
try {
if (tasksManagerBl.isSuspendedTasksPropagation()) {
// do not continue sending tasks to the engine until propagation is set to resume
waitForResumingPropagation();
}
schedule = getWaitingTaskSchedule();
} catch (InterruptedException e) {
String message = "Thread was interrupted, cannot continue.";
log.error(message, e);
throw new RuntimeException(message, e);
}
Task task = schedule.getTask();
if (task.isSourceUpdated() && schedule.getDelayCount() > 0 && !task.isPropagationForced()) {
// source data changed before sending, wait for more changes to come -> reschedule
log.warn("[{}] Task was not allowed to be sent to Engine now: {}.", task.getId(), task);
schedulingPool.scheduleTask(task, schedule.getDelayCount() - 1);
} else {
// send it to engine
TaskScheduled reason = sendToEngine(task);
switch(reason) {
case QUEUE_ERROR:
log.warn("[{}] Task dispatcherQueue could not be set, so it is rescheduled: {}.", task.getId(), task);
schedulingPool.scheduleTask(task, -1);
break;
case DENIED:
// Task is lost from waiting queue, since somebody blocked service on facility, all destinations or globally
log.info("[{}] Execution was denied for Task before sending to Engine: {}.", task.getId(), task);
break;
case ERROR:
log.error("[{}] Unexpected error when scheduling Task, so it is rescheduled: {}.", task.getId(), task);
schedulingPool.scheduleTask(task, -1);
break;
case SUCCESS:
log.info("[{}] Task was successfully sent to Engine: {}.", task.getId(), task);
break;
case DB_ERROR:
// Task is lost from waiting queue, will be cleared from pool by propagation maintainer
log.warn("[{}] Facility, Service or Destination could not be found in DB for Task {}.", task.getId(), task);
break;
}
// update task status in DB
tasksManagerBl.updateTask(perunSession, task);
}
}
log.debug("TaskScheduler has stopped.");
}
Aggregations