use of cz.metacentrum.perun.taskslib.model.Task in project perun by CESNET.
the class PropagationMaintainerImpl method onTaskComplete.
@Override
public void onTaskComplete(int taskId, int clientID, String status_s, String endTimestamp, String string) {
Task completedTask = schedulingPool.getTaskById(taskId);
if (completedTask == null) {
// eh? how would that be possible?
log.error("TASK id {} reported as complete, but we do not know it... (yet?)", taskId);
return;
}
TaskStatus status = TaskStatus.NONE;
if (status_s.equals("ERROR")) {
status = TaskStatus.ERROR;
} else if (status_s.equals("DONE")) {
status = TaskStatus.DONE;
} else {
log.error("Engine reported unexpected status {} for task id {}, setting to ERROR", status_s, taskId);
status = TaskStatus.ERROR;
}
try {
Date endTime = new Date(Long.parseLong(endTimestamp));
completedTask.setEndTime(endTime);
} catch (NumberFormatException e) {
log.error("Engine reported unparsable end time {} for task id {}, setting to current time", endTimestamp, taskId);
completedTask.setEndTime(new Date(System.currentTimeMillis()));
}
// date data
if (completedTask.getExecService().getExecServiceType().equals(ExecServiceType.SEND)) {
try {
schedulingPool.setQueueForTask(completedTask, null);
} catch (InternalErrorException e) {
log.error("Could not set destination queue for task {}: {}", completedTask.getId(), e.getMessage());
}
this.setAllGenerateDependenciesToNone(completedTask);
}
if (status.equals(TaskStatus.DONE)) {
// task completed successfully
// set destination list to null to refetch them later
completedTask.setDestinations(null);
schedulingPool.setTaskStatus(completedTask, TaskStatus.DONE);
completedTask.setRecurrence(0);
log.debug("TASK {} reported as DONE", completedTask.toString());
// for GEN tasks, signal SENDs that source data are updated
if (completedTask.getExecService().getExecServiceType().equals(ExecServiceType.GENERATE)) {
List<ExecService> dependantServices = dependenciesResolver.listDependantServices(completedTask.getExecService());
for (ExecService dependantService : dependantServices) {
Task dependantTask = schedulingPool.getTask(dependantService, completedTask.getFacility());
if (dependantTask != null && dependantService.getExecServiceType().equals(ExecServiceType.SEND)) {
dependantTask.setSourceUpdated(false);
}
if (completedTask.isPropagationForced() && dependantTask.isPropagationForced()) {
log.debug("Going to force schedule dependant task " + dependantTask.getId());
taskScheduler.scheduleTask(dependantTask);
}
}
}
completedTask.setPropagationForced(false);
} else {
if (string.isEmpty()) {
// weird - task is in error and no destinations reported as
// failed...
log.warn("TASK {} ended in ERROR state with no remaining destinations.", completedTask.toString());
} else {
// task failed, some destinations remain
// resolve list of destinations
List<PerunBean> listOfBeans;
List<Destination> destinationList = new ArrayList<Destination>();
try {
listOfBeans = AuditParser.parseLog(string);
log.debug("Found list of destination beans: " + listOfBeans);
for (PerunBean bean : listOfBeans) {
destinationList.add((Destination) bean);
}
} catch (InternalErrorException e) {
log.error("Could not resolve destination from destination list");
}
if (completedTask.getDestinations() != null && !completedTask.getDestinations().isEmpty()) {
completedTask.setDestinations(destinationList);
}
}
schedulingPool.setTaskStatus(completedTask, TaskStatus.ERROR);
completedTask.setPropagationForced(false);
log.debug("Task set to ERROR state with remaining destinations: " + completedTask.getDestinations());
}
}
use of cz.metacentrum.perun.taskslib.model.Task in project perun by CESNET.
the class PropagationMaintainerImpl method endStuckTasks.
private void endStuckTasks() {
// list all tasks in processing and planned and check if any have beeen
// running for too long.
log.info("I am gonna list planned and processing tasks and kill them if necessary...");
List<Task> suspiciousTasks = schedulingPool.getProcessingTasks();
suspiciousTasks.addAll(schedulingPool.getPlannedTasks());
for (Task task : suspiciousTasks) {
// count how many minutes the task stays in one state - if the state
// is PLANNED count it from when it was scheduled ; if it is
// PROCESSING count it from when it started
Date started = task.getStartTime();
Date scheduled = task.getSchedule();
TaskStatus status = task.getStatus();
if (status == null) {
log.error("ERROR: Task presumably in PLANNED or PROCESSING state, but does not have a valid status. Switching to ERROR. {}", task);
task.setEndTime(new Date(System.currentTimeMillis()));
schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
task.setPropagationForced(false);
continue;
}
if (started == null && scheduled == null) {
log.error("ERROR: Task presumably in PLANNED or PROCESSING state, but does not have a valid scheduled or started time. Switching to ERROR. {}", task);
task.setEndTime(new Date(System.currentTimeMillis()));
schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
task.setPropagationForced(false);
continue;
}
int howManyMinutesAgo = (int) (System.currentTimeMillis() - (started == null ? scheduled : started).getTime()) / 1000 / 60;
// If too much time has passed something is broken
if (howManyMinutesAgo >= rescheduleTime) {
log.error("ERROR: Task is stuck in PLANNED or PROCESSING state. Switching it to ERROR. {}", task);
task.setEndTime(new Date(System.currentTimeMillis()));
schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
task.setPropagationForced(false);
}
}
/*
*
* List<Task> suspiciousTasks =
* taskManager.listAllTasksInState(TaskStatus.PROCESSING,
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id")));
* suspiciousTasks
* .addAll(taskManager.listAllTasksInState(TaskStatus.PLANNED,
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id"))));
* for (Task task : suspiciousTasks) { //count how many minutes the task
* stays in one state - if the state is PLANNED count it from when it
* was scheduled ; if it is PROCESSING count it from when it started int
* howManyMinutesAgo = (int) (System.currentTimeMillis() - (
* task.getStatus().equals(TaskStatus.PLANNED) ? task.getSchedule() :
* task.getStartTime() ).getTime()) / 1000 / 60;
*
* //If too much time has passed something is broken if
* (howManyMinutesAgo >= 60) { log.error(
* "ERROR: Task is stuck in PLANNED or PROCESSING state. Switching it to ERROR. {}"
* , task); task.setEndTime(new Date(System.currentTimeMillis()));
* task.setStatus(TaskStatus.ERROR); taskManager.updateTask(task,
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id"))); }
* }
*/
}
use of cz.metacentrum.perun.taskslib.model.Task in project perun by CESNET.
the class PropagationMaintainerImpl method rescheduleOldDoneTasks.
private void rescheduleOldDoneTasks() {
// Reschedule SEND tasks in DONE that haven't been running for quite a
// while
log.info("I am gonna list complete tasks and reschedule if they are too old...");
for (Task task : schedulingPool.getDoneTasks()) {
// skip GEN tasks
if (task.getExecService() != null && task.getExecService().getExecServiceType().equals(ExecService.ExecServiceType.GENERATE)) {
log.debug("Found finished GEN TASK {} that was not running for a while, leaving it as is.", task.toString());
continue;
}
Date twoDaysAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 2);
if (task.isSourceUpdated()) {
// reschedule the task
log.info("TASK [" + task + "] data changed. Going to schedule for propagation now.");
taskScheduler.scheduleTask(task);
} else if (task.getEndTime() == null || task.getEndTime().before(twoDaysAgo)) {
// reschedule the task
log.info("TASK [" + task + "] wasn't propagated for more then 2 days. Going to schedule it for propagation now.");
taskScheduler.scheduleTask(task);
} else {
log.info("TASK [" + task + "] has finished recently, leaving it for now.");
}
}
/*
*
* for(Task task : taskManager.listAllTasksInState(TaskStatus.DONE,
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id")))) {
* //skip GEN tasks
* if(task.getExecService().getExecServiceType().equals(
* ExecService.ExecServiceType.GENERATE)) continue;
*
* Date twoDaysAgo = new Date(System.currentTimeMillis() - 1000 * 60 *
* 24 * 2); if(task.getEndTime().before(twoDaysAgo)) { //reschedule the
* task try { taskScheduler.propagateService(task.getExecService(), new
* Date(System.currentTimeMillis()), task.getFacility());
* log.info("TASK [" + task +
* "] wasn't propagated for more then 2 days. Going to schedule it for propagation now."
* ); } catch (InternalErrorException e) { log.error(
* "Rescheduling of task which wasn't propagated for more than 2 days failed. {}, Exception: {}"
* , task, e); } }
*
* }
*/
}
use of cz.metacentrum.perun.taskslib.model.Task in project perun by CESNET.
the class SchedulingPoolTest method getTaskByIdTest.
@IfProfileValue(name = "perun.test.groups", values = ("unit-tests"))
@Test
public void getTaskByIdTest() {
System.out.println("SchedulingPool.getTaskById()");
Task task = schedulingPool.getTaskById(task1.getId());
Assert.isTrue(task == task1);
}
use of cz.metacentrum.perun.taskslib.model.Task in project perun by CESNET.
the class PropagationMaintainerImpl method rescheduleErrorTasks.
@Deprecated
private void rescheduleErrorTasks() {
for (Task task : schedulingPool.getErrorTasks()) {
log.debug("TASK " + task.toString() + " finished in error");
if (task.getEndTime() == null) {
log.error("RECOVERY FROM INCONSISTENT STATE: ERROR task does not have end_time! Setting end_time to task.getDelay + 1.");
// getDelay is in minutes, therefore we multiply it with 60*1000
Date endTime = new Date(System.currentTimeMillis() - ((task.getDelay() + 1) * 60000));
task.setEndTime(endTime);
}
int howManyMinutesAgo = (int) (System.currentTimeMillis() - task.getEndTime().getTime()) / 1000 / 60;
log.info("TASK [" + task + "] in ERROR state completed " + howManyMinutesAgo + " minutes ago.");
// check and set recurrence
int recurrence = task.getRecurrence() - 1;
if (recurrence < 0) {
// no more retries, sorry
log.info("TASK [ " + task + "] in ERROR state has no more retries, bailing out.");
schedulingPool.removeTask(task);
log.debug("TASK {} removed from database.", task.getId());
continue;
}
// If DELAY time has passed, we reschedule...
if (howManyMinutesAgo >= task.getDelay()) {
try {
task.setRecurrence(recurrence);
ExecService execService = task.getExecService();
Facility facility = task.getFacility();
log.info("TASK [ " + task + "] in ERROR state is going to be rescheduled: taskScheduler.propagateService(execService:ID " + execService.getId() + ", new Date(System.currentTimeMillis()), facility:ID " + facility.getId() + ");");
taskScheduler.propagateService(task, new Date(System.currentTimeMillis()));
log.info("TASK [" + task + "] in ERROR state has been rescheduled.");
// Also (to be sure) reschedule all Tasks that depend on
// this Task
//
// While engine starts in state GEN = ERROR, SEND = DONE
// => GEN will be rescheduled but without this SEND will
// never be propagated
List<Task> dependentTasks = dependenciesResolver.getDependants(task);
if (dependentTasks != null) {
for (Task dependantTask : dependentTasks) {
taskScheduler.propagateService(dependantTask, new Date(System.currentTimeMillis()));
log.info("{} was rescheduled because it depends on {}", dependantTask, task);
}
}
} catch (InternalErrorException e) {
log.error("{}", e);
}
}
}
/*
* Original implementation:
*
* //TODO: Take into account Recurrence! for (Task task :
* taskManager.listAllTasksInState(TaskStatus.ERROR,
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id")))) {
* if (task.getEndTime() == null) { log.error(
* "RECOVERY FROM INCONSISTATE STATE: ERROR task does not have end_time! Setting end_time to task.getDelay + 1."
* ); // getDelay is in minutes, therefore we multiply it with 60*1000
* Date endTime = new Date(System.currentTimeMillis() -
* ((task.getDelay() + 1) * 60000)); task.setEndTime(endTime);
* taskManager.updateTask(task,
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id"))); }
* int howManyMinutesAgo = (int) (System.currentTimeMillis() -
* task.getEndTime().getTime()) / 1000 / 60; log.info("TASK [" + task +
* "] in ERROR state completed " + howManyMinutesAgo + " minutes ago.");
* //If DELAY time has passed, we reschedule... if (howManyMinutesAgo >=
* task.getDelay()) { //check if service is still assigned on facility
* try { List<Service> assignedServices =
* Rpc.ServicesManager.getAssignedServices(engineManager.getRpcCaller(),
* task.getFacility());
* if(assignedServices.contains(task.getExecService().getService())) {
* try { taskManager.updateTask(task,
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id")));
* ExecService execService = task.getExecService(); Facility facility =
* task.getFacility(); log.info("TASK [" + task +
* "] in ERROR state is going to be rescheduled: taskScheduler.propagateService(execService:ID "
* + execService.getId() +
* ", new Date(System.currentTimeMillis()), facility:ID " +
* facility.getId() + ");"); taskScheduler.propagateService(execService,
* new Date(System.currentTimeMillis()), facility); log.info("TASK [" +
* task + "] in ERROR state has been rescheduled.");
*
* //Also (to be sure) reschedule all execServices which depends on this
* exec service // //While engine starts in state GEN = ERROR, SEND =
* DONE => GEN will be rescheduled but without this SEND will never be
* propagated List<ExecService> dependentExecServices =
* Rpc.GeneralServiceManager
* .listExecServicesDependingOn(engineManager.getRpcCaller(),
* execService); if(dependentExecServices != null) { for(ExecService
* dependantExecService : dependentExecServices) {
* taskScheduler.propagateService(dependantExecService, new
* Date(System.currentTimeMillis()), facility);
* log.info("{} was rescheduled because it depends on {}",
* dependantExecService, execService); } }
*
* } catch (InternalErrorException e) { log.error(e.toString(), e); } }
* else { //delete this tasks (SEND and GEN) because service is no
* longer assigned to facility List<ExecService> execServicesGenAndSend
* =
* Rpc.GeneralServiceManager.listExecServices(engineManager.getRpcCaller
* (), task.getExecService().getService().getId()); for(ExecService
* execService : execServicesGenAndSend) { Task taskToDelete =
* taskManager.getTask(execService, task.getFacility(),
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id")));
* if(taskToDelete!= null) {
* resultManager.clearByTask(taskToDelete.getId(),
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id")));
* taskManager.removeTask(taskToDelete.getId(),
* Integer.parseInt(propertiesBean.getProperty("engine.unique.id"))); }
* } } } catch(PrivilegeException ex) {
* log.error("Consistency error. {}", ex); }
* catch(FacilityNotExistsException ex) {
* log.error("Consistency error - found task for non-existing facility. {}"
* , ex); } catch(ServiceNotExistsException ex) {
* log.error("Consistency error - found task for non-existing service. {}"
* , ex); } catch(InternalErrorException ex) { log.error("{}", ex); } }
* }
*/
}
Aggregations