Search in sources :

Example 6 with JobTriggerUpdate

use of org.graylog.scheduler.JobTriggerUpdate 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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) JobTriggerUpdates(org.graylog.scheduler.JobTriggerUpdates) JobTriggerUpdate(org.graylog.scheduler.JobTriggerUpdate) JobExecutionContext(org.graylog.scheduler.JobExecutionContext) DateTime(org.joda.time.DateTime) TestEventProcessorParameters(org.graylog.events.TestEventProcessorParameters) EventProcessorExecutionJob(org.graylog.events.processor.EventProcessorExecutionJob) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 7 with JobTriggerUpdate

use of org.graylog.scheduler.JobTriggerUpdate 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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) JobTriggerUpdates(org.graylog.scheduler.JobTriggerUpdates) JobTriggerUpdate(org.graylog.scheduler.JobTriggerUpdate) JobExecutionContext(org.graylog.scheduler.JobExecutionContext) DateTime(org.joda.time.DateTime) TestEventProcessorParameters(org.graylog.events.TestEventProcessorParameters) EventProcessorExecutionJob(org.graylog.events.processor.EventProcessorExecutionJob) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 8 with JobTriggerUpdate

use of org.graylog.scheduler.JobTriggerUpdate 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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) JobTriggerUpdates(org.graylog.scheduler.JobTriggerUpdates) JobTriggerUpdate(org.graylog.scheduler.JobTriggerUpdate) JobExecutionContext(org.graylog.scheduler.JobExecutionContext) DateTime(org.joda.time.DateTime) TestEventProcessorParameters(org.graylog.events.TestEventProcessorParameters) EventProcessorExecutionJob(org.graylog.events.processor.EventProcessorExecutionJob) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Aggregations

JobTriggerUpdate (org.graylog.scheduler.JobTriggerUpdate)8 DateTime (org.joda.time.DateTime)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 TestEventProcessorParameters (org.graylog.events.TestEventProcessorParameters)7 EventProcessorExecutionJob (org.graylog.events.processor.EventProcessorExecutionJob)7 JobDefinitionDto (org.graylog.scheduler.JobDefinitionDto)7 JobExecutionContext (org.graylog.scheduler.JobExecutionContext)7 JobTriggerDto (org.graylog.scheduler.JobTriggerDto)7 JobTriggerUpdates (org.graylog.scheduler.JobTriggerUpdates)7 Test (org.junit.Test)6 JobExecutionException (org.graylog.scheduler.JobExecutionException)1 JobTriggerData (org.graylog.scheduler.JobTriggerData)1 Duration (org.joda.time.Duration)1