Search in sources :

Example 11 with Event

use of io.gravitee.repository.management.model.Event in project gravitee-management-rest-api by gravitee-io.

the class SyncManager method getLastEvent.

private Event getLastEvent(String api) {
    EventCriteria eventCriteria;
    if (lastRefreshAt == -1) {
        eventCriteria = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api).build();
    } else {
        eventCriteria = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api).from(lastRefreshAt).to(System.currentTimeMillis()).build();
    }
    List<Event> events = eventRepository.search(eventCriteria, new PageableBuilder().pageNumber(0).pageSize(1).build()).getContent();
    return (!events.isEmpty()) ? events.get(0) : null;
}
Also used : Event(io.gravitee.repository.management.model.Event) EventCriteria(io.gravitee.repository.management.api.search.EventCriteria) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder)

Example 12 with Event

use of io.gravitee.repository.management.model.Event in project gravitee-management-rest-api by gravitee-io.

the class EventServiceImpl method findById.

@Override
public EventEntity findById(String id) {
    try {
        LOGGER.debug("Find event by ID: {}", id);
        Optional<Event> event = eventRepository.findById(id);
        if (event.isPresent()) {
            return convert(event.get());
        }
        throw new EventNotFoundException(id);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find an event using its ID {}", id, ex);
        throw new TechnicalManagementException("An error occurs while trying to find an event using its ID " + id, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Event(io.gravitee.repository.management.model.Event) EventNotFoundException(io.gravitee.management.service.exceptions.EventNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 13 with Event

use of io.gravitee.repository.management.model.Event in project gravitee-management-rest-api by gravitee-io.

the class EventServiceImpl method create.

@Override
public EventEntity create(NewEventEntity newEventEntity) {
    String hostAddress = "";
    try {
        hostAddress = InetAddress.getLocalHost().getHostAddress();
        LOGGER.debug("Create {} for server {}", newEventEntity, hostAddress);
        Event event = convert(newEventEntity);
        event.setId(io.gravitee.common.utils.UUID.random().toString());
        // Set origin
        event.getProperties().put(Event.EventProperties.ORIGIN.getValue(), hostAddress);
        // Set date fields
        event.setCreatedAt(new Date());
        event.setUpdatedAt(event.getCreatedAt());
        Event createdEvent = eventRepository.create(event);
        return convert(createdEvent);
    } catch (UnknownHostException e) {
        LOGGER.error("An error occurs while getting the server IP address", e);
        throw new TechnicalManagementException("An error occurs while getting the server IP address", e);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create {} for server {}", newEventEntity, hostAddress, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + newEventEntity + " for server " + hostAddress, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UnknownHostException(java.net.UnknownHostException) Event(io.gravitee.repository.management.model.Event) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 14 with Event

use of io.gravitee.repository.management.model.Event in project gravitee-gateway by gravitee-io.

the class MonitoringService method doStart.

@Override
protected void doStart() throws Exception {
    if (enabled) {
        super.doStart();
        LOGGER.info("Start gateway monitor");
        Event evt = prepareEvent();
        LOGGER.debug("Sending a {} event", evt.getType());
        heartbeatEvent = eventRepository.create(evt);
        executorService = Executors.newSingleThreadScheduledExecutor(r -> new Thread(r, "gateway-monitor"));
        MonitorThread monitorThread = new MonitorThread(heartbeatEvent);
        this.applicationContext.getAutowireCapableBeanFactory().autowireBean(monitorThread);
        LOGGER.info("Monitoring scheduled with fixed delay {} {} ", delay, unit.name());
        ((ScheduledExecutorService) executorService).scheduleWithFixedDelay(monitorThread, 0, delay, unit);
        LOGGER.info("Associate a new HTTP handler on {}", PATH);
        // Create and associate handler
        MonitorHandler monitorHandler = new MonitorHandler();
        applicationContext.getAutowireCapableBeanFactory().autowireBean(monitorHandler);
        router.get(PATH).produces(MediaType.APPLICATION_JSON).handler(monitorHandler);
        LOGGER.info("Start gateway monitor : DONE");
    }
}
Also used : Plugin(io.gravitee.gateway.services.monitoring.event.Plugin) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) Router(io.vertx.ext.web.Router) Autowired(org.springframework.beans.factory.annotation.Autowired) MonitorHandler(io.gravitee.gateway.services.monitoring.handler.MonitorHandler) InstanceEventPayload(io.gravitee.gateway.services.monitoring.event.InstanceEventPayload) Value(org.springframework.beans.factory.annotation.Value) InetAddress(java.net.InetAddress) EventType(io.gravitee.repository.management.model.EventType) UUID(io.gravitee.common.utils.UUID) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) EventRepository(io.gravitee.repository.management.api.EventRepository) Logger(org.slf4j.Logger) Event(io.gravitee.repository.management.model.Event) Version(io.gravitee.common.util.Version) PluginRegistry(io.gravitee.plugin.core.api.PluginRegistry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AbstractService(io.gravitee.common.service.AbstractService) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) MediaType(io.gravitee.common.http.MediaType) Node(io.gravitee.common.node.Node) GatewayConfiguration(io.gravitee.gateway.env.GatewayConfiguration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MonitorHandler(io.gravitee.gateway.services.monitoring.handler.MonitorHandler) Event(io.gravitee.repository.management.model.Event)

Example 15 with Event

use of io.gravitee.repository.management.model.Event in project gravitee-gateway by gravitee-io.

the class MonitoringService method prepareEvent.

private Event prepareEvent() {
    Event event = new Event();
    event.setId(UUID.random().toString());
    event.setType(EventType.GATEWAY_STARTED);
    event.setCreatedAt(new Date());
    event.setUpdatedAt(event.getCreatedAt());
    Map<String, String> properties = new HashMap<>();
    properties.put("id", node.id());
    properties.put("started_at", Long.toString(event.getCreatedAt().getTime()));
    properties.put("last_heartbeat_at", Long.toString(event.getCreatedAt().getTime()));
    event.setProperties(properties);
    InstanceEventPayload instance = createInstanceInfo();
    try {
        String payload = objectMapper.writeValueAsString(instance);
        event.setPayload(payload);
    } catch (JsonProcessingException jsex) {
        LOGGER.error("An error occurs while transforming instance information into JSON", jsex);
    }
    return event;
}
Also used : InstanceEventPayload(io.gravitee.gateway.services.monitoring.event.InstanceEventPayload) Event(io.gravitee.repository.management.model.Event) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

Event (io.gravitee.repository.management.model.Event)25 EventCriteria (io.gravitee.repository.management.api.search.EventCriteria)19 Api (io.gravitee.gateway.handlers.api.definition.Api)16 RepositoryApiBuilder (io.gravitee.gateway.services.sync.builder.RepositoryApiBuilder)15 Pageable (io.gravitee.repository.management.api.search.Pageable)14 Test (org.junit.Test)14 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)4 Instant (java.time.Instant)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 EventRepository (io.gravitee.repository.management.api.EventRepository)3 EventType (io.gravitee.repository.management.model.EventType)3 Collectors (java.util.stream.Collectors)3 ArgumentMatcher (org.mockito.ArgumentMatcher)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 GatewayConfiguration (io.gravitee.gateway.env.GatewayConfiguration)2 InstanceEventPayload (io.gravitee.gateway.services.monitoring.event.InstanceEventPayload)2