Search in sources :

Example 1 with IntervalJobSchedule

use of org.graylog.scheduler.schedule.IntervalJobSchedule in project graylog2-server by Graylog2.

the class EventDefinitionHandlerTest method schedule.

@Test
@MongoDBFixtures("event-processors-without-schedule.json")
public void schedule() {
    assertThat(eventDefinitionService.get("54e3deadbeefdeadbeef0000")).isPresent();
    assertThat(jobDefinitionService.streamAll().count()).isEqualTo(0);
    assertThat(jobTriggerService.all()).isEmpty();
    handler.schedule("54e3deadbeefdeadbeef0000");
    assertThat(eventDefinitionService.get("54e3deadbeefdeadbeef0000")).isPresent();
    assertThat(jobDefinitionService.getByConfigField("event_definition_id", "54e3deadbeefdeadbeef0000")).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(60000);
        assertThat(jobTriggerService.nextRunnableTrigger()).get().satisfies(trigger -> {
            assertThat(trigger.jobDefinitionId()).isEqualTo(definition.id());
            assertThat(trigger.schedule()).isInstanceOf(IntervalJobSchedule.class);
            final IntervalJobSchedule schedule = (IntervalJobSchedule) trigger.schedule();
            assertThat(schedule.interval()).isEqualTo(60000);
            assertThat(schedule.unit()).isEqualTo(TimeUnit.MILLISECONDS);
        });
    });
    assertThat(jobDefinitionService.get("54e3deadbeefdeadbeef0001")).isNotPresent();
    assertThat(jobTriggerService.get("54e3deadbeefdeadbeef0002")).isNotPresent();
}
Also used : TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) IntervalJobSchedule(org.graylog.scheduler.schedule.IntervalJobSchedule) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 2 with IntervalJobSchedule

use of org.graylog.scheduler.schedule.IntervalJobSchedule in project graylog2-server by Graylog2.

the class TestEventProcessorConfig method toJobSchedulerConfig.

@Override
public Optional<EventProcessorSchedulerConfig> toJobSchedulerConfig(EventDefinition eventDefinition, JobSchedulerClock clock) {
    final DateTime now = clock.nowUTC();
    // We need an initial timerange for the first execution of the event processor
    final AbsoluteRange timerange = AbsoluteRange.create(now.minus(searchWithinMs()), now);
    final EventProcessorExecutionJob.Config jobDefinitionConfig = EventProcessorExecutionJob.Config.builder().eventDefinitionId(eventDefinition.id()).processingWindowSize(searchWithinMs()).processingHopSize(executeEveryMs()).parameters(TestEventProcessorParameters.builder().timerange(timerange).build()).build();
    final IntervalJobSchedule schedule = IntervalJobSchedule.builder().interval(executeEveryMs()).unit(TimeUnit.MILLISECONDS).build();
    return Optional.of(EventProcessorSchedulerConfig.create(jobDefinitionConfig, schedule));
}
Also used : AbsoluteRange(org.graylog2.plugin.indexer.searches.timeranges.AbsoluteRange) IntervalJobSchedule(org.graylog.scheduler.schedule.IntervalJobSchedule) DateTime(org.joda.time.DateTime) EventProcessorExecutionJob(org.graylog.events.processor.EventProcessorExecutionJob)

Example 3 with IntervalJobSchedule

use of org.graylog.scheduler.schedule.IntervalJobSchedule in project graylog2-server by Graylog2.

the class AggregationEventProcessorConfigTest method toJobSchedulerConfig.

