use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.
the class EventDefinitionFacadeTest method createNativeEntity.
@Test
public void createNativeEntity() {
final EntityV1 entityV1 = createTestEntity();
final NotificationDto notificationDto = NotificationDto.builder().config(HTTPEventNotificationConfig.builder().url("https://hulud.net").build()).title("Notify me Senpai").description("A notification for senpai").id("dead-beef").build();
final EntityDescriptor entityDescriptor = EntityDescriptor.create("123123", ModelTypes.NOTIFICATION_V1);
final ImmutableMap<EntityDescriptor, Object> nativeEntities = ImmutableMap.of(entityDescriptor, notificationDto);
final JobDefinitionDto jobDefinitionDto = mock(JobDefinitionDto.class);
final JobTriggerDto jobTriggerDto = mock(JobTriggerDto.class);
when(jobDefinitionDto.id()).thenReturn("job-123123");
when(jobSchedulerClock.nowUTC()).thenReturn(DateTime.now(DateTimeZone.UTC));
when(jobDefinitionService.save(any(JobDefinitionDto.class))).thenReturn(jobDefinitionDto);
when(jobTriggerService.create(any(JobTriggerDto.class))).thenReturn(jobTriggerDto);
final UserImpl kmerzUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "kmerz"));
when(userService.load("kmerz")).thenReturn(kmerzUser);
final NativeEntity<EventDefinitionDto> nativeEntity = facade.createNativeEntity(entityV1, ImmutableMap.of(), nativeEntities, "kmerz");
assertThat(nativeEntity).isNotNull();
final EventDefinitionDto eventDefinitionDto = nativeEntity.entity();
assertThat(eventDefinitionDto.title()).isEqualTo("title");
assertThat(eventDefinitionDto.description()).isEqualTo("description");
assertThat(eventDefinitionDto.config().type()).isEqualTo("aggregation-v1");
// verify that ownership was registered for this entity
verify(entityOwnershipService, times(1)).registerNewEventDefinition(nativeEntity.entity().id(), kmerzUser);
}
use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.
the class EventProcessorExecutionJobTest method executeWithTimerangeInTheFuture.
@Test
public void executeWithTimerangeInTheFuture() 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;
// Set the "to" timestamp of the timerange way into the future
final DateTime to = now.plusDays(1);
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);
// The engine should not be called because the "to" timestamp of the timerange is in the future
verify(eventProcessorEngine, never()).execute(any(), any());
// The update sets the nextTime to the "to" timestamp because that is the earliest time we can execute
// the job for the timerange
assertThat(triggerUpdate.nextTime()).isPresent().get().isEqualTo(to);
// Data should not be updated with any new timerange
assertThat(triggerUpdate.data()).isNotPresent();
assertThat(triggerUpdate.status()).isNotPresent();
}
use of org.graylog.scheduler.JobDefinitionDto 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.JobDefinitionDto 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.JobDefinitionDto 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();
}
Aggregations