Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eagerly start and initialize EventsService before EventSourcedRegistryStorage #3519

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,30 @@
import io.apicurio.registry.storage.dto.StoredArtifactDto;
import io.apicurio.registry.types.ArtifactState;
import io.apicurio.registry.types.RuleType;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

/**
* @author Fabian Martinez
*/
@ApplicationScoped
public class EventSourcedRegistryStorage extends RegistryStorageDecorator {

@Inject
EventsService eventsService;
final Logger log;

final EventsService eventsService;

// Need to have an eager evaluation of the EventsService implementation
EventSourcedRegistryStorage(EventsService eventService, Logger log) {
this.log = log;
this.eventsService = eventService;
}

private void fireEvent(RegistryEventType type, String artifactId, Object data, Throwable error) {
if (error == null && data != null) {
Expand All @@ -73,6 +80,10 @@ private void fireEvent(RegistryEventType type, String artifactId, Object data, T
*/
@Override
public boolean isEnabled() {
if (!eventsService.isReady()) {
throw new RuntimeException("Events Service not configured, please report this as a bug.");
}
log.info("Events service is configured: " + eventsService.isConfigured());
return eventsService.isConfigured();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
public interface EventsService {

boolean isReady();

boolean isConfigured();

void triggerEvent(RegistryEventType type, Optional<String> artifactId, Object data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import java.util.Optional;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
Expand All @@ -27,7 +27,6 @@

import org.slf4j.Logger;
import io.apicurio.registry.events.dto.RegistryEventType;
import io.quarkus.runtime.StartupEvent;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.DeliveryOptions;
Expand All @@ -42,6 +41,7 @@ public class EventsServiceImpl implements EventsService {
private static final String INTERNAL_EVENTS_ADDRESS = "registry-events";

private ObjectMapper mapper;
private boolean initDone = false;
private boolean configuredSinks = false;

@Inject
Expand All @@ -56,14 +56,21 @@ public class EventsServiceImpl implements EventsService {
@Inject
Instance<EventSink> sinks;

public void init(@Observes StartupEvent ev) {
@PostConstruct
public void init() {
for (EventSink sink : sinks) {
if (sink.isConfigured()) {
log.info("Subscribing sink " + sink.name());
eventBus.consumer(INTERNAL_EVENTS_ADDRESS, sink::handle);
configuredSinks = true;
}
}
initDone = true;
}

@Override
public boolean isReady() {
return initDone;
}

@Override
Expand Down