use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.
the class NotificationResourceHandler method create.
/**
* Creates a new notification definition and a corresponding scheduler job definition.
*
* @param unsavedDto the notification definition to save
* @param user
* @return the created event definition
*/
public NotificationDto create(NotificationDto unsavedDto, Optional<User> user) {
final NotificationDto dto;
if (user.isPresent()) {
dto = notificationService.saveWithOwnership(unsavedDto, user.get());
LOG.debug("Created notification definition <{}/{}> with user <{}>", dto.id(), dto.title(), user.get());
} else {
dto = notificationService.save(unsavedDto);
LOG.debug("Created notification definition <{}/{}> without user", dto.id(), dto.title());
}
try {
final JobDefinitionDto unsavedJobDefinition = JobDefinitionDto.builder().title(dto.title()).description(dto.description()).config(getSchedulerConfig(dto.id())).build();
JobDefinitionDto jobDefinition = jobDefinitionService.save(unsavedJobDefinition);
LOG.debug("Created scheduler job definition <{}/{}> for notification <{}/{}>", jobDefinition.id(), jobDefinition.title(), dto.id(), dto.title());
} catch (Exception e) {
LOG.error("Failed to create job definition for notification <{}/{}>", dto.id(), dto.title(), e);
throw e;
}
return dto;
}
use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.
the class EventDefinitionHandler method updateJobDefinition.
private JobDefinitionDto updateJobDefinition(EventDefinitionDto eventDefinition, JobDefinitionDto oldJobDefinition, EventProcessorSchedulerConfig schedulerConfig) {
// Update the existing object to make sure we keep the ID
final JobDefinitionDto unsavedJobDefinition = oldJobDefinition.toBuilder().title(eventDefinition.title()).description(eventDefinition.description()).config(schedulerConfig.jobDefinitionConfig()).build();
final JobDefinitionDto jobDefinition = jobDefinitionService.save(unsavedJobDefinition);
LOG.debug("Updated scheduler job definition <{}/{}> for event definition <{}/{}>", jobDefinition.id(), jobDefinition.title(), eventDefinition.id(), eventDefinition.title());
return jobDefinition;
}
use of org.graylog.scheduler.JobDefinitionDto 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