Search in sources :

Example 1 with TaskDefinition

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

the class CloseStaleVisitsSetup method setupCloseStaleVisitsTask.

public static void setupCloseStaleVisitsTask() {
    SchedulerService schedulerService = Context.getSchedulerService();
    TaskDefinition task = schedulerService.getTaskByName(EmrConstants.TASK_CLOSE_STALE_VISITS_NAME);
    if (task == null) {
        task = new TaskDefinition();
        task.setName(EmrConstants.TASK_CLOSE_STALE_VISITS_NAME);
        task.setDescription(EmrConstants.TASK_CLOSE_STALE_VISITS_DESCRIPTION);
        task.setTaskClass(PihCloseStaleVisitsTask.class.getName());
        task.setStartTime(DateUtils.addMinutes(new Date(), 5));
        task.setRepeatInterval(EmrConstants.TASK_CLOSE_STALE_VISITS_REPEAT_INTERVAL);
        task.setStartOnStartup(true);
        try {
            schedulerService.scheduleTask(task);
        } catch (SchedulerException e) {
            throw new RuntimeException("Failed to schedule close stale visits task", e);
        }
    } else {
        // if you modify any of the properties above, you also need to set them here, in order to update existing servers
        boolean changed = GeneralUtils.setPropertyIfDifferent(task, "taskClass", PihCloseStaleVisitsTask.class.getName());
        if (changed) {
            schedulerService.saveTaskDefinition(task);
        }
        if (!task.getStarted()) {
            task.setStarted(true);
            try {
                schedulerService.scheduleTask(task);
            } catch (SchedulerException e) {
                throw new RuntimeException("Failed to schedule close stale visits task", e);
            }
        }
    }
}
Also used : SchedulerService(org.openmrs.scheduler.SchedulerService) TaskDefinition(org.openmrs.scheduler.TaskDefinition) SchedulerException(org.openmrs.scheduler.SchedulerException) PihCloseStaleVisitsTask(org.openmrs.module.pihcore.task.PihCloseStaleVisitsTask) Date(java.util.Date)

Example 2 with TaskDefinition

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

the class RetireProvidersSetup method setupRetireProvidersTask.

public static void setupRetireProvidersTask() {
    SchedulerService schedulerService = Context.getSchedulerService();
    TaskDefinition task = schedulerService.getTaskByName(TASK_RETIRE_PROVIDERS);
    if (task == null) {
        task = new TaskDefinition();
        task.setName(TASK_RETIRE_PROVIDERS);
        task.setDescription(TASK_RETIRE_PROVIDERS);
        task.setTaskClass(UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts.class.getName());
        task.setStartTime(DateUtils.addMinutes(new Date(), 5));
        task.setRepeatInterval(TASK_RETIRE_PROVIDERS_REPEAT_INTERVAL);
        task.setStartOnStartup(true);
        try {
            schedulerService.scheduleTask(task);
        } catch (SchedulerException e) {
            throw new RuntimeException("Failed to schedule retire old providers task", e);
        }
    } else {
        // if you modify any of the properties above, you also need to set them here, in order to update existing servers
        boolean changed = GeneralUtils.setPropertyIfDifferent(task, "taskClass", UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts.class.getName());
        if (changed) {
            schedulerService.saveTaskDefinition(task);
        }
        if (!task.getStarted()) {
            task.setStarted(true);
            try {
                schedulerService.scheduleTask(task);
            } catch (SchedulerException e) {
                throw new RuntimeException("Failed to schedule retire old providers task", e);
            }
        }
    }
}
Also used : SchedulerService(org.openmrs.scheduler.SchedulerService) TaskDefinition(org.openmrs.scheduler.TaskDefinition) SchedulerException(org.openmrs.scheduler.SchedulerException) UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts(org.openmrs.module.pihcore.task.UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts) Date(java.util.Date)

Example 3 with TaskDefinition

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

the class PihCoreActivatorTest method testSetupCloseStateVisits.

