Search in sources :

Example 1 with ScheduledJobRunner

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());
}
Also used : ScheduledJobRunner(org.opensearch.jobscheduler.spi.ScheduledJobRunner) ScheduledJobParameter(org.opensearch.jobscheduler.spi.ScheduledJobParameter) Scheduler(org.opensearch.threadpool.Scheduler) Schedule(org.opensearch.jobscheduler.spi.schedule.Schedule) CronSchedule(org.opensearch.jobscheduler.spi.schedule.CronSchedule)

Example 2 with ScheduledJobRunner

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;
        }
    });
}
Also used : ScheduledJobParameter(org.opensearch.jobscheduler.spi.ScheduledJobParameter) ScheduledJobRunner(org.opensearch.jobscheduler.spi.ScheduledJobRunner) ScheduledJobProvider(org.opensearch.jobscheduler.ScheduledJobProvider) JobDocVersion(org.opensearch.jobscheduler.spi.JobDocVersion) XContentParser(org.opensearch.common.xcontent.XContentParser) OpenSearchException(org.opensearch.OpenSearchException) VisibleForTesting(org.opensearch.jobscheduler.utils.VisibleForTesting)

Example 3 with ScheduledJobRunner

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);
    }
}
Also used : JobSchedulerExtension(org.opensearch.jobscheduler.spi.JobSchedulerExtension) ScheduledJobRunner(org.opensearch.jobscheduler.spi.ScheduledJobRunner) ScheduledJobParser(org.opensearch.jobscheduler.spi.ScheduledJobParser)

Aggregations

ScheduledJobRunner (org.opensearch.jobscheduler.spi.ScheduledJobRunner)3 ScheduledJobParameter (org.opensearch.jobscheduler.spi.ScheduledJobParameter)2 OpenSearchException (org.opensearch.OpenSearchException)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 ScheduledJobProvider (org.opensearch.jobscheduler.ScheduledJobProvider)1 JobDocVersion (org.opensearch.jobscheduler.spi.JobDocVersion)1 JobSchedulerExtension (org.opensearch.jobscheduler.spi.JobSchedulerExtension)1 ScheduledJobParser (org.opensearch.jobscheduler.spi.ScheduledJobParser)1 CronSchedule (org.opensearch.jobscheduler.spi.schedule.CronSchedule)1 Schedule (org.opensearch.jobscheduler.spi.schedule.Schedule)1 VisibleForTesting (org.opensearch.jobscheduler.utils.VisibleForTesting)1 Scheduler (org.opensearch.threadpool.Scheduler)1