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);
}
}
}
}
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);
}
}
}
}
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);
}
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.");
}
}
}
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);
}
}
}
}
Aggregations