use of org.opensearch.jobscheduler.spi.ScheduledJobRunner in project job-scheduler by opensearch-project.
the class JobSchedulerTests method testSchedule.
public void testSchedule() {
Schedule schedule = Mockito.mock(Schedule.class);
ScheduledJobRunner runner = Mockito.mock(ScheduledJobRunner.class);
ScheduledJobParameter jobParameter = buildScheduledJobParameter("job-id", "dummy job name", Instant.now().minus(1, ChronoUnit.HOURS), Instant.now(), schedule, true);
Mockito.when(schedule.getNextExecutionTime(Mockito.any())).thenReturn(Instant.now().plus(1, ChronoUnit.MINUTES));
Scheduler.ScheduledCancellable cancellable = Mockito.mock(Scheduler.ScheduledCancellable.class);
Mockito.when(this.threadPool.schedule(Mockito.any(), Mockito.any(), Mockito.anyString())).thenReturn(cancellable);
boolean scheduled = this.scheduler.schedule("index", "job-id", jobParameter, runner, dummyVersion, jitterLimit);
Assert.assertTrue(scheduled);
Mockito.verify(this.threadPool, Mockito.times(1)).schedule(Mockito.any(), Mockito.any(), Mockito.anyString());
scheduled = this.scheduler.schedule("index", "job-id", jobParameter, runner, dummyVersion, jitterLimit);
Assert.assertTrue(scheduled);
// already scheduled, no extra threadpool call
Mockito.verify(this.threadPool, Mockito.times(1)).schedule(Mockito.any(), Mockito.any(), Mockito.anyString());
}
use of org.opensearch.jobscheduler.spi.ScheduledJobRunner in project job-scheduler by opensearch-project.
the class JobSweeper method sweep.
@VisibleForTesting
void sweep(ShardId shardId, String docId, BytesReference jobSource, JobDocVersion jobDocVersion) {
ConcurrentHashMap<String, JobDocVersion> jobVersionMap;
if (this.sweptJobs.containsKey(shardId)) {
jobVersionMap = this.sweptJobs.get(shardId);
} else {
jobVersionMap = new ConcurrentHashMap<>();
this.sweptJobs.put(shardId, jobVersionMap);
}
jobVersionMap.compute(docId, (id, currentJobDocVersion) -> {
if (jobDocVersion.compareTo(currentJobDocVersion) <= 0) {
log.debug("Skipping job {}, new version {} <= current version {}", docId, jobDocVersion, currentJobDocVersion);
return currentJobDocVersion;
}
if (this.scheduler.getScheduledJobIds(shardId.getIndexName()).contains(docId)) {
this.scheduler.deschedule(shardId.getIndexName(), docId);
}
if (jobSource != null) {
try {
ScheduledJobProvider provider = this.indexToProviders.get(shardId.getIndexName());
XContentParser parser = XContentHelper.createParser(this.xContentRegistry, LoggingDeprecationHandler.INSTANCE, jobSource, XContentType.JSON);
ScheduledJobParameter jobParameter = provider.getJobParser().parse(parser, docId, jobDocVersion);
if (jobParameter == null) {
// allow parser to return null, which means this is not a scheduled job document.
return null;
}
ScheduledJobRunner jobRunner = this.indexToProviders.get(shardId.getIndexName()).getJobRunner();
if (jobParameter.isEnabled()) {
this.scheduler.schedule(shardId.getIndexName(), docId, jobParameter, jobRunner, jobDocVersion, jitterLimit);
}
return jobDocVersion;
} catch (Exception e) {
log.warn("Unable to parse job {}, error message: {}", docId, e.getMessage());
return currentJobDocVersion;
}
} else {
return null;
}
});
}
use of org.opensearch.jobscheduler.spi.ScheduledJobRunner in project job-scheduler by opensearch-project.
the class JobSchedulerPlugin method loadExtensions.
@Override
public void loadExtensions(ExtensionLoader loader) {
for (JobSchedulerExtension extension : loader.loadExtensions(JobSchedulerExtension.class)) {
String jobType = extension.getJobType();
String jobIndexName = extension.getJobIndex();
ScheduledJobParser jobParser = extension.getJobParser();
ScheduledJobRunner runner = extension.getJobRunner();
if (this.indexToJobProviders.containsKey(jobIndexName)) {
continue;
}
ScheduledJobProvider provider = new ScheduledJobProvider(jobType, jobIndexName, jobParser, runner);
this.indexToJobProviders.put(jobIndexName, provider);
this.indicesToListen.add(jobIndexName);
log.info("Loaded scheduler extension: {}, index: {}", jobType, jobIndexName);
}
}
Aggregations