Search in sources :

Example 6 with JobTriggerDto

use of org.graylog.scheduler.JobTriggerDto 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 7 with JobTriggerDto

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

the class EventNotificationExecutionJob method execute.

@Override
public JobTriggerUpdate execute(JobExecutionContext ctx) throws JobExecutionException {
    Optional<EventDefinitionDto> optionalEventDefinition;
    long gracePeriodInMS = 0;
    final JobTriggerDto trigger = ctx.trigger();
    final Optional<Data> optionalData = trigger.data().map(d -> (Data) d);
    if (!optionalData.isPresent()) {
        throw new JobExecutionException("Missing notification job data for notification <" + jobConfig.notificationId() + ">, unable to execute notification: " + ctx.definition().title(), trigger, JobTriggerUpdate.withoutNextTime());
    }
    final Data data = optionalData.get();
    final EventDto eventDto = data.eventDto();
    final NotificationDto notification = notificationService.get(jobConfig.notificationId()).orElseThrow(() -> new JobExecutionException("Couldn't find notification <" + jobConfig.notificationId() + ">", trigger, JobTriggerUpdate.withError(trigger)));
    final EventNotification.Factory eventNotificationFactory = eventNotificationFactories.get(notification.config().type());
    if (eventNotificationFactory == null) {
        throw new JobExecutionException("Couldn't find factory for notification type <" + notification.config().type() + ">", trigger, ctx.jobTriggerUpdates().scheduleNextExecution());
    }
    final EventNotification eventNotification = eventNotificationFactory.create();
    metrics.registerEventNotification(eventNotification, notification);
    try {
        optionalEventDefinition = Optional.ofNullable(getEventDefinition(eventDto));
        if (optionalEventDefinition.isPresent()) {
            gracePeriodInMS = optionalEventDefinition.get().notificationSettings().gracePeriodMs();
        }
    } catch (NotFoundException e) {
        LOG.error("Couldn't find event definition with ID <{}>.", eventDto.eventDefinitionId());
        optionalEventDefinition = Optional.empty();
    }
    EventNotificationContext notificationContext = EventNotificationContext.builder().notificationId(notification.id()).notificationConfig(notification.config()).event(eventDto).eventDefinition(optionalEventDefinition.get()).jobTrigger(trigger).build();
    updateTriggerStatus(eventDto, gracePeriodInMS);
    if (inGrace(eventDto, gracePeriodInMS)) {
        LOG.debug("Notification <{}> triggered but it's in grace period.", jobConfig.notificationId());
        metrics.markInGrace(eventNotification, notification);
        return ctx.jobTriggerUpdates().scheduleNextExecution();
    }
    try {
        metrics.markExecution(eventNotification, notification);
        eventNotification.execute(notificationContext);
        metrics.markSuccess(eventNotification, notification);
    } catch (TemporaryEventNotificationException e) {
        metrics.markFailedTemporarily(eventNotification, notification);
        final long retryPeriod = configurationProvider.get().eventNotificationsRetry();
        throw new JobExecutionException(String.format(Locale.ROOT, "Failed to execute notification, retrying in %d minutes - <%s/%s/%s>", TimeUnit.MILLISECONDS.toMinutes(retryPeriod), notification.id(), notification.title(), notification.config().type()), trigger, ctx.jobTriggerUpdates().retryIn(retryPeriod, TimeUnit.MILLISECONDS), e);
    } catch (PermanentEventNotificationException e) {
        metrics.markFailedPermanently(eventNotification, notification);
        throw new JobExecutionException(String.format(Locale.ROOT, "Failed permanently to execute notification, giving up - <%s/%s/%s>", notification.id(), notification.title(), notification.config().type()), trigger, ctx.jobTriggerUpdates().scheduleNextExecution(), e);
    } catch (EventNotificationException e) {
        metrics.markFailed(eventNotification, notification);
        throw new JobExecutionException(String.format(Locale.ROOT, "Notification failed to execute - <%s/%s/%s>", notification.id(), notification.title(), notification.config().type()), trigger, ctx.jobTriggerUpdates().scheduleNextExecution(), e);
    }
    updateNotifiedStatus(eventDto, gracePeriodInMS);
    return ctx.jobTriggerUpdates().scheduleNextExecution();
}
Also used : EventDto(org.graylog.events.event.EventDto) NotFoundException(org.graylog2.database.NotFoundException) JobTriggerData(org.graylog.scheduler.JobTriggerData) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) JobExecutionException(org.graylog.scheduler.JobExecutionException) EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto)

