Search in sources :

Example 6 with JobDefinitionDto

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();
}
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 JobDefinitionDto

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();
}
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 JobDefinitionDto

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;
}
Also used : JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException)

Example 9 with JobDefinitionDto

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;
    }
}
Also used : JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto)

Example 10 with JobDefinitionDto

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;
}
Also used : JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto)

Aggregations

JobDefinitionDto (org.graylog.scheduler.JobDefinitionDto)23 JobTriggerDto (org.graylog.scheduler.JobTriggerDto)15 Test (org.junit.Test)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 TestEventProcessorParameters (org.graylog.events.TestEventProcessorParameters)8 EventProcessorExecutionJob (org.graylog.events.processor.EventProcessorExecutionJob)8 JobExecutionContext (org.graylog.scheduler.JobExecutionContext)8 JobTriggerUpdates (org.graylog.scheduler.JobTriggerUpdates)8 DateTime (org.joda.time.DateTime)8 JobTriggerUpdate (org.graylog.scheduler.JobTriggerUpdate)7 TestEventProcessorConfig (org.graylog.events.TestEventProcessorConfig)4 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)4 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotFoundException (javax.ws.rs.NotFoundException)2 NotificationDto (org.graylog.events.notifications.NotificationDto)2 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)2 IntervalJobSchedule (org.graylog.scheduler.schedule.IntervalJobSchedule)2 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)2 PasswordAlgorithmFactory (org.graylog2.security.PasswordAlgorithmFactory)2 Permissions (org.graylog2.shared.security.Permissions)2