@Test
@MongoDBFixtures("aggregation-processors.json")
public void toJobSchedulerConfig() {
    final EventDefinitionDto dto = dbService.get("54e3deadbeefdeadbeefaffe").orElse(null);
    assertThat(dto).isNotNull();
    assertThat(dto.config().toJobSchedulerConfig(dto, clock)).isPresent().get().satisfies(schedulerConfig -> {
        assertThat(schedulerConfig.jobDefinitionConfig()).satisfies(jobDefinitionConfig -> {
            assertThat(jobDefinitionConfig).isInstanceOf(EventProcessorExecutionJob.Config.class);
            final EventProcessorExecutionJob.Config config = (EventProcessorExecutionJob.Config) jobDefinitionConfig;
            assertThat(config.eventDefinitionId()).isEqualTo(dto.id());
            assertThat(config.processingWindowSize()).isEqualTo(300000);
            assertThat(config.processingHopSize()).isEqualTo(300000);
            assertThat(config.parameters()).isEqualTo(AggregationEventProcessorParameters.builder().timerange(AbsoluteRange.create(clock.nowUTC().minus(300000), clock.nowUTC())).build());
        });
        assertThat(schedulerConfig.schedule()).satisfies(schedule -> {
            assertThat(schedule).isInstanceOf(IntervalJobSchedule.class);
            final IntervalJobSchedule config = (IntervalJobSchedule) schedule;
            assertThat(config.interval()).isEqualTo(300000);
            assertThat(config.unit()).isEqualTo(TimeUnit.MILLISECONDS);
        });
    });
}
Also used : EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto) IntervalJobSchedule(org.graylog.scheduler.schedule.IntervalJobSchedule) EventProcessorExecutionJob(org.graylog.events.processor.EventProcessorExecutionJob) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 4 with IntervalJobSchedule

use of org.graylog.scheduler.schedule.IntervalJobSchedule in project graylog2-server by Graylog2.

the class DBJobTriggerServiceTest method nextRunnableTriggerWithPausedCompletedAndErrorStatus.

@Test
public void nextRunnableTriggerWithPausedCompletedAndErrorStatus() {
    // No triggers yet
    assertThat(dbJobTriggerService.nextRunnableTrigger()).isEmpty();
    final IntervalJobSchedule schedule = IntervalJobSchedule.builder().interval(1).unit(TimeUnit.SECONDS).build();
    final JobTriggerDto trigger1 = dbJobTriggerService.create(JobTriggerDto.Builder.create(clock).jobDefinitionId("abc-123").nextTime(clock.nowUTC().plusSeconds(11)).schedule(schedule).build());
    // This trigger should never be fired because its status is COMPLETE
    dbJobTriggerService.create(JobTriggerDto.Builder.create(clock).jobDefinitionId("abc-123").status(JobTriggerStatus.COMPLETE).nextTime(clock.nowUTC().plusSeconds(10)).schedule(schedule).build());
    // This trigger should never be fired because its status is PAUSED
    dbJobTriggerService.create(JobTriggerDto.Builder.create(clock).jobDefinitionId("abc-123").status(JobTriggerStatus.PAUSED).nextTime(clock.nowUTC().plusSeconds(10)).schedule(schedule).build());
    // This trigger should never be fired because its status is ERROR
    dbJobTriggerService.create(JobTriggerDto.Builder.create(clock).jobDefinitionId("abc-123").status(JobTriggerStatus.ERROR).nextTime(clock.nowUTC().plusSeconds(10)).schedule(schedule).build());
    // First try is empty because the next time of the trigger is in the future
    assertThat(dbJobTriggerService.nextRunnableTrigger()).isEmpty();
    clock.plus(20, TimeUnit.SECONDS);
    // The second call should return trigger1 because the other ones are in status COMPLETE, PAUSED and ERROR
    assertThat(dbJobTriggerService.nextRunnableTrigger()).isNotEmpty().get().satisfies(trigger -> assertThat(trigger.id()).isEqualTo(trigger1.id()));
    clock.plus(20, TimeUnit.SECONDS);
    // No trigger left
    assertThat(dbJobTriggerService.nextRunnableTrigger()).isEmpty();
}
Also used : IntervalJobSchedule(org.graylog.scheduler.schedule.IntervalJobSchedule) Test(org.junit.Test)

