Search in sources :

Example 21 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-module-mirebalais by PIH.

the class ArchivesSetup method setupCloseStaleCreateRequestsTask.

public static void setupCloseStaleCreateRequestsTask() {
    SchedulerService schedulerService = Context.getSchedulerService();
    TaskDefinition task = schedulerService.getTaskByName(PaperRecordConstants.TASK_CLOSE_STALE_CREATE_REQUESTS);
    if (task == null) {
        task = new TaskDefinition();
        task.setName(PaperRecordConstants.TASK_CLOSE_STALE_CREATE_REQUESTS);
        task.setDescription(PaperRecordConstants.TASK_CLOSE_STALE_CREATE_REQUESTS_DESCRIPTION);
        task.setTaskClass(CloseStaleCreateRequestsTask.class.getName());
        task.setStartTime(DateUtils.addMinutes(new Date(), 5));
        // once an hour
        task.setRepeatInterval(new Long(3600));
        task.setStartOnStartup(true);
        try {
            schedulerService.scheduleTask(task);
        } catch (SchedulerException e) {
            throw new RuntimeException("Failed to schedule close stale create requests task", e);
        }
    } else {
        boolean anyChanges = GeneralUtils.setPropertyIfDifferent(task, "description", PaperRecordConstants.TASK_CLOSE_STALE_CREATE_REQUESTS_DESCRIPTION);
        anyChanges |= GeneralUtils.setPropertyIfDifferent(task, "taskClass", CloseStaleCreateRequestsTask.class.getName());
        anyChanges |= GeneralUtils.setPropertyIfDifferent(task, "repeatInterval", new Long(3600));
        anyChanges |= GeneralUtils.setPropertyIfDifferent(task, "startOnStartup", true);
        if (anyChanges) {
            schedulerService.saveTaskDefinition(task);
        }
        if (!task.getStarted()) {
            task.setStarted(true);
            try {
                schedulerService.scheduleTask(task);
            } catch (SchedulerException e) {
                throw new RuntimeException("Failed to schedule close stale create requests task", e);
            }
        }
    }
}
Also used : SchedulerService(org.openmrs.scheduler.SchedulerService) TaskDefinition(org.openmrs.scheduler.TaskDefinition) SchedulerException(org.openmrs.scheduler.SchedulerException) CloseStaleCreateRequestsTask(org.openmrs.module.paperrecord.CloseStaleCreateRequestsTask) Date(java.util.Date)

Example 22 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-module-mirebalais by PIH.

the class AppointmentSchedulingSetup method setupMarkAppointmentAsMissedOrCompletedTask.

public static void setupMarkAppointmentAsMissedOrCompletedTask() {
    SchedulerService schedulerService = Context.getSchedulerService();
    TaskDefinition task = schedulerService.getTaskByName(MirebalaisConstants.TASK_MARK_APPOINTMENTS_AS_MISSED_OR_COMPLETED);
    if (task == null) {
        task = new TaskDefinition();
        task.setName(MirebalaisConstants.TASK_MARK_APPOINTMENTS_AS_MISSED_OR_COMPLETED);
        task.setDescription(MirebalaisConstants.TASK_MARK_APPOINTMENTS_AS_MISSED_OR_COMPLETED_DESCRIPTION);
        task.setTaskClass(MarkAppointmentsAsMissedOrCompletedTask.class.getName());
        // doesn't really do anything since start on startup = true
        task.setStartTime(DateUtils.addMinutes(new Date(), 5));
        task.setRepeatInterval(MirebalaisConstants.TASK_MARK_APPOINTMENTS_AS_MISSED_OR_COMPLETED_REPEAT_INTERVAL);
        task.setStartOnStartup(true);
        try {
            schedulerService.scheduleTask(task);
        } catch (SchedulerException e) {
            throw new RuntimeException("Failed to schedule mark appointments as missed or completed task", e);
        }
    } else {
        boolean anyChanges = GeneralUtils.setPropertyIfDifferent(task, "description", MirebalaisConstants.TASK_MARK_APPOINTMENTS_AS_MISSED_OR_COMPLETED_DESCRIPTION);
        anyChanges |= GeneralUtils.setPropertyIfDifferent(task, "taskClass", MarkAppointmentsAsMissedOrCompletedTask.class.getName());
        // we can't pass in the constant directly for some reason because it is static
        anyChanges |= GeneralUtils.setPropertyIfDifferent(task, "repeatInterval", MirebalaisConstants.TASK_MARK_APPOINTMENTS_AS_MISSED_OR_COMPLETED_REPEAT_INTERVAL);
        anyChanges |= GeneralUtils.setPropertyIfDifferent(task, "startOnStartup", true);
        if (anyChanges) {
            schedulerService.saveTaskDefinition(task);
        }
        if (!task.getStarted()) {
            task.setStarted(true);
            try {
                schedulerService.scheduleTask(task);
            } catch (SchedulerException e) {
                throw new RuntimeException("Failed to schedule mark appointments as missed or completed task", e);
            }
        }
    }
}
Also used : SchedulerService(org.openmrs.scheduler.SchedulerService) TaskDefinition(org.openmrs.scheduler.TaskDefinition) SchedulerException(org.openmrs.scheduler.SchedulerException) MarkAppointmentsAsMissedOrCompletedTask(org.openmrs.module.mirebalais.task.MarkAppointmentsAsMissedOrCompletedTask) Date(java.util.Date)

Example 23 with TaskDefinition

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

the class TimerSchedulerServiceImpl method deleteTask.

/**
 * Delete the task with the given identifier.
 *
 * @param id the identifier of the task
 */
@Override
public void deleteTask(Integer id) {
    TaskDefinition task = getTask(id);
    if (task.getStarted()) {
        throw new APIException("Scheduler.timer.task.delete", (Object[]) null);
    }
    // delete the task
    getSchedulerDAO().deleteTask(id);
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) APIException(org.openmrs.api.APIException)

Example 24 with TaskDefinition

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

the class TimerSchedulerTask method saveLastExecutionTime.

/**
 * Save the last execution time in the TaskDefinition
 */
private static void saveLastExecutionTime(Task task) {
    TaskDefinition taskDefinition;
    try {
        // Therefore we might get an NPE below.
        if (task.getTaskDefinition() != null) {
            SchedulerService schedulerService = Context.getSchedulerService();
            taskDefinition = task.getTaskDefinition();
            taskDefinition.setLastExecutionTime(new Date());
            schedulerService.saveTaskDefinition(taskDefinition);
        } else {
            log.warn("Unable to save the last execution time for task. Task.taskDefinition is null in " + task.getClass());
        }
    } catch (Exception e) {
        log.warn("Unable to save the last execution time for task ", e);
    }
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) SchedulerService(org.openmrs.scheduler.SchedulerService) Date(java.util.Date)

Example 25 with TaskDefinition

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

the class SchedulerFormValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.

/**
 * @see SchedulerFormValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
    TaskDefinition def = new TaskDefinition();
    def.setName("Chores");
    def.setRepeatInterval(3600000L);
    def.setTaskClass("org.openmrs.scheduler.tasks.HelloWorldTask");
    def.setDescription("description");
    def.setStartTimePattern("startTimePattern");
    Errors errors = new BindException(def, "def");
    new SchedulerFormValidator().validate(def, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) TaskDefinition(org.openmrs.scheduler.TaskDefinition) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

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