From a9ce521c0c3fc1e4fb1b552602aee1d3b501130d Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sat, 13 Mar 2021 10:15:29 +0200 Subject: [PATCH] Made shutdown session save less disruptive - Session save is now properly waited for when plugin disables The session save attempt times out after 4 seconds instead of waiting forever - If shutdown save is not performed, instead of attempting again on JVM death, the sessions are placed into a file that is read next time the plugin enables. Affects issues: - Fixed #1770 --- .../main/java/com/djrapitops/plan/Plan.java | 29 +++- .../plan/modules/bukkit/BukkitTaskModule.java | 5 + .../gathering/ShutdownDataPreservation.java | 159 ++++++++++++++++++ .../plan/gathering/ShutdownHook.java | 23 +-- .../plan/gathering/domain/DataMap.java | 6 +- .../gathering/domain/FinishedSession.java | 49 ++++++ .../plan/settings/locale/Locale.java | 4 + .../plan/settings/locale/lang/PluginLang.java | 2 + .../events/ServerShutdownTransaction.java | 2 + .../java/com/djrapitops/plan/PlanNukkit.java | 28 ++- .../plan/modules/nukkit/NukkitTaskModule.java | 5 + .../java/com/djrapitops/plan/PlanSponge.java | 25 ++- .../plan/modules/sponge/SpongeTaskModule.java | 5 + 13 files changed, 308 insertions(+), 34 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/gathering/ShutdownDataPreservation.java diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java index d681b05c7..97dfc9052 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java @@ -34,6 +34,11 @@ import org.bukkit.command.PluginCommand; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; @@ -118,16 +123,30 @@ public class Plan extends JavaPlugin implements PlanPlugin { return PlanColorScheme.create(system.getConfigSystem().getConfig(), logger); } - /** - * Disables the plugin. - */ @Override public void onDisable() { - if (serverShutdownSave != null) serverShutdownSave.performSave(); + storeSessionsOnShutdown(); cancelAllTasks(); if (system != null) system.disable(); - logger.info(locale != null ? locale.getString(PluginLang.DISABLED) : PluginLang.DISABLED.getDefault()); + logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED)); + } + + private void storeSessionsOnShutdown() { + if (serverShutdownSave != null) { + Optional> complete = serverShutdownSave.performSave(); + if (complete.isPresent()) { + try { + complete.get().get(4, TimeUnit.SECONDS); // wait for completion for 4s + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + logger.error("Failed to save sessions to database on shutdown: " + e.getCause().getMessage()); + } catch (TimeoutException e) { + logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED_UNSAVED_SESSIONS_TIMEOUT)); + } + } + } } public void cancelAllTasks() { diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/modules/bukkit/BukkitTaskModule.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/modules/bukkit/BukkitTaskModule.java index 89f50a802..b817aee32 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/modules/bukkit/BukkitTaskModule.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/modules/bukkit/BukkitTaskModule.java @@ -19,6 +19,7 @@ package com.djrapitops.plan.modules.bukkit; import com.djrapitops.plan.TaskSystem; import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage; import com.djrapitops.plan.extension.ExtensionServerDataUpdater; +import com.djrapitops.plan.gathering.ShutdownDataPreservation; import com.djrapitops.plan.gathering.ShutdownHook; import com.djrapitops.plan.gathering.timed.BukkitPingCounter; import com.djrapitops.plan.gathering.timed.ServerTPSCounter; @@ -74,4 +75,8 @@ public interface BukkitTaskModule { @IntoSet TaskSystem.Task bindJSONFileStorageCleanTask(JSONFileStorage.CleanTask cleanTask); + @Binds + @IntoSet + TaskSystem.Task bindShutdownDataPreservation(ShutdownDataPreservation dataPreservation); + } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/gathering/ShutdownDataPreservation.java b/Plan/common/src/main/java/com/djrapitops/plan/gathering/ShutdownDataPreservation.java new file mode 100644 index 000000000..13744097a --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/gathering/ShutdownDataPreservation.java @@ -0,0 +1,159 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.gathering; + +import com.djrapitops.plan.TaskSystem; +import com.djrapitops.plan.gathering.cache.SessionCache; +import com.djrapitops.plan.gathering.domain.FinishedSession; +import com.djrapitops.plan.settings.locale.Locale; +import com.djrapitops.plan.settings.locale.lang.PluginLang; +import com.djrapitops.plan.storage.database.DBSystem; +import com.djrapitops.plan.storage.database.queries.LargeStoreQueries; +import com.djrapitops.plan.storage.database.transactions.Transaction; +import com.djrapitops.plan.storage.file.PlanFiles; +import com.djrapitops.plan.utilities.logging.ErrorContext; +import com.djrapitops.plan.utilities.logging.ErrorLogger; +import net.playeranalytics.plugin.scheduling.RunnableFactory; +import net.playeranalytics.plugin.server.PluginLogger; +import org.apache.commons.text.TextStringBuilder; + +import javax.inject.Inject; +import javax.inject.Singleton; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Singleton +public class ShutdownDataPreservation extends TaskSystem.Task { + + private final Locale locale; + private final DBSystem dbSystem; + private final PluginLogger logger; + private final ErrorLogger errorLogger; + + private final Path storeLocation; + + @Inject + public ShutdownDataPreservation( + PlanFiles files, + Locale locale, + DBSystem dbSystem, + PluginLogger logger, + ErrorLogger errorLogger + ) { + this.locale = locale; + this.dbSystem = dbSystem; + + storeLocation = files.getDataDirectory().resolve("unsaved-sessions.csv"); + this.logger = logger; + this.errorLogger = errorLogger; + } + + public void storePreviouslyPreservedSessions() { + if (storeLocation.toFile().exists()) { + try { + logger.info(locale.getString(PluginLang.ENABLE_NOTIFY_STORING_PRESERVED_SESSIONS)); + List finishedSessions = loadFinishedSessions(); + storeInDB(finishedSessions); + deleteStorageFile(); + } catch (IllegalStateException e) { + errorLogger.error(e, ErrorContext.builder().related(storeLocation).build()); + } + } + } + + private void storeInDB(List finishedSessions) { + if (!finishedSessions.isEmpty()) { + try { + dbSystem.getDatabase().executeTransaction(new Transaction() { + @Override + protected void performOperations() { + execute(LargeStoreQueries.storeAllSessionsWithKillAndWorldData(finishedSessions)); + } + }).get(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + throw new IllegalStateException("Failed to store unsaved sessions in database: " + e.getCause().getMessage(), e); + } + } + } + + @Override + public void run() { + storePreviouslyPreservedSessions(); + } + + @Override + public void register(RunnableFactory runnableFactory) { + runnableFactory.create(this).runTaskAsynchronously(); + } + + private void deleteStorageFile() { + try { + Files.delete(storeLocation); + } catch (IOException e) { + throw new IllegalStateException("Could not delete " + storeLocation.toFile().getAbsolutePath() + ", " + e.getMessage()); + } + } + + private List loadFinishedSessions() { + try (Stream lines = Files.lines(storeLocation)) { + return lines.map(FinishedSession::deserializeCSV) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toList()); + } catch (IOException e) { + throw new IllegalStateException("Could not read " + storeLocation.toFile().getAbsolutePath() + ", " + e.getMessage(), e); + } + } + + public void preserveSessionsInCache() { + long now = System.currentTimeMillis(); + List finishedSessions = SessionCache.getActiveSessions().stream() + .map(session -> session.toFinishedSession(now)) + .collect(Collectors.toList()); + storeFinishedSessions(finishedSessions); + } + + private void storeFinishedSessions(List sessions) { + if (sessions.isEmpty()) return; + if (storeLocation.toFile().exists()) { + sessions.addAll(loadFinishedSessions()); + } + + List lines = sessions.stream().map(FinishedSession::serializeCSV).collect(Collectors.toList()); + try { + Files.write(storeLocation, lines, StandardCharsets.UTF_8, + StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING, + StandardOpenOption.WRITE + ); + } catch (IOException e) { + logger.warn("Place this to " + storeLocation.toFile().getAbsolutePath() + " if you wish to store missing sessions:"); + logger.warn(new TextStringBuilder().appendWithSeparators(lines, "\n").build()); + throw new IllegalStateException("Could not write " + storeLocation.toFile().getAbsolutePath() + ", " + e.getMessage(), e); + } + } +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/gathering/ShutdownHook.java b/Plan/common/src/main/java/com/djrapitops/plan/gathering/ShutdownHook.java index 981333bea..1e4d0ebad 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/gathering/ShutdownHook.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/gathering/ShutdownHook.java @@ -21,10 +21,6 @@ import net.playeranalytics.plugin.scheduling.RunnableFactory; import javax.inject.Inject; import javax.inject.Singleton; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.logging.Level; -import java.util.logging.Logger; /** * Thread that is run when JVM shuts down. @@ -38,11 +34,11 @@ public class ShutdownHook extends Thread { private static ShutdownHook activated; - private final ServerShutdownSave serverShutdownSave; + private final ShutdownDataPreservation dataPreservation; @Inject - public ShutdownHook(ServerShutdownSave serverShutdownSave) { - this.serverShutdownSave = serverShutdownSave; + public ShutdownHook(ShutdownDataPreservation dataPreservation) { + this.dataPreservation = dataPreservation; } private static boolean isActivated() { @@ -68,18 +64,7 @@ public class ShutdownHook extends Thread { @Override public void run() { - serverShutdownSave.serverIsKnownToBeShuttingDown(); - serverShutdownSave.performSave().ifPresent(this::waitForSave); - } - - private void waitForSave(Future sessionsAreSavedFuture) { - try { - sessionsAreSavedFuture.get(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } catch (ExecutionException e) { - Logger.getGlobal().log(Level.SEVERE, "Plan failed to save sessions on JVM shutdown.", e); - } + dataPreservation.preserveSessionsInCache(); } @Singleton diff --git a/Plan/common/src/main/java/com/djrapitops/plan/gathering/domain/DataMap.java b/Plan/common/src/main/java/com/djrapitops/plan/gathering/domain/DataMap.java index e48389f8c..5cd51803d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/gathering/domain/DataMap.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/gathering/domain/DataMap.java @@ -23,18 +23,18 @@ import java.util.Optional; public class DataMap { - private final Map, Object> data; + private final Map data; public DataMap() { this.data = new HashMap<>(); } public void put(Class type, T value) { - data.put(type, value); + data.put(type.getName(), value); } public Optional get(Class ofType) { - return Optional.ofNullable(ofType.cast(data.get(ofType))); + return Optional.ofNullable(ofType.cast(data.get(ofType.getName()))); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/gathering/domain/FinishedSession.java b/Plan/common/src/main/java/com/djrapitops/plan/gathering/domain/FinishedSession.java index a78685c92..99a159b24 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/gathering/domain/FinishedSession.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/gathering/domain/FinishedSession.java @@ -18,6 +18,8 @@ package com.djrapitops.plan.gathering.domain; import com.djrapitops.plan.delivery.domain.DateHolder; import com.djrapitops.plan.identification.ServerUUID; +import com.google.gson.Gson; +import org.apache.commons.lang3.StringUtils; import java.util.List; import java.util.Objects; @@ -154,4 +156,51 @@ public class FinishedSession implements DateHolder { return id; } } + + /** + * Deserialize csv format of the session. + * + * @param serialized Serialized version of the session + * @return Proper session if the csv had 9 columns or more + * @throws com.google.gson.JsonSyntaxException if serialized format has a json syntax error + */ + public static Optional deserializeCSV(String serialized) { + String[] asArray = StringUtils.split(serialized, ';'); + if (asArray.length < 9) return Optional.empty(); + // Note for the future: Use length to determine version of serialized class + + Gson gson = new Gson(); + + UUID playerUUID = UUID.fromString(asArray[0]); + ServerUUID serverUUID = ServerUUID.fromString(asArray[1]); + long start = Long.parseLong(asArray[2]); + long end = Long.parseLong(asArray[3]); + long afkTime = Long.parseLong(asArray[4]); + + DataMap extraData = new DataMap(); + extraData.put(WorldTimes.class, gson.fromJson(asArray[5], WorldTimes.class)); + extraData.put(PlayerKills.class, gson.fromJson(asArray[6], PlayerKills.class)); + extraData.put(MobKillCounter.class, gson.fromJson(asArray[7], MobKillCounter.class)); + extraData.put(DeathCounter.class, gson.fromJson(asArray[8], DeathCounter.class)); + return Optional.of(new FinishedSession(playerUUID, serverUUID, start, end, afkTime, extraData)); + } + + /** + * Serialize into csv format. + * + * @return Serialized format + */ + public String serializeCSV() { + Gson gson = new Gson(); + + return String.valueOf(playerUUID) + ';' + + serverUUID + ';' + + start + ';' + + end + ';' + + afkTime + ';' + + gson.toJson(getExtraData(WorldTimes.class).orElseGet(WorldTimes::new)) + ';' + + gson.toJson(getExtraData(PlayerKills.class).orElseGet(PlayerKills::new)) + ';' + + gson.toJson(getExtraData(MobKillCounter.class).orElseGet(MobKillCounter::new)) + ';' + + gson.toJson(getExtraData(DeathCounter.class).orElseGet(DeathCounter::new)); + } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/Locale.java b/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/Locale.java index 1cabc6f4c..a3a6f2636 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/Locale.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/Locale.java @@ -40,6 +40,10 @@ public class Locale extends HashMap { return forLangCode(LangCode.fromString(code), files); } + public static String getStringNullSafe(Locale locale, Lang lang) { + return locale != null ? locale.getString(lang) : lang.getDefault(); + } + private LangCode langCode; public Locale() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/lang/PluginLang.java b/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/lang/PluginLang.java index 7f28e8a2c..0595e76c5 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/lang/PluginLang.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/settings/locale/lang/PluginLang.java @@ -28,6 +28,7 @@ public enum PluginLang implements Lang { API_ADD_RESOURCE_JS("API - js+", "PageExtension: ${0} added javascript(s) to ${1}, ${2}"), API_ADD_RESOURCE_CSS("API - css+", "PageExtension: ${0} added stylesheet(s) to ${1}, ${2}"), + ENABLE_NOTIFY_STORING_PRESERVED_SESSIONS("Enable - Storing preserved sessions", "Storing sessions that were preserved before previous shutdown."), ENABLE_NOTIFY_EMPTY_IP("Enable - Notify Empty IP", "IP in server.properties is empty & Alternative_IP is not in use. Incorrect links might be given!"), ENABLE_NOTIFY_BAD_IP("Enable - Notify Bad IP", "0.0.0.0 is not a valid address, set up Alternative_IP settings. Incorrect links might be given!"), ENABLE_NOTIFY_WEB_SERVER_DISABLED("Enable - Notify Webserver disabled", "WebServer was not initialized. (WebServer.DisableWebServer: true)"), @@ -56,6 +57,7 @@ public enum PluginLang implements Lang { DISABLED_PROCESSING("Disable - Processing", "Processing critical unprocessed tasks. (${0})"), DISABLED_PROCESSING_COMPLETE("Disable - Processing Complete", "Processing complete."), DISABLED_UNSAVED_SESSIONS("Disable - Unsaved Session Save", "Saving unfinished sessions.."), + DISABLED_UNSAVED_SESSIONS_TIMEOUT("Disable - Unsaved Session Save Timeout", "Timeout hit, storing the unfinished sessions on next enable instead."), VERSION_NEWEST("Version - Latest", "You're using the latest version."), VERSION_AVAILABLE("Version - New", "New Release (${0}) is available ${1}"), diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/ServerShutdownTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/ServerShutdownTransaction.java index eb1ea51b9..e641034b4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/ServerShutdownTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/ServerShutdownTransaction.java @@ -16,6 +16,7 @@ */ package com.djrapitops.plan.storage.database.transactions.events; +import com.djrapitops.plan.gathering.cache.SessionCache; import com.djrapitops.plan.gathering.domain.FinishedSession; import com.djrapitops.plan.storage.database.queries.LargeStoreQueries; import com.djrapitops.plan.storage.database.transactions.Transaction; @@ -38,5 +39,6 @@ public class ServerShutdownTransaction extends Transaction { @Override protected void performOperations() { execute(LargeStoreQueries.storeAllSessionsWithKillAndWorldData(unsavedSessions)); + SessionCache.clear(); } } \ No newline at end of file diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java index 8495e047d..f702b9af0 100644 --- a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java @@ -38,6 +38,10 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; @@ -101,16 +105,30 @@ public class PlanNukkit extends PluginBase implements PlanPlugin { return PlanColorScheme.create(system.getConfigSystem().getConfig(), logger); } - /** - * Disables the plugin. - */ @Override public void onDisable() { - if (serverShutdownSave != null) serverShutdownSave.performSave(); + storeSessionsOnShutdown(); cancelAllTasks(); if (system != null) system.disable(); - logger.info(locale != null ? locale.getString(PluginLang.DISABLED) : PluginLang.DISABLED.getDefault()); + logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED)); + } + + private void storeSessionsOnShutdown() { + if (serverShutdownSave != null) { + Optional> complete = serverShutdownSave.performSave(); + if (complete.isPresent()) { + try { + complete.get().get(4, TimeUnit.SECONDS); // wait for completion for 4s + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + logger.error("Failed to save sessions to database on shutdown: " + e.getCause().getMessage()); + } catch (TimeoutException e) { + logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED_UNSAVED_SESSIONS_TIMEOUT)); + } + } + } } public void cancelAllTasks() { diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/modules/nukkit/NukkitTaskModule.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/modules/nukkit/NukkitTaskModule.java index 3eb1a7259..dfcdff7b2 100644 --- a/Plan/nukkit/src/main/java/com/djrapitops/plan/modules/nukkit/NukkitTaskModule.java +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/modules/nukkit/NukkitTaskModule.java @@ -20,6 +20,7 @@ import cn.nukkit.level.Level; import com.djrapitops.plan.TaskSystem; import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage; import com.djrapitops.plan.extension.ExtensionServerDataUpdater; +import com.djrapitops.plan.gathering.ShutdownDataPreservation; import com.djrapitops.plan.gathering.ShutdownHook; import com.djrapitops.plan.gathering.timed.NukkitPingCounter; import com.djrapitops.plan.gathering.timed.ServerTPSCounter; @@ -73,4 +74,8 @@ public interface NukkitTaskModule { @Binds @IntoSet TaskSystem.Task bindJSONFileStorageCleanTask(JSONFileStorage.CleanTask cleanTask); + + @Binds + @IntoSet + TaskSystem.Task bindShutdownDataPreservation(ShutdownDataPreservation dataPreservation); } diff --git a/Plan/sponge/src/main/java/com/djrapitops/plan/PlanSponge.java b/Plan/sponge/src/main/java/com/djrapitops/plan/PlanSponge.java index 9346c7b1c..87e81b679 100644 --- a/Plan/sponge/src/main/java/com/djrapitops/plan/PlanSponge.java +++ b/Plan/sponge/src/main/java/com/djrapitops/plan/PlanSponge.java @@ -47,6 +47,10 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.logging.Level; @Plugin( @@ -142,11 +146,28 @@ public class PlanSponge implements PlanPlugin { } public void onDisable() { - if (serverShutdownSave != null) serverShutdownSave.performSave(); + storeSessionsOnShutdown(); cancelAllTasks(); if (system != null) system.disable(); - logger.info(locale.getString(PluginLang.DISABLED)); + logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED)); + } + + private void storeSessionsOnShutdown() { + if (serverShutdownSave != null) { + Optional> complete = serverShutdownSave.performSave(); + if (complete.isPresent()) { + try { + complete.get().get(4, TimeUnit.SECONDS); // wait for completion for 4s + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + logger.error("Failed to save sessions to database on shutdown: " + e.getCause().getMessage()); + } catch (TimeoutException e) { + logger.info(Locale.getStringNullSafe(locale, PluginLang.DISABLED_UNSAVED_SESSIONS_TIMEOUT)); + } + } + } } public void cancelAllTasks() { diff --git a/Plan/sponge/src/main/java/com/djrapitops/plan/modules/sponge/SpongeTaskModule.java b/Plan/sponge/src/main/java/com/djrapitops/plan/modules/sponge/SpongeTaskModule.java index 9aa3a6c7e..4fbe926d6 100644 --- a/Plan/sponge/src/main/java/com/djrapitops/plan/modules/sponge/SpongeTaskModule.java +++ b/Plan/sponge/src/main/java/com/djrapitops/plan/modules/sponge/SpongeTaskModule.java @@ -19,6 +19,7 @@ package com.djrapitops.plan.modules.sponge; import com.djrapitops.plan.TaskSystem; import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage; import com.djrapitops.plan.extension.ExtensionServerDataUpdater; +import com.djrapitops.plan.gathering.ShutdownDataPreservation; import com.djrapitops.plan.gathering.ShutdownHook; import com.djrapitops.plan.gathering.timed.ServerTPSCounter; import com.djrapitops.plan.gathering.timed.SpongePingCounter; @@ -73,4 +74,8 @@ public interface SpongeTaskModule { @Binds @IntoSet TaskSystem.Task bindJSONFileStorageCleanTask(JSONFileStorage.CleanTask cleanTask); + + @Binds + @IntoSet + TaskSystem.Task bindShutdownDataPreservation(ShutdownDataPreservation dataPreservation); }