From e73325edcf9c6c2e052a4d93addc631b6388b8c2 Mon Sep 17 00:00:00 2001 From: jianglai Date: Tue, 28 Nov 2017 10:38:47 -0800 Subject: [PATCH] Make FOSS build work with DummyKeyringModule DummyKeyringModule provides a fake string as the JSON credential used to instantiate a GoogleCredential. Of course this would not work and when the metric reporter requests a GoogleCredential in the main thread. This causes the FOSS build to crash on startup, because it defaults to use DummyKeyringModule. This change allows a graceful handling of such an error by wrapping any calls to instantiate a metric reporter in a try block. Note that any attempt to write to stackdriver will still fail, but that happens in a different thread and will not make the whole program crash. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=177183337 --- .../module/backend/BackendServlet.java | 28 ++++++++++--------- .../module/frontend/FrontendServlet.java | 28 ++++++++++--------- 2 files changed, 30 insertions(+), 26 deletions(-) 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