Job Scheduler

The OpenSearch Job Scheduler plugin provides a framework that can be used to build schedules for common tasks performed on your cluster. You can use Job Scheduler’s Service Provider Interface (SPI) to define schedules for cluster management tasks such as taking snapshots, managing your data’s lifecycle, and running periodic jobs. Job Scheduler has a sweeper that listens for updated events on the OpenSearch cluster and a scheduler that manages when jobs run.

You can install the Job Scheduler plugin by following the standard OpenSearch plugin installation process. The sample-extension-plugin example provided in the Job Scheduler GitHub repository provides a complete example of utilizing Job Scheduler when building a plugin. To define schedules, you build a plugin that implements the interfaces provided in the Job Scheduler library. You can schedule jobs by specifying an interval, or you can use a Unix cron expression such as 0 12 * * ?, which runs at noon every day, to define a more flexible schedule.

Building a plugin for Job Scheduler

OpenSearch plugin developers can extend the Job Scheduler plugin to schedule jobs to perform on the cluster. Jobs you can schedule include running aggregation queries against raw data, saving the aggregated data to a new index every hour, or continuing to monitor the shard allocation by calling the OpenSearch API and then posting the output to a webhook.

For examples of building a plugin that uses the Job Scheduler plugin, see the Job Scheduler README.

Defining an endpoint

You can configure your plugin’s API endpoint by referencing the example SampleExtensionRestHandler.java file. Set the endpoint URL that your plugin will expose with WATCH_INDEX_URI:

  1. public class SampleExtensionRestHandler extends BaseRestHandler {
  2. public static final String WATCH_INDEX_URI = "/_plugins/scheduler_sample/watch";

You can define the job configuration by extending ScheduledJobParameter. You can also define the fields used by your plugin, like indexToWatch, as shown in the example SampleJobParameter file. This job configuration will be saved as a document in an index you define, as shown in this example.

Configuring parameters

You can configure your plugin’s parameters by referencing the example SampleJobParameter.java file and modifying it to fit your needs:

  1. /**
  2. * A sample job parameter.
  3. * <p>
  4. * It adds an additional "indexToWatch" field to {@link ScheduledJobParameter}, which stores the index
  5. * the job runner will watch.
  6. */
  7. public class SampleJobParameter implements ScheduledJobParameter {
  8. public static final String NAME_FIELD = "name";
  9. public static final String ENABLED_FILED = "enabled";
  10. public static final String LAST_UPDATE_TIME_FIELD = "last_update_time";
  11. public static final String LAST_UPDATE_TIME_FIELD_READABLE = "last_update_time_field";
  12. public static final String SCHEDULE_FIELD = "schedule";
  13. public static final String ENABLED_TIME_FILED = "enabled_time";
  14. public static final String ENABLED_TIME_FILED_READABLE = "enabled_time_field";
  15. public static final String INDEX_NAME_FIELD = "index_name_to_watch";
  16. public static final String LOCK_DURATION_SECONDS = "lock_duration_seconds";
  17. public static final String JITTER = "jitter";
  18. private String jobName;
  19. private Instant lastUpdateTime;
  20. private Instant enabledTime;
  21. private boolean isEnabled;
  22. private Schedule schedule;
  23. private String indexToWatch;
  24. private Long lockDurationSeconds;
  25. private Double jitter;

Next, configure the request parameters you would like your plugin to use with Job Scheduler. These will be based on the variables you declare when configuring your plugin. The following example shows the request parameters you set when building your plugin:

  1. public SampleJobParameter(String id, String name, String indexToWatch, Schedule schedule, Long lockDurationSeconds, Double jitter) {
  2. this.jobName = name;
  3. this.indexToWatch = indexToWatch;
  4. this.schedule = schedule;
  5. Instant now = Instant.now();
  6. this.isEnabled = true;
  7. this.enabledTime = now;
  8. this.lastUpdateTime = now;
  9. this.lockDurationSeconds = lockDurationSeconds;
  10. this.jitter = jitter;
  11. }
  12. @Override
  13. public String getName() {
  14. return this.jobName;
  15. }
  16. @Override
  17. public Instant getLastUpdateTime() {
  18. return this.lastUpdateTime;
  19. }
  20. @Override
  21. public Instant getEnabledTime() {
  22. return this.enabledTime;
  23. }
  24. @Override
  25. public Schedule getSchedule() {
  26. return this.schedule;
  27. }
  28. @Override
  29. public boolean isEnabled() {
  30. return this.isEnabled;
  31. }
  32. @Override
  33. public Long getLockDurationSeconds() {
  34. return this.lockDurationSeconds;
  35. }
  36. @Override public Double getJitter() {
  37. return jitter;
  38. }

The following table describes the request parameters configured in the previous example. All the request parameters shown are required.

FieldData typeDescription
getNameStringReturns the name of the job.
getLastUpdateTimeTime unitReturns the time that the job was last run.
getEnabledTimeTime unitReturns the time that the job was enabled.
getScheduleUnix cronReturns the job schedule formatted in Unix cron syntax.
isEnabledBooleanIndicates whether or not the job is enabled.
getLockDurationSecondsIntegerReturns the duration of time for which the job is locked.
getJitterIntegerReturns the defined jitter value.

The logic used by your job should be defined by a class extended from ScheduledJobRunner in the SampleJobParameter.java sample file, such as SampleJobRunner. While the job is running, there is a locking mechanism you can use to prevent other nodes from running the same job. First, acquire the lock. Then make sure to release the lock before the job finishes.

For more information, see the Job Scheduler sample extension directory in the Job Scheduler GitHub repo.