Search in sources :

Example 21 with JobDefinitionDto

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

Example 22 with JobDefinitionDto

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

Example 23 with JobDefinitionDto

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

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