use of org.graylog.scheduler.JobTriggerDto in project graylog2-server by Graylog2.
the class EventProcessorExecutionJobTest method executeWithOnceSchedule.
@Test
public void executeWithOnceSchedule() throws Exception {
final DateTime now = clock.nowUTC();
final long processingWindowSize = Duration.standardSeconds(60).getMillis();
final long processingHopSize = Duration.standardSeconds(60).getMillis();
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(// This job should only be triggered ONCE
OnceJobSchedule.create()).build();
final JobExecutionContext jobExecutionContext = JobExecutionContext.builder().definition(jobDefinition).trigger(trigger).isRunning(new AtomicBoolean(true)).jobTriggerUpdates(new JobTriggerUpdates(clock, jobScheduleStrategies, trigger)).build();
final JobTriggerUpdate triggerUpdate = job.execute(jobExecutionContext);
verify(eventProcessorEngine, times(1)).execute("processor-1", eventProcessorParameters);
// No nextTime because this job was triggered ONCE
assertThat(triggerUpdate.nextTime()).isNotPresent();
assertThat(triggerUpdate.data()).isNotPresent();
assertThat(triggerUpdate.status()).isNotPresent();
}
use of org.graylog.scheduler.JobTriggerDto in project graylog2-server by Graylog2.
the class EventProcessorExecutionJobTest method executeWithHoppingWindow.
@Test
public void executeWithHoppingWindow() throws Exception {
clock.plus(60, TimeUnit.SECONDS);
final DateTime now = clock.nowUTC();
// When using a hopping window, the window size is not the same as the hop size
final long processingWindowSize = Duration.standardSeconds(60).getMillis();
final long processingHopSize = Duration.standardSeconds(5).getMillis();
final int scheduleIntervalSeconds = 5;
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(7, 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 7 second event processor
// runtime should not be added to the new nextTime.
assertThat(triggerUpdate.nextTime()).isPresent().get().isEqualTo(triggerNextTime.plusSeconds(scheduleIntervalSeconds));
// With a hopping window the "to" is calculated by adding the hopSize and the "from" is based on the next "to"
// minus the windowSize + 1 millisecond.
assertThat(triggerUpdate.data()).isPresent().get().isEqualTo(EventProcessorExecutionJob.Data.builder().timerangeFrom(to.plus(processingHopSize).minus(processingWindowSize)).timerangeTo(to.plus(processingHopSize)).build());
assertThat(triggerUpdate.status()).isNotPresent();
}
use of org.graylog.scheduler.JobTriggerDto in project graylog2-server by Graylog2.
the class EventProcessorExecutionJobTest method execute.
@Test
public void execute() 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();
final JobTriggerUpdate triggerUpdate = job.execute(jobExecutionContext);
verify(eventProcessorEngine, times(1)).execute("processor-1", eventProcessorParameters);
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.JobTriggerDto in project graylog2-server by Graylog2.
the class EventDefinitionHandlerTest method create.
@Test
public void create() {
final EventDefinitionDto newDto = EventDefinitionDto.builder().title("Test").description("A test event definition").config(TestEventProcessorConfig.builder().message("This is a test event processor").searchWithinMs(300000).executeEveryMs(60001).build()).priority(3).alert(false).notificationSettings(EventNotificationSettings.withGracePeriod(60000)).keySpec(ImmutableList.of("a", "b")).notifications(ImmutableList.of()).build();
final EventDefinitionDto dto = handler.create(newDto, Optional.empty());
// Handler should create the event definition
assertThat(eventDefinitionService.get(dto.id())).isPresent();
final Optional<JobDefinitionDto> jobDefinition = jobDefinitionService.getByConfigField("event_definition_id", dto.id());
// Handler also should create the job definition for the event definition/processor
assertThat(jobDefinition).isPresent().get().satisfies(definition -> {
assertThat(definition.title()).isEqualTo("Test");
assertThat(definition.description()).isEqualTo("A test event definition");
assertThat(definition.config()).isInstanceOf(EventProcessorExecutionJob.Config.class);
final EventProcessorExecutionJob.Config config = (EventProcessorExecutionJob.Config) definition.config();
assertThat(config.processingWindowSize()).isEqualTo(300000);
assertThat(config.processingHopSize()).isEqualTo(60001);
});
// And the handler should also create a job trigger for the created job definition
final Optional<JobTriggerDto> jobTrigger = jobTriggerService.nextRunnableTrigger();
assertThat(jobTrigger).isPresent().get().satisfies(trigger -> {
assertThat(trigger.jobDefinitionId()).isEqualTo(jobDefinition.get().id());
assertThat(trigger.schedule()).isInstanceOf(IntervalJobSchedule.class);
final IntervalJobSchedule schedule = (IntervalJobSchedule) trigger.schedule();
assertThat(schedule.interval()).isEqualTo(60001);
assertThat(schedule.unit()).isEqualTo(TimeUnit.MILLISECONDS);
});
}
use of org.graylog.scheduler.JobTriggerDto in project graylog2-server by Graylog2.
the class EventDefinitionHandlerTest method update.
@Test
@MongoDBFixtures("event-processors.json")
public void update() {
final String newTitle = "A NEW TITLE " + DateTime.now(DateTimeZone.UTC).toString();
final String newDescription = "A NEW DESCRIPTION " + DateTime.now(DateTimeZone.UTC).toString();
final EventDefinitionDto existingDto = eventDefinitionService.get("54e3deadbeefdeadbeef0000").orElse(null);
final JobDefinitionDto existingJobDefinition = jobDefinitionService.get("54e3deadbeefdeadbeef0001").orElse(null);
final JobTriggerDto existingTrigger = jobTriggerService.get("54e3deadbeefdeadbeef0002").orElse(null);
final TestEventProcessorConfig existingConfig = (TestEventProcessorConfig) existingDto.config();
final TestEventProcessorConfig newConfig = existingConfig.toBuilder().executeEveryMs(550000).searchWithinMs(800000).build();
final EventProcessorExecutionJob.Data existingTriggerData = (EventProcessorExecutionJob.Data) existingTrigger.data().orElseThrow(AssertionError::new);
assertThat(existingDto).isNotNull();
assertThat(existingJobDefinition).isNotNull();
assertThat(existingTrigger).isNotNull();
final EventDefinitionDto updatedDto = existingDto.toBuilder().title(newTitle).description(newDescription).config(newConfig).build();
assertThat(handler.update(updatedDto, true)).isNotEqualTo(existingDto);
assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
assertThat(dto.id()).isEqualTo(existingDto.id());
assertThat(dto.title()).isEqualTo(newTitle);
assertThat(dto.description()).isEqualTo(newDescription);
});
// Test that the schedule is updated to the new config
final JobDefinitionDto newJobDefinition = jobDefinitionService.get("54e3deadbeefdeadbeef0001").orElseThrow(AssertionError::new);
assertThat(newJobDefinition.title()).isEqualTo(newTitle);
assertThat(newJobDefinition.description()).isEqualTo(newDescription);
assertThat(((EventProcessorExecutionJob.Config) newJobDefinition.config()).processingHopSize()).isEqualTo(550000);
assertThat(((EventProcessorExecutionJob.Config) newJobDefinition.config()).processingWindowSize()).isEqualTo(800000);
// Test if the EventDefinition update removed the old trigger data
// and reset the job definition timerange to the new parameters
final EventProcessorExecutionJob.Config newJobConfig = (EventProcessorExecutionJob.Config) newJobDefinition.config();
final TimeRange newTimeRange = newJobConfig.parameters().timerange();
assertThat(newTimeRange.getFrom()).isEqualTo(clock.nowUTC().minus(newConfig.searchWithinMs()));
assertThat(newTimeRange.getTo()).isEqualTo(clock.nowUTC());
assertThat(jobTriggerService.get("54e3deadbeefdeadbeef0002")).isPresent().get().satisfies(trigger -> {
assertThat(trigger.data()).isEmpty();
assertThat(trigger.nextTime()).isEqualTo(clock.nowUTC());
});
}
Aggregations