From b74eeb95d89a488bcf800bb9d28d6c1ac68d7aa7 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Fri, 27 Nov 2020 19:21:39 +0100 Subject: [PATCH] Prevent save task overlaps (#1588) --- src/main/java/world/bentobox/bentobox/BentoBox.java | 12 ++++++++++-- .../bentobox/bentobox/managers/IslandsManager.java | 9 +++++++++ .../bentobox/bentobox/managers/PlayersManager.java | 8 ++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 06c4e3f45..90599491d 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -203,8 +203,16 @@ public void onEnable(){ // Save islands & players data every X minutes Bukkit.getScheduler().runTaskTimer(instance, () -> { - playersManager.saveAll(true); - islandsManager.saveAll(true); + if (!playersManager.isSaveTaskRunning()) { + playersManager.saveAll(true); + } else { + getLogger().warning("Tried to start a player data save task while the previous auto save was still running!"); + } + if (!islandsManager.isSaveTaskRunning()) { + islandsManager.saveAll(true); + } else { + getLogger().warning("Tried to start a island data save task while the previous auto save was still running!"); + } }, getSettings().getDatabaseBackupPeriod() * 20 * 60L, getSettings().getDatabaseBackupPeriod() * 20 * 60L); // Make sure all flag listeners are registered. diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index 4783bd35e..5f0a27257 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -88,6 +88,8 @@ public class IslandsManager { @NonNull private List deletedIslands; + private boolean isSaveTaskRunning; + /** * Islands Manager * @param plugin - plugin @@ -1209,6 +1211,10 @@ public void removePlayersFromIsland(Island island) { }); } + public boolean isSaveTaskRunning() { + return isSaveTaskRunning; + } + /** * Save the all the islands to the database */ @@ -1232,6 +1238,8 @@ public void saveAll(boolean schedule){ return; } + + isSaveTaskRunning = true; Queue queue = new LinkedList<>(islandCache.getIslands()); new BukkitRunnable() { @Override @@ -1239,6 +1247,7 @@ public void run() { for (int i = 0; i < plugin.getSettings().getMaxSavedIslandsPerTick(); i++) { Island island = queue.poll(); if (island == null) { + isSaveTaskRunning = false; cancel(); return; } diff --git a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java index 5af5191c4..8e57a61bd 100644 --- a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java @@ -25,6 +25,8 @@ public class PlayersManager { private Map playerCache; private Set inTeleport; + private boolean isSaveTaskRunning; + /** * Provides a memory cache of online player information * This is the one-stop-shop of player info @@ -59,6 +61,10 @@ public void load(){ handler.loadObjects().forEach(p -> playerCache.put(p.getPlayerUUID(), p)); } + public boolean isSaveTaskRunning() { + return isSaveTaskRunning; + } + /** * Save all players */ @@ -82,6 +88,7 @@ public void saveAll(boolean schedule){ return; } + isSaveTaskRunning = true; Queue queue = new LinkedList<>(playerCache.values()); new BukkitRunnable() { @Override @@ -89,6 +96,7 @@ public void run() { for (int i = 0; i < plugin.getSettings().getMaxSavedPlayersPerTick(); i++) { Players player = queue.poll(); if (player == null) { + isSaveTaskRunning = false; cancel(); return; }