fix: make EmulatorLifecycle use more idiomatic Quarkus code (#190)

This commit is contained in:
Georgios Andrianakis authored and GitHub committed 2026-04-06 11:13:34 -05:00
1 parent cd1759aebe
commit 7ea586e446
1 file changed
+16 -30
@@ -10,23 +10,20 @@ import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import io.quarkus.vertx.http.HttpServerStart;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.event.ObservesAsync;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
@ApplicationScoped
public class EmulatorLifecycle {
private static final Logger LOG = Logger.getLogger(EmulatorLifecycle.class);
private static final int HTTP_PORT = 4566;
private static final int PORT_POLL_TIMEOUT_MS = 100;
private static final int PORT_POLL_INTERVAL_MS = 50;
private static final int PORT_POLL_MAX_RETRIES = 100;
private final StorageFactory storageFactory;
private final ServiceRegistry serviceRegistry;
@@ -47,7 +44,7 @@ public class EmulatorLifecycle {
this.initializationHooksRunner = initializationHooksRunner;
}
void onStart(@Observes StartupEvent ignored) throws IOException {
void onStart(@Observes StartupEvent ignored) {
LOG.info("=== AWS Local Emulator Starting ===");
LOG.infov("Storage mode: {0}", config.storage().mode());
LOG.infov("Persistent path: {0}", config.storage().persistentPath());
@@ -55,36 +52,25 @@ public class EmulatorLifecycle {
serviceRegistry.logEnabledServices();
storageFactory.loadAll();
if (initializationHooksRunner.hasHooks(InitializationHook.START)) {
LOG.info("Startup hooks detected — deferring execution until HTTP server is ready");
Thread.ofVirtual().name("init-hooks-runner").start(this::runStartupHooksAfterReady);
} else {
if (!initializationHooksRunner.hasHooks(InitializationHook.START)) {
LOG.info("=== AWS Local Emulator Ready ===");
}
}
private void runStartupHooksAfterReady() {
try {
waitForHttpPort();
initializationHooksRunner.run(InitializationHook.START);
LOG.info("=== AWS Local Emulator Ready ===");
} catch (Exception e) {
LOG.error("Startup hook execution failed — shutting down", e);
Quarkus.asyncExit();
}
}
private static void waitForHttpPort() throws InterruptedException {
for (int attempt = 1; attempt <= PORT_POLL_MAX_RETRIES; attempt++) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress("localhost", HTTP_PORT), PORT_POLL_TIMEOUT_MS);
LOG.debugv("HTTP port {0} is ready (attempt {1})", HTTP_PORT, attempt);
return;
} catch (IOException ignored) {
Thread.sleep(PORT_POLL_INTERVAL_MS);
void onHttpStart(@ObservesAsync HttpServerStart event) {
if ((event.options().getPort() == HTTP_PORT) &&
initializationHooksRunner.hasHooks(InitializationHook.START)){
try {
initializationHooksRunner.run(InitializationHook.START);
LOG.info("=== AWS Local Emulator Ready ===");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error("Startup hook execution interrupted — shutting down", e);
} catch (Exception e) {
LOG.error("Startup hook execution failed — shutting down", e);
Quarkus.asyncExit();
}
}
throw new IllegalStateException("HTTP port " + HTTP_PORT + " did not become ready in time");
}
void onStop(@Observes ShutdownEvent ignored) throws IOException, InterruptedException {