Search in sources :

Example 16 with JobTriggerDto

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

the class EventDefinitionHandlerTest method updateWithErrors.

@Test
@MongoDBFixtures("event-processors.json")
public void updateWithErrors() {
    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);
    assertThat(existingDto).isNotNull();
    assertThat(existingJobDefinition).isNotNull();
    assertThat(existingTrigger).isNotNull();
    final EventDefinitionDto updatedDto = existingDto.toBuilder().title(newTitle).description(newDescription).build();
    doThrow(new NullPointerException("yolo1")).when(eventDefinitionService).save(any());
    assertThatCode(() -> handler.update(updatedDto, true)).isInstanceOf(NullPointerException.class).hasMessageContaining("yolo1");
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(existingDto.title());
        assertThat(dto.description()).isEqualTo(existingDto.description());
    });
    assertThat(jobDefinitionService.get("54e3deadbeefdeadbeef0001")).isPresent().get().satisfies(definition -> {
        assertThat(definition.title()).isEqualTo(existingJobDefinition.title());
        assertThat(definition.description()).isEqualTo(existingJobDefinition.description());
    });
    // Reset all before doing new stubs
    reset(eventDefinitionService);
    reset(jobDefinitionService);
    reset(jobTriggerService);
    doThrow(new NullPointerException("yolo2")).when(jobDefinitionService).save(any());
    assertThatCode(() -> handler.update(updatedDto, true)).isInstanceOf(NullPointerException.class).hasMessageContaining("yolo2");
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(existingDto.title());
        assertThat(dto.description()).isEqualTo(existingDto.description());
    });
    assertThat(jobDefinitionService.get("54e3deadbeefdeadbeef0001")).isPresent().get().satisfies(definition -> {
        assertThat(definition.title()).isEqualTo(existingJobDefinition.title());
        assertThat(definition.description()).isEqualTo(existingJobDefinition.description());
    });
    // Reset all before doing new stubs
    reset(eventDefinitionService);
    reset(jobDefinitionService);
    reset(jobTriggerService);
    doThrow(new NullPointerException("yolo3")).when(jobTriggerService).update(any());
    assertThatCode(() -> handler.update(updatedDto, true)).isInstanceOf(NullPointerException.class).hasMessageContaining("yolo3");
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(existingDto.title());
        assertThat(dto.description()).isEqualTo(existingDto.description());
    });
    assertThat(jobDefinitionService.get("54e3deadbeefdeadbeef0001")).isPresent().get().satisfies(definition -> {
        assertThat(definition.title()).isEqualTo(existingJobDefinition.title());
        assertThat(definition.description()).isEqualTo(existingJobDefinition.description());
    });
}
Also used : JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 17 with JobTriggerDto

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

the class EventDefinitionHandler method deleteJobTrigger.

private void deleteJobTrigger(JobDefinitionDto jobDefinition, EventDefinitionDto eventDefinition) {
    final Optional<JobTriggerDto> optionalJobTrigger = getJobTrigger(jobDefinition);
    if (!optionalJobTrigger.isPresent()) {
        return;
    }
    final JobTriggerDto jobTrigger = optionalJobTrigger.get();
    LOG.debug("Deleting scheduler job trigger <{}> for job definition <{}/{}> and event definition <{}/{}>", jobTrigger.id(), jobDefinition.id(), jobDefinition.title(), eventDefinition.id(), eventDefinition.title());
    jobTriggerService.delete(jobTrigger.id());
}
Also used : JobTriggerDto(org.graylog.scheduler.JobTriggerDto)

Example 18 with JobTriggerDto

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

the class EventDefinitionHandler method updateJobTrigger.

