Repeating tasks
Although the base Task
class provides methods to schedule tasks to run with periodic delays, the com.psddev.dari.util.RepeatingTask class provides enhanced interval control. A subclass of Task, RepeatingTask
adds interval-control methods based on Joda Time. For example, you can call methods to run an implementation every minute, hour, or day. You can also adjust intervals at run time based on conditional testing.
A repeating task requires that you implement calculateRunTime
and doRepeatingTask
. calculateRunTime
determines the run time intervals of the repeating task. Passed a Joda DateTime object representing the current time, calculateRunTime
returns a DateTime
object representing the run time interval, such as minute, hour, or day. The doRepeatingTask
method implements the task logic.
When a repeating task instance is created, the Dari framework calls the concrete RepeatingTask#doTask
method, which controls the run-and-delay intervals of the implementation. doTask
first calls calculateRunTime
. Based on the DateTime
object returned, doTask
evaluates whether the implementation should delay or execute. If doTask
evaluates to execute, it passes the DateTime
object to the doRepeatingTask
method. After doRepeatingTask
executes, the framework increments the task’s run count by one and returns control to doTask
.
The following snippet implements the data migration as a repeating task. Called automatically by the framework, the constructor sets the name and the category of the task that displays in the Task Status tool.
The calculateRunTime
implementation simply specifies that the task repeat execution every minute. However, calculateRunTime
can be implemented to test run time conditions and adjust execution intervals if need be.
ublic class MigrationTask extends RepeatingTask {
public MigrationTask() {
super("Migration", "Migration Task");
}
@Override
public DateTime calculateRunTime(DateTime currentTime) {
return everyMinute(currentTime);
}
@Override
public void doRepeatingTask(DateTime currentTime) throws Exception {
/* Do processing here. */
}
}