Search in sources :

Example 6 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class TimerSchedulerServiceImpl method restoreFromMemento.

/**
 */
@Override
@SuppressWarnings("unchecked")
public void restoreFromMemento(OpenmrsMemento memento) {
    if (memento != null && memento instanceof TimerSchedulerMemento) {
        TimerSchedulerMemento timerMemento = (TimerSchedulerMemento) memento;
        Set<Integer> taskIds = (HashSet<Integer>) timerMemento.getState();
        // try to start all of the tasks that were stopped right before this restore
        for (Integer taskId : taskIds) {
            TaskDefinition task = getTask(taskId);
            try {
                scheduleTask(task);
            } catch (Exception e) {
                // essentially swallow exceptions
                log.debug("EXPECTED ERROR IF STOPPING THIS TASK'S MODULE: Unable to start task " + taskId, e);
                // save this errored task and try again next time we restore
                timerMemento.addErrorTask(taskId);
            }
        }
    }
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) APIException(org.openmrs.api.APIException) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException) SchedulerException(org.openmrs.scheduler.SchedulerException) HashSet(java.util.HashSet)

Example 7 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class TimerSchedulerServiceImpl method saveToMemento.

/**
 * Saves and stops all active tasks
 *
 * @return OpenmrsMemento
 */
@Override
public OpenmrsMemento saveToMemento() {
    Set<Integer> tasks = new HashSet<>();
    for (TaskDefinition task : getScheduledTasks()) {
        tasks.add(task.getId());
        try {
            shutdownTask(task);
        } catch (SchedulerException e) {
            // just swallow exceptions
            log.debug("Failed to stop task while saving memento " + task.getName(), e);
        }
    }
    TimerSchedulerMemento memento = new TimerSchedulerMemento(tasks);
    memento.saveErrorTasks();
    return memento;
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) SchedulerException(org.openmrs.scheduler.SchedulerException) HashSet(java.util.HashSet)

Example 8 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class TimerSchedulerServiceImpl method getScheduledTasks.

/**
 * Get all scheduled tasks.
 *
 * @return all scheduled tasks
 */
@Override
public Collection<TaskDefinition> getScheduledTasks() {
    // The real list of scheduled tasks is kept up-to-date in the scheduledTasks map
    // TODO change the index for the scheduledTasks map to be the TaskDefinition rather than the ID
    List<TaskDefinition> list = new ArrayList<>();
    if (scheduledTasks != null) {
        Set<Integer> taskIds = scheduledTasks.keySet();
        for (Integer id : taskIds) {
            TaskDefinition task = getTask(id);
            log.debug("Adding scheduled task " + id + " to list (" + task.getRepeatInterval() + ")");
            list.add(task);
        }
    }
    return list;
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) ArrayList(java.util.ArrayList)

Example 9 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class HibernateSchedulerDAO method deleteTask.

/**
 * Delete task from database.
 *
 * @param taskId <code>Integer</code> identifier of task to be deleted
 * @throws DAOException
 */
@Override
public void deleteTask(Integer taskId) throws DAOException {
    TaskDefinition taskConfig = getTask(taskId);
    deleteTask(taskConfig);
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition)

Example 10 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class HibernateSchedulerDAO method getTaskByName.

/**
 * Get task by public name.
 *
 * @param name public task name
 * @return task with given public name
 * @throws DAOException
 */
@Override
public TaskDefinition getTaskByName(String name) throws DAOException {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(TaskDefinition.class).add(Restrictions.eq("name", name));
    TaskDefinition task = (TaskDefinition) crit.uniqueResult();
    if (task == null) {
        log.warn("Task '" + name + "' not found");
        throw new ObjectRetrievalFailureException(TaskDefinition.class, name);
    }
    return task;
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException) Criteria(org.hibernate.Criteria)

Aggregations

TaskDefinition (org.openmrs.scheduler.TaskDefinition)30 Test (org.junit.Test)12 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)11 BindException (org.springframework.validation.BindException)10 Errors (org.springframework.validation.Errors)10 SchedulerException (org.openmrs.scheduler.SchedulerException)9 SchedulerService (org.openmrs.scheduler.SchedulerService)7 Date (java.util.Date)6 APIException (org.openmrs.api.APIException)3 ObjectRetrievalFailureException (org.springframework.orm.ObjectRetrievalFailureException)3 HashSet (java.util.HashSet)2 Task (org.openmrs.scheduler.Task)2 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Criteria (org.hibernate.Criteria)1 MarkAppointmentsAsMissedOrCompletedTask (org.openmrs.module.mirebalais.task.MarkAppointmentsAsMissedOrCompletedTask)1 CloseStaleCreateRequestsTask (org.openmrs.module.paperrecord.CloseStaleCreateRequestsTask)1 CloseStalePullRequestsTask (org.openmrs.module.paperrecord.CloseStalePullRequestsTask)1 PihCloseStaleVisitsTask (org.openmrs.module.pihcore.task.PihCloseStaleVisitsTask)1 UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts (org.openmrs.module.pihcore.task.UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts)1