use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.
the class EventProcessorExecutionJobTest method executeWithNextTimeNotBasedOnCurrentTime.
@Test
public void executeWithNextTimeNotBasedOnCurrentTime() throws Exception {
final DateTime now = clock.nowUTC();
final long processingWindowSize = Duration.standardSeconds(60).getMillis();
final long processingHopSize = Duration.standardSeconds(60).getMillis();
final int scheduleIntervalSeconds = 1;
final DateTime from = now.minus(processingWindowSize);
final DateTime to = now;
final DateTime triggerNextTime = now;
final TestEventProcessorParameters eventProcessorParameters = TestEventProcessorParameters.create(from, to);
final JobDefinitionDto jobDefinition = JobDefinitionDto.builder().id("job-1").title("Test").description("A test").config(EventProcessorExecutionJob.Config.builder().eventDefinitionId("processor-1").processingWindowSize(processingWindowSize).processingHopSize(processingHopSize).parameters(eventProcessorParameters).build()).build();
final EventProcessorExecutionJob job = new EventProcessorExecutionJob(jobScheduleStrategies, clock, eventProcessorEngine, eventsConfigurationProvider, jobDefinition);
final JobTriggerDto trigger = JobTriggerDto.builderWithClock(clock).id("trigger-1").jobDefinitionId(jobDefinition.id()).startTime(now).nextTime(triggerNextTime).status(JobTriggerStatus.RUNNABLE).schedule(IntervalJobSchedule.builder().interval(scheduleIntervalSeconds).unit(TimeUnit.SECONDS).build()).build();
final JobExecutionContext jobExecutionContext = JobExecutionContext.builder().definition(jobDefinition).trigger(trigger).isRunning(new AtomicBoolean(true)).jobTriggerUpdates(new JobTriggerUpdates(clock, jobScheduleStrategies, trigger)).build();
doAnswer(invocation -> {
// Simulate work in the event processor
clock.plus(10, TimeUnit.SECONDS);
return null;
}).when(eventProcessorEngine).execute(any(), any());
final JobTriggerUpdate triggerUpdate = job.execute(jobExecutionContext);
verify(eventProcessorEngine, times(1)).execute("processor-1", eventProcessorParameters);
// The next time should be based on the previous nextTime + the schedule. The 10 second event processor
// runtime should not be added to the new nextTime.
assertThat(triggerUpdate.nextTime()).isPresent().get().isEqualTo(triggerNextTime.plusSeconds(scheduleIntervalSeconds));
assertThat(triggerUpdate.data()).isPresent().get().isEqualTo(EventProcessorExecutionJob.Data.builder().timerangeFrom(to).timerangeTo(to.plus(processingWindowSize)).build());
assertThat(triggerUpdate.status()).isNotPresent();
}
use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.
the class EventProcessorExecutionJobTest method executeWithTriggerDataTimerange.
@Test
public void executeWithTriggerDataTimerange() throws Exception {
final DateTime now = clock.nowUTC();
final long processingWindowSize = Duration.standardSeconds(60).getMillis();
final long processingHopSize = Duration.standardSeconds(60).getMillis();
final int scheduleIntervalSeconds = 1;
final DateTime from = now.minusDays(10).minus(processingWindowSize);
final DateTime to = now.minusDays(10);
final DateTime triggerFrom = now.minus(processingWindowSize);
final DateTime triggerTo = now;
final DateTime triggerNextTime = now;
final TestEventProcessorParameters eventProcessorParameters = TestEventProcessorParameters.create(from, to);
final JobDefinitionDto jobDefinition = JobDefinitionDto.builder().id("job-1").title("Test").description("A test").config(EventProcessorExecutionJob.Config.builder().eventDefinitionId("processor-1").processingWindowSize(processingWindowSize).processingHopSize(processingHopSize).parameters(eventProcessorParameters).build()).build();
final EventProcessorExecutionJob job = new EventProcessorExecutionJob(jobScheduleStrategies, clock, eventProcessorEngine, eventsConfigurationProvider, jobDefinition);
final JobTriggerDto trigger = JobTriggerDto.builderWithClock(clock).id("trigger-1").jobDefinitionId(jobDefinition.id()).startTime(now).nextTime(triggerNextTime).status(JobTriggerStatus.RUNNABLE).schedule(IntervalJobSchedule.builder().interval(scheduleIntervalSeconds).unit(TimeUnit.SECONDS).build()).data(EventProcessorExecutionJob.Data.create(triggerFrom, triggerTo)).build();
final JobExecutionContext jobExecutionContext = JobExecutionContext.builder().definition(jobDefinition).trigger(trigger).isRunning(new AtomicBoolean(true)).jobTriggerUpdates(new JobTriggerUpdates(clock, jobScheduleStrategies, trigger)).build();
doAnswer(invocation -> {
// Simulate work in the event processor
clock.plus(5, TimeUnit.SECONDS);
return null;
}).when(eventProcessorEngine).execute(any(), any());
final JobTriggerUpdate triggerUpdate = job.execute(jobExecutionContext);
// Check that we use the timerange from the trigger instead of the parameters
verify(eventProcessorEngine, times(1)).execute("processor-1", eventProcessorParameters.withTimerange(triggerFrom, triggerTo));
assertThat(triggerUpdate.nextTime()).isPresent().get().isEqualTo(triggerNextTime.plusSeconds(scheduleIntervalSeconds));
// The next timerange in the trigger update also needs to be based on the timerange from the trigger
assertThat(triggerUpdate.data()).isPresent().get().isEqualTo(EventProcessorExecutionJob.Data.builder().timerangeFrom(triggerTo).timerangeTo(triggerTo.plus(processingWindowSize)).build());
assertThat(triggerUpdate.status()).isNotPresent();
}
use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.
the class NotificationResourceHandler method update.
/**
* Updates an existing notification definition and its corresponding scheduler job definition.
*
* @param updatedDto the notification definition to update
* @return the updated notification definition
*/
public NotificationDto update(NotificationDto updatedDto) {
// Grab the old record so we can revert to it if something goes wrong
final Optional<NotificationDto> oldDto = notificationService.get(updatedDto.id());
final NotificationDto dto = notificationService.save(updatedDto);
LOG.debug("Updated notification definition <{}/{}>", dto.id(), dto.title());
try {
// Grab the old record so we can revert to it if something goes wrong
final JobDefinitionDto oldJobDefinition = jobDefinitionService.getByConfigField(EventNotificationConfig.FIELD_NOTIFICATION_ID, updatedDto.id()).orElseThrow(() -> new IllegalStateException("Couldn't find job definition for notification definition <" + updatedDto.id() + ">"));
// Update the existing object to make sure we keep the ID
final JobDefinitionDto unsavedJobDefinition = oldJobDefinition.toBuilder().title(dto.title()).description(dto.description()).config(getSchedulerConfig(dto.id())).build();
final JobDefinitionDto jobDefinition = jobDefinitionService.save(unsavedJobDefinition);
LOG.debug("Updated scheduler job definition <{}/{}> for notification <{}/{}>", jobDefinition.id(), jobDefinition.title(), dto.id(), dto.title());
} catch (Exception e) {
// Cleanup if anything goes wrong
LOG.error("Reverting to old notification definition <{}/{}> because of an error updating the job definition", dto.id(), dto.title(), e);
oldDto.ifPresent(notificationService::save);
throw e;
}
return dto;
}
use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.
the class EventDefinitionHandler method updateJobDefinitionAndTrigger.
private void updateJobDefinitionAndTrigger(EventDefinitionDto eventDefinition, EventProcessorSchedulerConfig schedulerConfig) {
// Grab the old record so we can revert to it if something goes wrong
final JobDefinitionDto oldJobDefinition = getJobDefinitionOrThrowISE(eventDefinition);
final JobDefinitionDto jobDefinition = updateJobDefinition(eventDefinition, oldJobDefinition, schedulerConfig);
try {
updateJobTrigger(eventDefinition, jobDefinition, oldJobDefinition, schedulerConfig);
} catch (Exception e) {
// Cleanup if anything goes wrong
LOG.error("Reverting to old job definition <{}/{}> because of an error updating the job trigger", jobDefinition.id(), jobDefinition.title(), e);
jobDefinitionService.save(oldJobDefinition);
throw e;
}
}
use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.
the class EventDefinitionHandler method createJobDefinition.
private JobDefinitionDto createJobDefinition(EventDefinitionDto eventDefinition, EventProcessorSchedulerConfig schedulerConfig) {
final JobDefinitionDto jobDefinition = jobDefinitionService.save(newJobDefinition(eventDefinition, schedulerConfig));
LOG.debug("Created scheduler job definition <{}/{}> for event definition <{}/{}>", jobDefinition.id(), jobDefinition.title(), eventDefinition.id(), eventDefinition.title());
return jobDefinition;
}
Aggregations