Search in sources :

Example 16 with Task

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());
    }
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) Task(cz.metacentrum.perun.taskslib.model.Task) PerunBean(cz.metacentrum.perun.core.api.PerunBean) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) TaskStatus(cz.metacentrum.perun.taskslib.model.Task.TaskStatus) Date(java.util.Date)

Example 17 with Task

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"))); }
		 * }
		 */
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) TaskStatus(cz.metacentrum.perun.taskslib.model.Task.TaskStatus) Date(java.util.Date)

Example 18 with Task

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); } }
		 *
		 * }
		 */
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) Date(java.util.Date)

Example 19 with Task

use of cz.metacentrum.perun.taskslib.model.Task in project perun by CESNET.

the class SchedulingPoolTest method addToPoolTest.

@IfProfileValue(name = "perun.test.groups", values = ("unit-tests"))
@Test
public void addToPoolTest() throws InternalErrorException {
    System.out.println("SchedulingPool.addToPool()");
    Assert.isTrue(schedulingPool.getSize() == 1, "original size is 1");
    schedulingPool.addToPool(task1, dispatcherQueue);
    // pool already contains this task
    Assert.isTrue(schedulingPool.getSize() == 1, "new size is 1");
    task2 = new Task();
    task2.setId(2);
    task2.setExecService(execservice2);
    task2.setFacility(facility1);
    task2.setDestinations(destinations);
    task2.setSchedule(new Date(System.currentTimeMillis()));
    schedulingPool.addToPool(task2, dispatcherQueue);
    Assert.isTrue(schedulingPool.getSize() == 2, "new size is 2");
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) Date(java.util.Date) AbstractDispatcherTest(cz.metacentrum.perun.dispatcher.AbstractDispatcherTest) Test(org.junit.Test) IfProfileValue(org.springframework.test.annotation.IfProfileValue)

Example 20 with Task

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);
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) AbstractDispatcherTest(cz.metacentrum.perun.dispatcher.AbstractDispatcherTest) Test(org.junit.Test) IfProfileValue(org.springframework.test.annotation.IfProfileValue)

Aggregations

Task (cz.metacentrum.perun.taskslib.model.Task)34 Date (java.util.Date)17 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)13 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)11 Test (org.junit.Test)7 DispatcherQueue (cz.metacentrum.perun.dispatcher.jms.DispatcherQueue)5 TaskStatus (cz.metacentrum.perun.taskslib.model.Task.TaskStatus)5 Destination (cz.metacentrum.perun.core.api.Destination)4 Facility (cz.metacentrum.perun.core.api.Facility)4 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)4 AbstractEngineTest (cz.metacentrum.perun.engine.AbstractEngineTest)4 ArrayList (java.util.ArrayList)4 Pair (cz.metacentrum.perun.core.api.Pair)3 ServiceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException)3 PerunBean (cz.metacentrum.perun.core.api.PerunBean)2 FacilityNotExistsException (cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException)2 AbstractDispatcherTest (cz.metacentrum.perun.dispatcher.AbstractDispatcherTest)2 InvalidEventMessageException (cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException)2 ExecutorEngineWorker (cz.metacentrum.perun.engine.scheduling.ExecutorEngineWorker)2 ExecutorEngineWorkerImpl (cz.metacentrum.perun.engine.scheduling.impl.ExecutorEngineWorkerImpl)2