Brightspot CMS Developer Guide

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. */
    }
}

Previous Topic
Schedule tasks
Next Topic
Recalculation tasks
Was this topic helpful?
Thanks for your feedback.
The elements that get you up and running in a matter of days, from pre-built content types, to modules, to landing pages.

Content types
Modules
Landing pages
Everything you need to manage and administer content within Brightspot CMS, including plug-and-play integrations.

Dashboards
Authoring content
Workflows
Admin configurations
A guide for installing, supporting and administering code on the Brightspot platform, including integrations requiring developer support to use.

Field types
Content modeling
Rich-text elements
Images