diff --git a/java/google/registry/module/backend/BackendServlet.java b/java/google/registry/module/backend/BackendServlet.java index 43d093766..822435968 100644 --- a/java/google/registry/module/backend/BackendServlet.java +++ b/java/google/registry/module/backend/BackendServlet.java @@ -39,23 +39,25 @@ public final class BackendServlet extends HttpServlet { public void init() { Security.addProvider(new BouncyCastleProvider()); + // If metric reporter failed to instantiate for any reason (bad keyring, bad json credential, + // etc), we log the error but keep the main thread running. Also the shutdown hook will only be + // registered if metric reporter starts up correctly. try { metricReporter.get().startAsync().awaitRunning(10, TimeUnit.SECONDS); logger.info("Started up MetricReporter"); - } catch (TimeoutException timeoutException) { - logger.severefmt("Failed to initialize MetricReporter: %s", timeoutException); + LifecycleManager.getInstance() + .setShutdownHook( + () -> { + try { + metricReporter.get().stopAsync().awaitTerminated(10, TimeUnit.SECONDS); + logger.info("Shut down MetricReporter"); + } catch (TimeoutException timeoutException) { + logger.severefmt("Failed to stop MetricReporter: %s", timeoutException); + } + }); + } catch (Exception e) { + logger.severefmt(e, "Failed to initialize MetricReporter."); } - - LifecycleManager.getInstance() - .setShutdownHook( - () -> { - try { - metricReporter.get().stopAsync().awaitTerminated(10, TimeUnit.SECONDS); - logger.info("Shut down MetricReporter"); - } catch (TimeoutException timeoutException) { - logger.severefmt("Failed to stop MetricReporter: %s", timeoutException); - } - }); } @Override diff --git a/java/google/registry/module/frontend/FrontendServlet.java b/java/google/registry/module/frontend/FrontendServlet.java index dd06e3775..0668ac6a1 100644 --- a/java/google/registry/module/frontend/FrontendServlet.java +++ b/java/google/registry/module/frontend/FrontendServlet.java @@ -39,23 +39,25 @@ public final class FrontendServlet extends HttpServlet { public void init() { Security.addProvider(new BouncyCastleProvider()); + // If metric reporter failed to instantiate for any reason (bad keyring, bad json credential, + // etc), we log the error but keep the main thread running. Also the shutdown hook will only be + // registered if metric reporter starts up correctly. try { metricReporter.get().startAsync().awaitRunning(10, TimeUnit.SECONDS); logger.info("Started up MetricReporter"); - } catch (TimeoutException timeoutException) { - logger.severefmt("Failed to initialize MetricReporter: %s", timeoutException); + LifecycleManager.getInstance() + .setShutdownHook( + () -> { + try { + metricReporter.get().stopAsync().awaitTerminated(10, TimeUnit.SECONDS); + logger.info("Shut down MetricReporter"); + } catch (TimeoutException timeoutException) { + logger.severefmt("Failed to stop MetricReporter: %s", timeoutException); + } + }); + } catch (Exception e) { + logger.severefmt(e, "Failed to initialize MetricReporter: %s"); } - - LifecycleManager.getInstance() - .setShutdownHook( - () -> { - try { - metricReporter.get().stopAsync().awaitTerminated(10, TimeUnit.SECONDS); - logger.info("Shut down MetricReporter"); - } catch (TimeoutException timeoutException) { - logger.severefmt("Failed to stop MetricReporter: %s", timeoutException); - } - }); } @Override