Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve backup functionality #3258

Merged
merged 9 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Essentials/src/com/earth2me/essentials/Backup.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -20,11 +22,13 @@ public class Backup implements Runnable {
private transient boolean running = false;
private transient int taskId = -1;
private transient boolean active = false;
private final AtomicBoolean pendingShutdown = new AtomicBoolean(false);
private transient CompletableFuture<Object> taskLock = null;

public Backup(final IEssentials ess) {
this.ess = ess;
server = ess.getServer();
if (!ess.getOnlinePlayers().isEmpty()) {
if (!ess.getOnlinePlayers().isEmpty() || ess.getSettings().isAlwaysRunBackup()) {
ess.runTaskAsynchronously(this::startTask);
}
}
Expand Down Expand Up @@ -52,12 +56,21 @@ private synchronized void startTask() {
}
}

public CompletableFuture<Object> getTaskLock() {
return taskLock;
}

public void setPendingShutdown(boolean shutdown) {
pendingShutdown.set(shutdown);
}

@Override
public void run() {
if (active) {
return;
}
active = true;
taskLock = new CompletableFuture<>();
final String command = ess.getSettings().getBackupCommand();
if (command == null || "".equals(command)) {
return;
Expand All @@ -66,6 +79,7 @@ public void run() {
final CommandSender cs = server.getConsoleSender();
server.dispatchCommand(cs, "save-all");
active = false;
taskLock.complete(new Object());
return;
}
LOGGER.log(Level.INFO, tl("backupStarted"));
Expand Down Expand Up @@ -102,14 +116,17 @@ class BackupEnableSaveTask implements Runnable {
@Override
public void run() {
server.dispatchCommand(cs, "save-on");
if (ess.getOnlinePlayers().isEmpty()) {
if (!ess.getSettings().isAlwaysRunBackup() && ess.getOnlinePlayers().isEmpty()) {
stopTask();
}
active = false;
taskLock.complete(new Object());
LOGGER.log(Level.INFO, tl("backupFinished"));
}
}
ess.scheduleSyncDelayedTask(new BackupEnableSaveTask());
if (!pendingShutdown.get()) {
ess.scheduleSyncDelayedTask(new BackupEnableSaveTask());
}
}
});
}
Expand Down
12 changes: 11 additions & 1 deletion Essentials/src/com/earth2me/essentials/Essentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public void onEnable() {
handleCrash(ex);
throw ex;
}
getBackup().setPendingShutdown(false);
}

@Override
Expand Down Expand Up @@ -363,12 +364,17 @@ private void registerListeners(PluginManager pm) {

@Override
public void onDisable() {
boolean stopping = ServerState.isStopping();
if (!stopping) {
LOGGER.log(Level.SEVERE, tl("serverReloading"));
}
getBackup().setPendingShutdown(true);
for (User user : getOnlineUsers()) {
if (user.isVanished()) {
user.setVanished(false);
user.sendMessage(tl("unvanishedReload"));
}
if (ServerState.isStopping()) {
if (stopping) {
user.setLastLocation();
if (!user.isHidden()) {
user.setLastLogout(System.currentTimeMillis());
Expand All @@ -379,6 +385,10 @@ public void onDisable() {
}
}
cleanupOpenInventories();
if (getBackup().getTaskLock() != null && !getBackup().getTaskLock().isDone()) {
LOGGER.log(Level.SEVERE, tl("backupInProgress"));
getBackup().getTaskLock().join();
}
if (i18n != null) {
i18n.onDisable();
}
Expand Down
2 changes: 2 additions & 0 deletions Essentials/src/com/earth2me/essentials/ISettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public interface ISettings extends IConf {

long getBackupInterval();

boolean isAlwaysRunBackup();

String getChatFormat(String group);

int getChatRadius();
Expand Down
5 changes: 5 additions & 0 deletions Essentials/src/com/earth2me/essentials/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ public String getBackupCommand() {
return config.getString("backup.command", null);
}

@Override
public boolean isAlwaysRunBackup() {
return config.getBoolean("backup.always-run", false);
}

private final Map<String, String> chatFormats = Collections.synchronizedMap(new HashMap<>());

@Override
Expand Down
2 changes: 2 additions & 0 deletions Essentials/src/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ unprotected-sign-names:
backup:
# Interval in minutes.
interval: 30
# If true, the backup task will run even if there are no players online.
always-run: false
# Unless you add a valid backup command or script here, this feature will be useless.
# Use 'save-all' to simply force regular world saving without backup.
#command: 'rdiff-backup World1 backups/World1'
Expand Down
2 changes: 2 additions & 0 deletions Essentials/src/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ backOther=\u00a76Returned\u00a7c {0}\u00a76 to previous location.
backupDisabled=\u00a74An external backup script has not been configured.
backupFinished=\u00a76Backup finished.
backupStarted=\u00a76Backup started.
backupInProgress=\u00a76An external backup script is currently in progress! Halting plugin disable until finished.
backUsageMsg=\u00a76Returning to previous location.
balance=\u00a7aBalance\:\u00a7c {0}
balanceOther=\u00a7aBalance of {0}\u00a7a\:\u00a7c {1}
Expand Down Expand Up @@ -484,6 +485,7 @@ seenOnline=\u00a76Player\u00a7c {0} \u00a76has been \u00a7aonline\u00a76 since \
sellBulkPermission=\u00a76You do not have permission to bulk sell.
sellHandPermission=\u00a76You do not have permission to hand sell.
serverFull=Server is full\!
serverReloading=There's a good chance you're probably reloading your server right now. If that's the case, why do you hate yourself? Except no support from the Essentials team when using /reload
serverTotal=\u00a76Server Total\:\u00a7c {0}
serverUnsupported=You are running an unsupported server version!
setBal=\u00a7aYour balance was set to {0}.
Expand Down