Move metrics shutdown behavior to a ShutdownHook

HttpServlet#destroy() is not called on instance shutdown, so the metrics code was never shut down correctly before.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=156360866
This commit is contained in:
shikhman 2017-05-17 15:14:41 -07:00 committed by Ben McIlwain
parent 25ff8c7490
commit 2b1ab579dc
2 changed files with 30 additions and 18 deletions

View file

@ -14,6 +14,8 @@
package google.registry.module.backend; package google.registry.module.backend;
import com.google.appengine.api.LifecycleManager;
import com.google.appengine.api.LifecycleManager.ShutdownHook;
import google.registry.monitoring.metrics.MetricReporter; import google.registry.monitoring.metrics.MetricReporter;
import google.registry.util.FormattingLogger; import google.registry.util.FormattingLogger;
import java.io.IOException; import java.io.IOException;
@ -43,10 +45,12 @@ public final class BackendServlet extends HttpServlet {
} catch (TimeoutException timeoutException) { } catch (TimeoutException timeoutException) {
logger.severefmt("Failed to initialize MetricReporter: %s", timeoutException); logger.severefmt("Failed to initialize MetricReporter: %s", timeoutException);
} }
}
LifecycleManager.getInstance()
.setShutdownHook(
new ShutdownHook() {
@Override @Override
public void destroy() { public void shutdown() {
try { try {
metricReporter.stopAsync().awaitTerminated(10, TimeUnit.SECONDS); metricReporter.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
logger.info("Shut down MetricReporter"); logger.info("Shut down MetricReporter");
@ -54,6 +58,8 @@ public final class BackendServlet extends HttpServlet {
logger.severefmt("Failed to stop MetricReporter: %s", timeoutException); logger.severefmt("Failed to stop MetricReporter: %s", timeoutException);
} }
} }
});
}
@Override @Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException { public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException {

View file

@ -14,6 +14,8 @@
package google.registry.module.frontend; package google.registry.module.frontend;
import com.google.appengine.api.LifecycleManager;
import com.google.appengine.api.LifecycleManager.ShutdownHook;
import google.registry.monitoring.metrics.MetricReporter; import google.registry.monitoring.metrics.MetricReporter;
import google.registry.util.FormattingLogger; import google.registry.util.FormattingLogger;
import java.io.IOException; import java.io.IOException;
@ -43,10 +45,12 @@ public final class FrontendServlet extends HttpServlet {
} catch (TimeoutException timeoutException) { } catch (TimeoutException timeoutException) {
logger.severefmt("Failed to initialize MetricReporter: %s", timeoutException); logger.severefmt("Failed to initialize MetricReporter: %s", timeoutException);
} }
}
LifecycleManager.getInstance()
.setShutdownHook(
new ShutdownHook() {
@Override @Override
public void destroy() { public void shutdown() {
try { try {
metricReporter.stopAsync().awaitTerminated(10, TimeUnit.SECONDS); metricReporter.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
logger.info("Shut down MetricReporter"); logger.info("Shut down MetricReporter");
@ -54,6 +58,8 @@ public final class FrontendServlet extends HttpServlet {
logger.severefmt("Failed to stop MetricReporter: %s", timeoutException); logger.severefmt("Failed to stop MetricReporter: %s", timeoutException);
} }
} }
});
}
@Override @Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException { public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException {