use of org.opensearch.jobscheduler.spi.schedule.IntervalSchedule in project anomaly-detection by opensearch-project.
the class AnomalyDetectorJobRunner method runJob.
@Override
public void runJob(ScheduledJobParameter scheduledJobParameter, JobExecutionContext context) {
String detectorId = scheduledJobParameter.getName();
log.info("Start to run AD job {}", detectorId);
adTaskManager.refreshRealtimeJobRunTime(detectorId);
if (!(scheduledJobParameter instanceof AnomalyDetectorJob)) {
throw new IllegalArgumentException("Job parameter is not instance of AnomalyDetectorJob, type: " + scheduledJobParameter.getClass().getCanonicalName());
}
AnomalyDetectorJob jobParameter = (AnomalyDetectorJob) scheduledJobParameter;
Instant executionStartTime = Instant.now();
IntervalSchedule schedule = (IntervalSchedule) jobParameter.getSchedule();
Instant detectionStartTime = executionStartTime.minus(schedule.getInterval(), schedule.getUnit());
final LockService lockService = context.getLockService();
Runnable runnable = () -> {
if (jobParameter.getLockDurationSeconds() != null) {
lockService.acquireLock(jobParameter, context, ActionListener.wrap(lock -> runAdJob(jobParameter, lockService, lock, detectionStartTime, executionStartTime), exception -> {
indexAnomalyResultException(jobParameter, lockService, null, detectionStartTime, executionStartTime, exception, false);
throw new IllegalStateException("Failed to acquire lock for AD job: " + detectorId);
}));
} else {
log.warn("Can't get lock for AD job: " + detectorId);
}
};
ExecutorService executor = threadPool.executor(AD_THREAD_POOL_NAME);
executor.submit(runnable);
}
use of org.opensearch.jobscheduler.spi.schedule.IntervalSchedule in project anomaly-detection by opensearch-project.
the class AnomalyDetectorJobRunnerTests method setUpJobParameter.
private void setUpJobParameter() {
when(jobParameter.getName()).thenReturn(randomAlphaOfLength(10));
IntervalSchedule schedule = new IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES);
when(jobParameter.getSchedule()).thenReturn(schedule);
when(jobParameter.getWindowDelay()).thenReturn(new IntervalTimeConfiguration(10, ChronoUnit.SECONDS));
}
use of org.opensearch.jobscheduler.spi.schedule.IntervalSchedule in project anomaly-detection by opensearch-project.
the class AnomalyDetectorJobRunnerTests method testRunJobWithNullLockDuration.
@Test
public void testRunJobWithNullLockDuration() throws InterruptedException {
when(jobParameter.getLockDurationSeconds()).thenReturn(null);
when(jobParameter.getSchedule()).thenReturn(new IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES));
runner.runJob(jobParameter, context);
Thread.sleep(1000);
assertTrue(testAppender.containsMessage("Can't get lock for AD job"));
}
use of org.opensearch.jobscheduler.spi.schedule.IntervalSchedule in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorJobActionHandler method createJob.
private void createJob(AnomalyDetector detector) {
try {
IntervalTimeConfiguration interval = (IntervalTimeConfiguration) detector.getDetectionInterval();
Schedule schedule = new IntervalSchedule(Instant.now(), (int) interval.getInterval(), interval.getUnit());
Duration duration = Duration.of(interval.getInterval(), interval.getUnit());
AnomalyDetectorJob job = new AnomalyDetectorJob(detector.getDetectorId(), schedule, detector.getWindowDelay(), true, Instant.now(), null, Instant.now(), duration.getSeconds(), detector.getUser(), detector.getResultIndex());
getAnomalyDetectorJobForWrite(detector, job);
} catch (Exception e) {
String message = "Failed to parse anomaly detector job " + detectorId;
logger.error(message, e);
listener.onFailure(new OpenSearchStatusException(message, RestStatus.INTERNAL_SERVER_ERROR));
}
}
use of org.opensearch.jobscheduler.spi.schedule.IntervalSchedule in project anomaly-detection by opensearch-project.
the class AnomalyDetectorJobRunnerTests method testRunJobWithLockDuration.
@Test
public void testRunJobWithLockDuration() throws InterruptedException {
when(jobParameter.getLockDurationSeconds()).thenReturn(100L);
when(jobParameter.getSchedule()).thenReturn(new IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES));
runner.runJob(jobParameter, context);
Thread.sleep(1000);
assertFalse(testAppender.containsMessage("Can't get lock for AD job"));
verify(context, times(1)).getLockService();
}
Aggregations