Start and stop all lifecycle-managed objects in `CrawlAccountsCommand`

This commit is contained in:
Chris Eager 2023-06-17 10:03:07 -05:00 committed by Chris Eager
parent 7dce183170
commit 6d81f69785
2 changed files with 18 additions and 44 deletions

View File

@ -5,11 +5,7 @@
package org.whispersystems.textsecuregcm.metrics; package org.whispersystems.textsecuregcm.metrics;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries; import com.codahale.metrics.SharedMetricRegistries;
import io.dropwizard.lifecycle.JettyManaged;
import io.dropwizard.lifecycle.setup.LifecycleEnvironment;
import io.dropwizard.metrics.ScheduledReporterManager;
import io.dropwizard.setup.Environment; import io.dropwizard.setup.Environment;
import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.Metrics;
@ -90,41 +86,4 @@ public class MetricsUtil {
GarbageCollectionGauges.registerMetrics(); GarbageCollectionGauges.registerMetrics();
} }
/**
* For use in commands where {@link JettyManaged} doesn't apply
*
* @see io.dropwizard.metrics.MetricsFactory#configure(LifecycleEnvironment, MetricRegistry)
*/
public static void startManagedReporters(Environment environment) {
environment.lifecycle().getManagedObjects().forEach(managedObject -> {
if (managedObject instanceof JettyManaged jettyManaged) {
if (jettyManaged.getManaged() instanceof ScheduledReporterManager scheduledReporterManager) {
try {
scheduledReporterManager.start();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
});
}
/**
* For use in commands where {@link JettyManaged} doesn't apply
*
* @see io.dropwizard.metrics.MetricsFactory#configure(LifecycleEnvironment, MetricRegistry)
*/
public static void stopManagedReporters(final Environment environment) {
environment.lifecycle().getManagedObjects().forEach(lifeCycle -> {
if (lifeCycle instanceof JettyManaged jettyManaged) {
if (jettyManaged.getManaged() instanceof ScheduledReporterManager scheduledReporterManager) {
try {
scheduledReporterManager.stop();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
});
}
} }

View File

@ -19,6 +19,7 @@ import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.ArgumentType; import net.sourceforge.argparse4j.inf.ArgumentType;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.WhisperServerConfiguration; import org.whispersystems.textsecuregcm.WhisperServerConfiguration;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration; import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
@ -39,12 +40,13 @@ public class CrawlAccountsCommand extends EnvironmentCommand<WhisperServerConfig
private static final String CRAWL_TYPE = "crawlType"; private static final String CRAWL_TYPE = "crawlType";
private static final String WORKER_COUNT = "workers"; private static final String WORKER_COUNT = "workers";
private static final Logger logger = LoggerFactory.getLogger(CrawlAccountsCommand.class);
public enum CrawlType implements ArgumentType<CrawlType> { public enum CrawlType implements ArgumentType<CrawlType> {
GENERAL_PURPOSE, GENERAL_PURPOSE,
ACCOUNT_CLEANER, ACCOUNT_CLEANER,
; ;
@Override @Override
public CrawlType convert(final ArgumentParser parser, final Argument arg, final String value) public CrawlType convert(final ArgumentParser parser, final Argument arg, final String value)
throws ArgumentParserException { throws ArgumentParserException {
@ -142,7 +144,14 @@ public class CrawlAccountsCommand extends EnvironmentCommand<WhisperServerConfig
} }
}; };
MetricsUtil.startManagedReporters(environment); environment.lifecycle().getManagedObjects().forEach(managedObject -> {
try {
managedObject.start();
} catch (final Exception e) {
logger.error("Failed to start managed object", e);
throw new RuntimeException(e);
}
});
try { try {
crawler.crawlAllAccounts(); crawler.crawlAllAccounts();
@ -150,6 +159,12 @@ public class CrawlAccountsCommand extends EnvironmentCommand<WhisperServerConfig
LoggerFactory.getLogger(CrawlAccountsCommand.class).error("Error crawling accounts", e); LoggerFactory.getLogger(CrawlAccountsCommand.class).error("Error crawling accounts", e);
} }
MetricsUtil.stopManagedReporters(environment); environment.lifecycle().getManagedObjects().forEach(managedObject -> {
try {
managedObject.stop();
} catch (final Exception e) {
logger.error("Failed to stop managed object", e);
}
});
} }
} }