Replaced metrics and save-threads with TimerTasks

This commit is contained in:
Blue (Lukas Rieger) 2021-04-02 12:20:48 +02:00
parent 07932506fd
commit 5a342e1fe0
No known key found for this signature in database
GPG Key ID: 904C4995F9E1F800

View File

@ -44,9 +44,7 @@
import de.bluecolored.bluemap.core.world.World; import de.bluecolored.bluemap.core.world.World;
import java.io.*; import java.io.*;
import java.util.Collection; import java.util.*;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@ -58,8 +56,8 @@ public class Plugin {
private final InterruptableReentrantLock loadingLock = new InterruptableReentrantLock(); private final InterruptableReentrantLock loadingLock = new InterruptableReentrantLock();
private MinecraftVersion minecraftVersion; private final MinecraftVersion minecraftVersion;
private String implementationType; private final String implementationType;
private ServerInterface serverInterface; private ServerInterface serverInterface;
private BlueMapService blueMap; private BlueMapService blueMap;
@ -70,8 +68,10 @@ public class Plugin {
private RenderManager renderManager; private RenderManager renderManager;
private WebServer webServer; private WebServer webServer;
private Thread periodicalSaveThread;
private Thread metricsThread; private final Timer daemonTimer;
private TimerTask periodicalSaveTask;
private TimerTask metricsTask;
private PluginConfig pluginConfig; private PluginConfig pluginConfig;
private MapUpdateHandler updateHandler; private MapUpdateHandler updateHandler;
@ -83,6 +83,8 @@ public Plugin(MinecraftVersion minecraftVersion, String implementationType, Serv
this.minecraftVersion = minecraftVersion; this.minecraftVersion = minecraftVersion;
this.implementationType = implementationType.toLowerCase(); this.implementationType = implementationType.toLowerCase();
this.serverInterface = serverInterface; this.serverInterface = serverInterface;
this.daemonTimer = new Timer("BlueMap-Daemon-Timer", true);
} }
public void load() throws IOException, ParseResourceException { public void load() throws IOException, ParseResourceException {
@ -167,28 +169,26 @@ public void load() throws IOException, ParseResourceException {
Logger.global.logError("Failed to load render-manager state!", ex); Logger.global.logError("Failed to load render-manager state!", ex);
} }
//create periodical-save thread //do periodical saves
periodicalSaveThread = new Thread(() -> { periodicalSaveTask = new TimerTask() {
try { @Override
while (true) { public void run() {
Thread.sleep(TimeUnit.MINUTES.toMillis(5)); try {
try { saveRenderManagerState();
saveRenderManagerState();
//clean up caches //clean up caches
for (World world : blueMap.getWorlds().values()) { for (World world : blueMap.getWorlds().values()) {
world.cleanUpChunkCache(); world.cleanUpChunkCache();
}
} catch (IOException ex) {
Logger.global.logError("Failed to save render-manager state!", ex);
} }
} catch (IOException ex) {
Logger.global.logError("Failed to save render-manager state!", ex);
} catch (InterruptedException ex) {
this.cancel();
Thread.currentThread().interrupt();
} }
} catch (InterruptedException ex){
Thread.currentThread().interrupt();
return;
} }
}); };
periodicalSaveThread.start(); daemonTimer.schedule(periodicalSaveTask, TimeUnit.MINUTES.toMillis(5), TimeUnit.MINUTES.toMillis(5));
//start map updater //start map updater
this.updateHandler = new MapUpdateHandler(this); this.updateHandler = new MapUpdateHandler(this);
@ -208,21 +208,14 @@ public void load() throws IOException, ParseResourceException {
} }
//metrics //metrics
metricsThread = new Thread(() -> { metricsTask = new TimerTask() {
try { @Override
Thread.sleep(TimeUnit.MINUTES.toMillis(1)); public void run() {
if (Plugin.this.serverInterface.isMetricsEnabled(coreConfig.isMetricsEnabled()))
while (true) { Metrics.sendReport(Plugin.this.implementationType);
if (serverInterface.isMetricsEnabled(coreConfig.isMetricsEnabled())) Metrics.sendReport(this.implementationType);
Thread.sleep(TimeUnit.MINUTES.toMillis(30));
}
} catch (InterruptedException ex){
Thread.currentThread().interrupt();
return;
} }
}); };
metricsThread.start(); daemonTimer.scheduleAtFixedRate(metricsTask, TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(30));
loaded = true; loaded = true;
@ -252,19 +245,8 @@ public void unload() {
serverInterface.unregisterAllListeners(); serverInterface.unregisterAllListeners();
//stop scheduled threads //stop scheduled threads
if (metricsThread != null) { metricsTask.cancel();
metricsThread.interrupt(); periodicalSaveTask.cancel();
try {metricsThread.join(1000);} catch (InterruptedException ignore) { Thread.currentThread().interrupt(); }
if (metricsThread.isAlive()) Logger.global.logWarning("The metricsThread did not terminate correctly in time!");
metricsThread = null;
}
if (periodicalSaveThread != null) {
periodicalSaveThread.interrupt();
try {periodicalSaveThread.join(1000);} catch (InterruptedException ignore) { Thread.currentThread().interrupt(); }
if (periodicalSaveThread.isAlive()) Logger.global.logWarning("The periodicalSaveThread did not terminate correctly in time!");
periodicalSaveThread = null;
}
//stop services //stop services
if (renderManager != null) renderManager.stop(); if (renderManager != null) renderManager.stop();