Search in sources :

Example 1 with TaskStatus

use of cz.metacentrum.perun.taskslib.model.Task.TaskStatus 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.
    List<Task> suspiciousTasks = schedulingPool.getProcessingTasks();
    log.debug("There are {} PROCESSING tasks", suspiciousTasks.size());
    suspiciousTasks.addAll(schedulingPool.getPlannedTasks());
    log.debug("There are {} tasks that are PLANNED or PROCESSING", suspiciousTasks.size());
    for (Task task : suspiciousTasks) {
        log.debug("checking task " + task.toString() + " for staying around too long...");
        // 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 checkDate = task.getStatus().equals(TaskStatus.PLANNED) ? task.getSchedule() : task.getStartTime();
        if (checkDate == null) {
            log.error("ERROR: task in state {} has no corresponding timestamp", task.getStatus());
            checkDate = new Date(System.currentTimeMillis());
            if (task.getStatus().equals(TaskStatus.PLANNED)) {
                task.setSchedule(checkDate);
            } else {
                task.setStartTime(checkDate);
            }
        }
        Date ended = task.getEndTime();
        TaskStatus status = task.getStatus();
        if (ended != null || status.equals(TaskStatus.DONE) || status.equals(TaskStatus.ERROR)) {
            log.error("ERROR: Task presumably in PLANNED or PROCESSING state, but appears to have ended.");
            cz.metacentrum.perun.engine.scheduling.TaskStatus taskStatus = taskStatusManager.getTaskStatus(task);
            if (taskStatus.isTaskFinished()) {
                schedulingPool.setTaskStatus(task, taskStatus.getTaskStatus());
                log.debug("TASK " + task.getId() + " status set to DONE");
            } else {
                // there is something deeply wrong...
                log.error("ERROR: Task is weird. Switching it to ERROR. {}", task);
                task.setEndTime(new Date(System.currentTimeMillis()));
                schedulingPool.setTaskStatus(task, TaskStatus.ERROR);
            }
        }
        int howManyMinutesAgo = (int) (System.currentTimeMillis() - checkDate.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);
        }
    }