Example 8 with JobTriggerDto

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

the class EventDefinitionHandler method createJobTrigger.

private void createJobTrigger(EventDefinitionDto dto, JobDefinitionDto jobDefinition, EventProcessorSchedulerConfig schedulerConfig) {
    final JobTriggerDto jobTrigger = jobTriggerService.create(newJobTrigger(jobDefinition, schedulerConfig));
    LOG.debug("Created job trigger <{}> for job definition <{}/{}> and event definition <{}/{}>", jobTrigger.id(), jobDefinition.id(), jobDefinition.title(), dto.id(), dto.title());
}
Also used : JobTriggerDto(org.graylog.scheduler.JobTriggerDto)

Example 9 with JobTriggerDto

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

the class EventDefinitionFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() {
    final EntityV1 entityV1 = createTestEntity();
    final NotificationDto notificationDto = NotificationDto.builder().config(HTTPEventNotificationConfig.builder().url("https://hulud.net").build()).title("Notify me Senpai").description("A notification for senpai").id("dead-beef").build();
    final EntityDescriptor entityDescriptor = EntityDescriptor.create("123123", ModelTypes.NOTIFICATION_V1);
    final ImmutableMap<EntityDescriptor, Object> nativeEntities = ImmutableMap.of(entityDescriptor, notificationDto);
    final JobDefinitionDto jobDefinitionDto = mock(JobDefinitionDto.class);
    final JobTriggerDto jobTriggerDto = mock(JobTriggerDto.class);
    when(jobDefinitionDto.id()).thenReturn("job-123123");
    when(jobSchedulerClock.nowUTC()).thenReturn(DateTime.now(DateTimeZone.UTC));
    when(jobDefinitionService.save(any(JobDefinitionDto.class))).thenReturn(jobDefinitionDto);
    when(jobTriggerService.create(any(JobTriggerDto.class))).thenReturn(jobTriggerDto);
    final UserImpl kmerzUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "kmerz"));
    when(userService.load("kmerz")).thenReturn(kmerzUser);
    final NativeEntity<EventDefinitionDto> nativeEntity = facade.createNativeEntity(entityV1, ImmutableMap.of(), nativeEntities, "kmerz");
    assertThat(nativeEntity).isNotNull();
    final EventDefinitionDto eventDefinitionDto = nativeEntity.entity();
    assertThat(eventDefinitionDto.title()).isEqualTo("title");
    assertThat(eventDefinitionDto.description()).isEqualTo("description");
    assertThat(eventDefinitionDto.config().type()).isEqualTo("aggregation-v1");
    // verify that ownership was registered for this entity
    verify(entityOwnershipService, times(1)).registerNewEventDefinition(nativeEntity.entity().id(), kmerzUser);
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) NotificationDto(org.graylog.events.notifications.NotificationDto) EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 10 with JobTriggerDto

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

the class EventProcessorExecutionJobTest method executeWithTimerangeInTheFuture.

@Test
public void executeWithTimerangeInTheFuture() 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;
    // Set the "to" timestamp of the timerange way into the future
    final DateTime to = now.plusDays(1);
    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();
    final JobTriggerUpdate triggerUpdate = job.execute(jobExecutionContext);
    // The engine should not be called because the "to" timestamp of the timerange is in the future
    verify(eventProcessorEngine, never()).execute(any(), any());
    // The update sets the nextTime to the "to" timestamp because that is the earliest time we can execute
    // the job for the timerange
    assertThat(triggerUpdate.nextTime()).isPresent().get().isEqualTo(to);
    // Data should not be updated with any new timerange
    assertThat(triggerUpdate.data()).isNotPresent();
    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)

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