Example 5 with IntervalJobSchedule

use of org.graylog.scheduler.schedule.IntervalJobSchedule in project graylog2-server by Graylog2.

the class CreateJobTriggerRequestTest method toDto.

@Test
public void toDto() {
    final DateTime now = DateTime.now(UTC);
    final JobSchedulerTestClock clock = new JobSchedulerTestClock(now);
    final IntervalJobSchedule schedule = IntervalJobSchedule.builder().interval(1).unit(TimeUnit.SECONDS).build();
    final CreateJobTriggerRequest request = CreateJobTriggerRequest.builder().jobDefinitionId("abc-123").startTime(now).nextTime(now).schedule(schedule).build();
    final JobTriggerData.FallbackData data = new JobTriggerData.FallbackData();
    final CreateJobTriggerRequest requestWithDataAndEndTime = CreateJobTriggerRequest.builder().jobDefinitionId("abc-123").startTime(now).endTime(now.plusDays(1)).nextTime(now).schedule(schedule).data(data).build();
    assertThat(request.toDto(clock)).satisfies(dto -> {
        assertThat(dto.jobDefinitionId()).isEqualTo("abc-123");
        assertThat(dto.startTime()).isEqualTo(now);
        assertThat(dto.endTime()).isNotPresent();
        assertThat(dto.nextTime()).isEqualTo(now);
        assertThat(dto.createdAt()).isEqualTo(now);
        assertThat(dto.updatedAt()).isEqualTo(now);
        assertThat(dto.triggeredAt()).isNotPresent();
        assertThat(dto.status()).isEqualTo(JobTriggerStatus.RUNNABLE);
        assertThat(dto.lock()).isEqualTo(JobTriggerLock.empty());
        assertThat(dto.schedule()).isEqualTo(schedule);
        assertThat(dto.data()).isNotPresent();
    });
    assertThat(requestWithDataAndEndTime.toDto(clock)).satisfies(dto -> {
        assertThat(dto.jobDefinitionId()).isEqualTo("abc-123");
        assertThat(dto.startTime()).isEqualTo(now);
        assertThat(dto.endTime()).isPresent().get().isEqualTo(now.plusDays(1));
        assertThat(dto.nextTime()).isEqualTo(now);
        assertThat(dto.createdAt()).isEqualTo(now);
        assertThat(dto.updatedAt()).isEqualTo(now);
        assertThat(dto.triggeredAt()).isNotPresent();
        assertThat(dto.status()).isEqualTo(JobTriggerStatus.RUNNABLE);
        assertThat(dto.lock()).isEqualTo(JobTriggerLock.empty());
        assertThat(dto.schedule()).isEqualTo(schedule);
        assertThat(dto.data()).isPresent().get().isEqualTo(data);
    });
}
Also used : JobSchedulerTestClock(org.graylog.events.JobSchedulerTestClock) IntervalJobSchedule(org.graylog.scheduler.schedule.IntervalJobSchedule) JobTriggerData(org.graylog.scheduler.JobTriggerData) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

IntervalJobSchedule (org.graylog.scheduler.schedule.IntervalJobSchedule)8 Test (org.junit.Test)6 TestEventProcessorConfig (org.graylog.events.TestEventProcessorConfig)3 EventProcessorExecutionJob (org.graylog.events.processor.EventProcessorExecutionJob)3 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)3 DateTime (org.joda.time.DateTime)3 JobDefinitionDto (org.graylog.scheduler.JobDefinitionDto)2 AbsoluteRange (org.graylog2.plugin.indexer.searches.timeranges.AbsoluteRange)2 JobSchedulerTestClock (org.graylog.events.JobSchedulerTestClock)1 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)1 JobTriggerData (org.graylog.scheduler.JobTriggerData)1 JobTriggerDto (org.graylog.scheduler.JobTriggerDto)1