/*
		 *
		 * 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 >= 180) { log.error(
		 * "ERROR: Task is stucked 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 2 with TaskStatus

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

the class SchedulingPoolImpl method addToPool.

@Override
public int addToPool(Task task) {
    synchronized (pool) {
        if (taskIdMap.containsKey(task.getId())) {
            log.warn("Task already is in the pool " + task.toString());
            return this.getSize();
        }
        taskIdMap.put(task.getId(), task);
        TaskStatus status = task.getStatus();
        if (status == null) {
            task.setStatus(TaskStatus.NONE);
        }
        if (!pool.get(task.getStatus()).contains(task)) {
            pool.get(task.getStatus()).add(task);
        }
    }
    return this.getSize();
}
Also used : TaskStatus(cz.metacentrum.perun.taskslib.model.Task.TaskStatus)

Example 3 with TaskStatus

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

the class SchedulingPoolImpl method checkTasksDb.

@Override
public void checkTasksDb() {
    log.debug("Going to cross-check tasks in database...");
    for (Pair<Task, Integer> pair : taskManager.listAllTasksAndClients()) {
        Task task = pair.getLeft();
        DispatcherQueue taskQueue = dispatcherQueuePool.getDispatcherQueueByClient(pair.getRight());
        TaskStatus status = task.getStatus();
        if (status == null) {
            task.setStatus(TaskStatus.NONE);
        }
        Task local_task = null;
        TaskStatus local_status = null;
        log.debug("  checking task " + task.toString());
        if (taskQueue == null) {
            log.warn("  there is no task queue for client " + pair.getRight());
        // continue;
        }
        synchronized (tasksById) {
            Pair<Task, DispatcherQueue> local_pair = tasksById.get(task.getId());
            if (local_pair != null) {
                local_task = local_pair.getLeft();
            }
            if (local_task == null) {
                local_task = tasksByServiceAndFacility.get(new Pair<Integer, Integer>(task.getExecServiceId(), task.getFacilityId()));
            }
            if (local_task == null) {
                for (TaskStatus sts : TaskStatus.class.getEnumConstants()) {
                    List<Task> tasklist = pool.get(sts);
                    if (tasklist != null) {
                        local_task = tasklist.get(task.getId());
                    }
                    if (local_task != null) {
                        local_status = sts;
                        break;
                    }
                }
            }
        }
        if (local_task == null) {
            try {
                log.debug("  task not found in any of local structures, adding fresh");
                addToPool(task, taskQueue);
            } catch (InternalErrorException e) {
                log.error("Error adding task to the local structures: " + e.getMessage());
            }
        } else {
            synchronized (tasksById) {
                if (!tasksById.containsKey(local_task.getId())) {
                    log.debug("  task not known by id, adding");
                    tasksById.put(local_task.getId(), new Pair<Task, DispatcherQueue>(local_task, taskQueue));
                }
                if (!tasksByServiceAndFacility.containsKey(new Pair<Integer, Integer>(local_task.getExecServiceId(), local_task.getFacilityId()))) {
                    log.debug("  task not known by ExecService and Facility, adding");
                    tasksByServiceAndFacility.put(new Pair<Integer, Integer>(local_task.getExecServiceId(), local_task.getFacilityId()), task);
                }
                if (local_status != null && local_status != local_task.getStatus()) {
                    log.debug("  task listed with wrong status, removing");
                    if (pool.get(local_status) != null) {
                        pool.get(local_status).remove(local_task.getId());
                    } else {
                        log.error("  no task list for status " + local_status);
                    }
                }
                if (pool.get(local_task.getStatus()) != null && !pool.get(local_task.getStatus()).contains(local_task)) {
                    log.debug("  task not listed with its status, adding");
                    pool.get(local_task.getStatus()).add(local_task);
                }
            }
        }
    }
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) DispatcherQueue(cz.metacentrum.perun.dispatcher.jms.DispatcherQueue) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) TaskStatus(cz.metacentrum.perun.taskslib.model.Task.TaskStatus) Pair(cz.metacentrum.perun.core.api.Pair)

Example 4 with TaskStatus

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

the class SchedulingPoolImpl method setTaskStatus.

@Override
public void setTaskStatus(Task task, TaskStatus status) {
    TaskStatus old = task.getStatus();
    task.setStatus(status);
    // move task to the appropriate place
    synchronized (pool) {
        if (!old.equals(status)) {
            if (pool.get(old) != null) {
                pool.get(old).remove(task);
            } else {
                log.warn("task unknown by status");
            }
            if (pool.get(status) != null) {
                pool.get(status).add(task);
            } else {
                log.error("no task pool for status " + status.toString());
            }
        }
    }
    taskManager.updateTask(task);
}
Also used : TaskStatus(cz.metacentrum.perun.taskslib.model.Task.TaskStatus)

Example 5 with TaskStatus

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

the class SchedulingPoolImpl method clear.

@Override
public void clear() {
    synchronized (tasksById) {
        tasksById.clear();
        tasksByServiceAndFacility.clear();
        for (TaskStatus status : TaskStatus.class.getEnumConstants()) {
            pool.get(status).clear();
        }
    }
// taskManager.removeAllTasks();
}
Also used : TaskStatus(cz.metacentrum.perun.taskslib.model.Task.TaskStatus)

Aggregations

TaskStatus (cz.metacentrum.perun.taskslib.model.Task.TaskStatus)9 Task (cz.metacentrum.perun.taskslib.model.Task)5 Date (java.util.Date)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 DispatcherQueue (cz.metacentrum.perun.dispatcher.jms.DispatcherQueue)2 Destination (cz.metacentrum.perun.core.api.Destination)1 Pair (cz.metacentrum.perun.core.api.Pair)1 PerunBean (cz.metacentrum.perun.core.api.PerunBean)1 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)1 ArrayList (java.util.ArrayList)1