@Test
public void testSetupCloseStateVisits() throws Exception {
    CloseStaleVisitsSetup.setupCloseStaleVisitsTask();
    // verify scheduled task is started
    TaskDefinition closeStaleVisitsTask = schedulerService.getTaskByName(EmrConstants.TASK_CLOSE_STALE_VISITS_NAME);
    assertThat(closeStaleVisitsTask, is(notNullValue()));
    assertThat(closeStaleVisitsTask.getStarted(), is(true));
    assertThat(closeStaleVisitsTask.getStartOnStartup(), is(true));
    assertTrue(closeStaleVisitsTask.getSecondsUntilNextExecutionTime() <= 300);
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Example 4 with TaskDefinition

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

the class SchedulerFormValidator method validate.

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null or empty or whitespace
 * @should fail validation if taskClass is empty or whitespace
 * @should fail validation if repeatInterval is null or empty or whitespace
 * @should fail validation if class is not instance of Task
 * @should fail validation if class is not accessible
 * @should fail validation if class cannot be instantiated
 * @should fail validation if class not found
 * @should pass validation if all required fields have proper values
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object obj, Errors errors) {
    TaskDefinition taskDefinition = (TaskDefinition) obj;
    if (taskDefinition == null) {
        errors.rejectValue("task", "error.general");
    } else {
        // Won't work without name and description properties on Task Definition
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "Scheduler.taskForm.required", new Object[] { "Task name", taskDefinition.getName() });
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "taskClass", "Scheduler.taskForm.required", new Object[] { "Task class", taskDefinition.getTaskClass() });
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "repeatInterval", "Scheduler.taskForm.required", new Object[] { "Repeat interval", taskDefinition.getRepeatInterval() });
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "description", "taskClass", "startTimePattern");
        // Check if the class is valid
        try {
            Class<?> taskClass = OpenmrsClassLoader.getInstance().loadClass(taskDefinition.getTaskClass());
            Object o = taskClass.newInstance();
            if (!(o instanceof Task)) {
                errors.rejectValue("taskClass", "Scheduler.taskForm.classDoesNotImplementTask", new Object[] { taskDefinition.getTaskClass(), Task.class.getName() }, "Class does not implement Task interface");
            }
        } catch (IllegalAccessException iae) {
            errors.rejectValue("taskClass", "Scheduler.taskForm.illegalAccessException", new Object[] { taskDefinition.getTaskClass() }, "Illegal access exception.");
        } catch (InstantiationException ie) {
            errors.rejectValue("taskClass", "Scheduler.taskForm.instantiationException", new Object[] { taskDefinition.getTaskClass() }, "Error creating new instance of class.");
        } catch (ClassNotFoundException cnfe) {
            errors.rejectValue("taskClass", "Scheduler.taskForm.classNotFoundException", new Object[] { taskDefinition.getTaskClass() }, "Class not found error.");
        }
    }
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) Task(org.openmrs.scheduler.Task)

Example 5 with TaskDefinition

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

the class TimerSchedulerServiceImpl method onStartup.

/**
 * Start up hook for the scheduler and all of its scheduled tasks.
 */
@Override
public void onStartup() {
    log.debug("Starting scheduler service ...");
    // Get all of the tasks in the database
    Collection<TaskDefinition> taskDefinitions = getSchedulerDAO().getTasks();
    // Iterate through the tasks and start them if their startOnStartup flag is true
    if (taskDefinitions != null) {
        for (TaskDefinition taskDefinition : taskDefinitions) {
            try {
                // Otherwise it needs to be started manually.
                if (taskDefinition.getStartOnStartup()) {
                    scheduleTask(taskDefinition);
                }
            } catch (Exception e) {
                log.error("Failed to schedule task for class " + taskDefinition.getTaskClass(), e);
            }
        }
    }
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) APIException(org.openmrs.api.APIException) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException) SchedulerException(org.openmrs.scheduler.SchedulerException)

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