Fix shutdown issues

This commit is contained in:
RuscalWorld 2021-05-25 12:15:32 +03:00
parent c9fa96d792
commit c56b315c3e
No known key found for this signature in database
GPG key ID: 4F53776031D128ED

View file

@ -15,6 +15,7 @@ import java.util.Timer;
public class FabricExporter implements ModInitializer { public class FabricExporter implements ModInitializer {
private MinecraftServer server; private MinecraftServer server;
private MainConfig config; private MainConfig config;
private HTTPServer httpServer;
@Override @Override
public void onInitialize() { public void onInitialize() {
@ -35,19 +36,25 @@ public class FabricExporter implements ModInitializer {
metricUpdater.registerMetric(new LoadedChunks()); metricUpdater.registerMetric(new LoadedChunks());
metricUpdater.registerMetric(new TotalLoadedChunks()); metricUpdater.registerMetric(new TotalLoadedChunks());
Timer timer = new Timer();
ServerLifecycleEvents.SERVER_STARTING.register(this::setServer); ServerLifecycleEvents.SERVER_STARTING.register(this::setServer);
ServerLifecycleEvents.SERVER_STARTED.register(server -> { ServerLifecycleEvents.SERVER_STARTED.register(server -> {
try { try {
int port = this.getConfig().getPort(); int port = this.getConfig().getPort();
new HTTPServer(port); this.setHttpServer(new HTTPServer(port));
FabricExporter.getLogger().info("Prometheus exporter server is now listening on port " + port); FabricExporter.getLogger().info("Prometheus exporter server is now listening on port " + port);
Timer timer = new Timer();
timer.schedule(metricUpdater, 1000, this.getConfig().getUpdateInterval()); timer.schedule(metricUpdater, 1000, this.getConfig().getUpdateInterval());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}); });
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
this.getHttpServer().stop();
timer.cancel();
});
} }
public static Logger getLogger() { public static Logger getLogger() {
@ -69,4 +76,12 @@ public class FabricExporter implements ModInitializer {
public void setConfig(MainConfig config) { public void setConfig(MainConfig config) {
this.config = config; this.config = config;
} }
public HTTPServer getHttpServer() {
return httpServer;
}
public void setHttpServer(HTTPServer httpServer) {
this.httpServer = httpServer;
}
} }