Skip to content

Commit

Permalink
Prevent save task overlaps (#1588)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgdc3 authored Nov 27, 2020
1 parent 232d909 commit b74eeb9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/world/bentobox/bentobox/BentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class IslandsManager {
@NonNull
private List<String> deletedIslands;

private boolean isSaveTaskRunning;

/**
* Islands Manager
* @param plugin - plugin
Expand Down Expand Up @@ -1209,6 +1211,10 @@ public void removePlayersFromIsland(Island island) {
});
}

public boolean isSaveTaskRunning() {
return isSaveTaskRunning;
}

/**
* Save the all the islands to the database
*/
Expand All @@ -1232,13 +1238,16 @@ public void saveAll(boolean schedule){
return;
}


isSaveTaskRunning = true;
Queue<Island> queue = new LinkedList<>(islandCache.getIslands());
new BukkitRunnable() {
@Override
public void run() {
for (int i = 0; i < plugin.getSettings().getMaxSavedIslandsPerTick(); i++) {
Island island = queue.poll();
if (island == null) {
isSaveTaskRunning = false;
cancel();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class PlayersManager {
private Map<UUID, Players> playerCache;
private Set<UUID> inTeleport;

private boolean isSaveTaskRunning;

/**
* Provides a memory cache of online player information
* This is the one-stop-shop of player info
Expand Down Expand Up @@ -59,6 +61,10 @@ public void load(){
handler.loadObjects().forEach(p -> playerCache.put(p.getPlayerUUID(), p));
}

public boolean isSaveTaskRunning() {
return isSaveTaskRunning;
}

/**
* Save all players
*/
Expand All @@ -82,13 +88,15 @@ public void saveAll(boolean schedule){
return;
}

isSaveTaskRunning = true;
Queue<Players> queue = new LinkedList<>(playerCache.values());
new BukkitRunnable() {
@Override
public void run() {
for (int i = 0; i < plugin.getSettings().getMaxSavedPlayersPerTick(); i++) {
Players player = queue.poll();
if (player == null) {
isSaveTaskRunning = false;
cancel();
return;
}
Expand Down

0 comments on commit b74eeb9

Please sign in to comment.