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