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,16 +45,20 @@ 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);
} }
}
@Override LifecycleManager.getInstance()
public void destroy() { .setShutdownHook(
try { new ShutdownHook() {
metricReporter.stopAsync().awaitTerminated(10, TimeUnit.SECONDS); @Override
logger.info("Shut down MetricReporter"); public void shutdown() {
} catch (TimeoutException timeoutException) { try {
logger.severefmt("Failed to stop MetricReporter: %s", timeoutException); metricReporter.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
} logger.info("Shut down MetricReporter");
} catch (TimeoutException timeoutException) {
logger.severefmt("Failed to stop MetricReporter: %s", timeoutException);
}
}
});
} }
@Override @Override

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,16 +45,20 @@ 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);
} }
}
@Override LifecycleManager.getInstance()
public void destroy() { .setShutdownHook(
try { new ShutdownHook() {
metricReporter.stopAsync().awaitTerminated(10, TimeUnit.SECONDS); @Override
logger.info("Shut down MetricReporter"); public void shutdown() {
} catch (TimeoutException timeoutException) { try {
logger.severefmt("Failed to stop MetricReporter: %s", timeoutException); metricReporter.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
} logger.info("Shut down MetricReporter");
} catch (TimeoutException timeoutException) {
logger.severefmt("Failed to stop MetricReporter: %s", timeoutException);
}
}
});
} }
@Override @Override