use of org.openmrs.scheduler.SchedulerException 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.SchedulerException 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.SchedulerException in project openmrs-core by openmrs.
the class TimerSchedulerServiceImpl method scheduleTask.
/**
* Schedule the given task according to the given schedule.
*
* @param taskDefinition the task to be scheduled
* @should should handle zero repeat interval
*/
@Override
public Task scheduleTask(TaskDefinition taskDefinition) throws SchedulerException {
Task clientTask = null;
if (taskDefinition != null) {
// Cancel any existing timer tasks for the same task definition
// TODO Make sure this is the desired behavior
// TODO Do we ever want the same task definition to run more than once?
TimerSchedulerTask schedulerTask = scheduledTasks.get(taskDefinition.getId());
if (schedulerTask != null) {
log.info("Shutting down the existing instance of this task to avoid conflicts!!");
schedulerTask.shutdown();
}
try {
// Create new task from task definition
clientTask = TaskFactory.getInstance().createInstance(taskDefinition);
// if we were unable to get a class, just quit
if (clientTask != null) {
schedulerTask = new TimerSchedulerTask(clientTask);
taskDefinition.setTaskInstance(clientTask);
// Once this method is called, the timer is set to start at the given start time.
// NOTE: We need to adjust the repeat interval as the JDK Timer expects time in milliseconds and
// we record by seconds.
long repeatInterval = 0;
if (taskDefinition.getRepeatInterval() != null) {
repeatInterval = taskDefinition.getRepeatInterval() * SchedulerConstants.SCHEDULER_MILLIS_PER_SECOND;
}
if (taskDefinition.getStartTime() != null) {
// Need to calculate the "next execution time" because the scheduled time is most likely in the past
// and the JDK timer will run the task X number of times from the start time until now to catch up.
Date nextTime = SchedulerUtil.getNextExecution(taskDefinition);
// Start task at fixed rate at given future date and repeat as directed
log.info("Starting task ... the task will execute for the first time at " + nextTime);
if (repeatInterval > 0) {
// Schedule the task to run at a fixed rate
getTimer(taskDefinition).scheduleAtFixedRate(schedulerTask, nextTime, repeatInterval);
} else {
// Schedule the task to be non-repeating
getTimer(taskDefinition).schedule(schedulerTask, nextTime);
}
} else if (repeatInterval > 0) {
// Start task on repeating schedule, delay for SCHEDULER_DEFAULT_DELAY seconds
log.info("Delaying start time by " + SchedulerConstants.SCHEDULER_DEFAULT_DELAY + " seconds");
getTimer(taskDefinition).scheduleAtFixedRate(schedulerTask, SchedulerConstants.SCHEDULER_DEFAULT_DELAY, repeatInterval);
} else {
// schedule for single execution, starting now
log.info("Starting one-shot task");
getTimer(taskDefinition).schedule(schedulerTask, new Date());
}
// Update task that has been started
log.debug("Registering timer for task " + taskDefinition.getId());
// Add the new timer to the scheduler running task list
scheduledTasks.put(taskDefinition.getId(), schedulerTask);
// Update the timer status in the database
taskDefinition.setStarted(true);
saveTaskDefinition(taskDefinition);
}
} catch (Exception e) {
log.error("Failed to schedule task " + taskDefinition.getName(), e);
throw new SchedulerException("Failed to schedule task", e);
}
}
return clientTask;
}
use of org.openmrs.scheduler.SchedulerException 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;
}
use of org.openmrs.scheduler.SchedulerException in project openmrs-core by openmrs.
the class WebModuleUtil method stopTasks.
/**
* Stops all tasks started by given module
* @param mod
*/
private static void stopTasks(Module mod) {
SchedulerService schedulerService = Context.getSchedulerService();
String modulePackageName = mod.getPackageName();
for (TaskDefinition task : schedulerService.getRegisteredTasks()) {
String taskClass = task.getTaskClass();
if (isModulePackageNameInTaskClass(modulePackageName, taskClass)) {
try {
schedulerService.shutdownTask(task);
} catch (SchedulerException e) {
log.error("Couldn't stop task:" + task + " for module: " + mod);
}
}
}
}
Aggregations