update shutdown gauge when delayed shutdown starts

Otherwise we will report that we are not shutting down while k8s correctly
notes that we are unhealthy and it will look like something is wrong.
This commit is contained in:
Jonathan Klabunde Tomer
2025-11-19 15:04:11 -08:00
committed by Jonathan Klabunde Tomer
parent 65ce9af366
commit 4c4a954c1c
2 changed files with 22 additions and 4 deletions

View File

@@ -11,15 +11,19 @@ import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A managed monitor that reports whether the application is shutting down as a metric. That metric can then be used in
* conjunction with other indicators to conditionally fire or suppress alerts.
*/
public class ApplicationShutdownMonitor implements LifeCycle.Listener {
public class ApplicationShutdownMonitor extends AbstractLifeCycle {
private final AtomicBoolean shuttingDown = new AtomicBoolean(false);
private final Logger logger = LoggerFactory.getLogger(ApplicationShutdownMonitor.class);
public ApplicationShutdownMonitor(final MeterRegistry meterRegistry) {
// without a strong reference to the gauges value supplier, shutdown garbage collection
@@ -29,8 +33,22 @@ public class ApplicationShutdownMonitor implements LifeCycle.Listener {
.register(meterRegistry);
}
public void register() {
// Force this component to get shut down before Dropwizard's
// DelayedShutdownHandler, which initiates the delayed-shutdown process
// without an additional chance for us to hook it
logger.info("registering shutdown monitor");
try {
start(); // jetty won't stop an unstarted lifecycle
ShutdownThread.register(0, this);
} catch (Exception e) {
logger.error("failed to start application shutdown monitor", e);
}
}
@Override
public void lifeCycleStopping(final LifeCycle event) {
public void doStop() {
logger.info("setting shutdown flag");
shuttingDown.set(true);
}
}

View File

@@ -83,7 +83,7 @@ public class MetricsUtil {
environment.lifecycle().addServerLifecycleListener(
server -> JettySslHandshakeMetrics.addToAllConnectors(server, Metrics.globalRegistry));
environment.lifecycle().addEventListener(new ApplicationShutdownMonitor(Metrics.globalRegistry));
new ApplicationShutdownMonitor(Metrics.globalRegistry).register();
environment.lifecycle().addEventListener(
new MicrometerRegistryManager(Metrics.globalRegistry, shutdownWaitDuration));