private void updateJobTrigger(EventDefinitionDto eventDefinition, JobDefinitionDto jobDefinition, JobDefinitionDto oldJobDefinition, EventProcessorSchedulerConfig schedulerConfig) {
    final Optional<JobTriggerDto> optionalOldJobTrigger = getJobTrigger(jobDefinition);
    if (!optionalOldJobTrigger.isPresent()) {
        // Nothing to do if there are no job triggers to update
        return;
    }
    final JobTriggerDto oldJobTrigger = optionalOldJobTrigger.get();
    // Update the existing object to make sure we keep the ID
    final JobTriggerDto.Builder unsavedJobTriggerBuilder = oldJobTrigger.toBuilder().jobDefinitionId(requireNonNull(jobDefinition.id(), "Job definition ID cannot be null")).schedule(schedulerConfig.schedule()).nextTime(clock.nowUTC());
    final EventProcessorExecutionJob.Config oldConfig = (EventProcessorExecutionJob.Config) oldJobDefinition.config();
    final EventProcessorExecutionJob.Config config = (EventProcessorExecutionJob.Config) jobDefinition.config();
    // If necessary, reset the scheduling times
    if (!config.hasEqualSchedule(oldConfig)) {
        // jobDefinition has a newly created scheduling timerange.
        // Wipe the old one so EventProcessorExecutionJob.execute()
        // will fall back to the new one from the JobDefinition.
        unsavedJobTriggerBuilder.data(null);
        // schedule the next execution accordingly
        unsavedJobTriggerBuilder.nextTime(config.parameters().timerange().getTo());
    }
    final JobTriggerDto jobTrigger = unsavedJobTriggerBuilder.build();
    jobTriggerService.update(jobTrigger);
    LOG.debug("Updated scheduler job trigger <{}> for job definition <{}/{}> and event definition <{}/{}>", jobTrigger.id(), jobDefinition.id(), jobDefinition.title(), eventDefinition.id(), eventDefinition.title());
}
Also used : JobTriggerDto(org.graylog.scheduler.JobTriggerDto)

Example 19 with JobTriggerDto

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

the class EventNotificationHandler method handleEvents.

public void handleEvents(EventDefinition definition, List<EventWithContext> eventsWithContext) {
    for (Config config : definition.notifications()) {
        final Optional<JobDefinitionDto> jobDefinition = jobDefinitionService.getByConfigField(Config.FIELD_NOTIFICATION_ID, config.notificationId());
        if (!jobDefinition.isPresent()) {
            LOG.error("Couldn't find job definition for notification <{}>", config.notificationId());
            continue;
        }
        final Optional<NotificationDto> notificationDto = notificationService.get(config.notificationId());
        if (!notificationDto.isPresent()) {
            LOG.error("Couldn't find notification definition for id <{}>", config.notificationId());
            continue;
        }
        final EventNotificationConfig notificationConfig = notificationDto.get().config();
        for (EventWithContext eventWithContext : eventsWithContext) {
            final Event event = eventWithContext.event();
            if (notificationGracePeriodService.inGracePeriod(definition, config.notificationId(), event)) {
                continue;
            }
            try {
                final JobTriggerDto trigger = jobTriggerService.create(JobTriggerDto.builder().jobDefinitionId(jobDefinition.get().id()).schedule(OnceJobSchedule.create()).data(notificationConfig.toJobTriggerData(event.toDto())).build());
                LOG.debug("Scheduled job <{}> for notification <{}> - event <{}/{}>", trigger.id(), config.notificationId(), event.getId(), event.getMessage());
            // TODO: The trigger ID needs to be added to the "triggered_tasks" list of the event
            } catch (Exception e) {
                LOG.error("Couldn't create job trigger for notification <{}> and event: {}", config.notificationId(), event, e);
            }
        }
    }
}
Also used : JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) Event(org.graylog.events.event.Event) EventWithContext(org.graylog.events.event.EventWithContext) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) NotFoundException(org.graylog2.database.NotFoundException)

Aggregations

JobTriggerDto (org.graylog.scheduler.JobTriggerDto)19 JobDefinitionDto (org.graylog.scheduler.JobDefinitionDto)15 Test (org.junit.Test)12 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)3 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)3 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)3 JobExecutionException (org.graylog.scheduler.JobExecutionException)2 NotFoundException (org.graylog2.database.NotFoundException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 List (java.util.List)1 Event (org.graylog.events.event.Event)1 EventDto (org.graylog.events.event.EventDto)1 EventWithContext (org.graylog.events.event.EventWithContext)1