Cleanup & output result of backup command

This commit is contained in:
snowleo 2011-08-27 17:50:44 +02:00
parent d3302dea00
commit 033babd586

View File

@ -1,35 +1,45 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftServer;
public class Backup implements Runnable {
private static final Logger logger = Logger.getLogger("Minecraft");
private final CraftServer server;
private final IEssentials ess;
private boolean running = false;
private int taskId = -1;
private boolean active = false;
public Backup(IEssentials ess) { public class Backup implements Runnable
{
private static final Logger LOGGER = Logger.getLogger("Minecraft");
private transient final CraftServer server;
private transient final IEssentials ess;
private transient boolean running = false;
private transient int taskId = -1;
private transient boolean active = false;
public Backup(final IEssentials ess)
{
this.ess = ess; this.ess = ess;
server = (CraftServer)ess.getServer(); server = (CraftServer)ess.getServer();
if (server.getOnlinePlayers().length > 0) { if (server.getOnlinePlayers().length > 0)
{
startTask(); startTask();
} }
} }
void onPlayerJoin() { void onPlayerJoin()
{
startTask(); startTask();
} }
private void startTask() { private void startTask()
if (!running) { {
long interval = ess.getSettings().getBackupInterval()*1200; // minutes -> ticks if (!running)
if (interval < 1200) { {
final long interval = ess.getSettings().getBackupInterval() * 1200; // minutes -> ticks
if (interval < 1200)
{
return; return;
} }
taskId = ess.scheduleSyncRepeatingTask(this, interval, interval); taskId = ess.scheduleSyncRepeatingTask(this, interval, interval);
@ -37,48 +47,84 @@ public class Backup implements Runnable {
} }
} }
public void run() { public void run()
if (active) return; {
active = true; if (active)
final String command = ess.getSettings().getBackupCommand(); {
if (command == null || "".equals(command)) {
return; return;
} }
logger.log(Level.INFO, Util.i18n("backupStarted")); active = true;
final String command = ess.getSettings().getBackupCommand();
if (command == null || "".equals(command))
{
return;
}
LOGGER.log(Level.INFO, Util.i18n("backupStarted"));
final CommandSender cs = server.getServer().console; final CommandSender cs = server.getServer().console;
server.dispatchCommand(cs, "save-all"); server.dispatchCommand(cs, "save-all");
server.dispatchCommand(cs, "save-off"); server.dispatchCommand(cs, "save-off");
ess.scheduleAsyncDelayedTask( ess.scheduleAsyncDelayedTask(
new Runnable() { new Runnable()
{
public void run() { public void run()
try { {
Process child = Runtime.getRuntime().exec(command); try
child.waitFor(); {
} catch (InterruptedException ex) { final ProcessBuilder childBuilder = new ProcessBuilder(command);
logger.log(Level.SEVERE, null, ex); childBuilder.redirectErrorStream(true);
} catch (IOException ex) { childBuilder.directory(ess.getDataFolder().getParentFile().getParentFile());
logger.log(Level.SEVERE, null, ex); final Process child = childBuilder.start();
} finally { final BufferedReader reader = new BufferedReader(new InputStreamReader(child.getInputStream()));
ess.scheduleSyncDelayedTask( try
new Runnable() { {
child.waitFor();
public void run() { String line;
server.dispatchCommand(cs, "save-on"); do
if (server.getOnlinePlayers().length == 0) { {
running = false; line = reader.readLine();
if (taskId != -1) { if (line != null)
server.getScheduler().cancelTask(taskId); {
LOGGER.log(Level.INFO, line);
}
} }
while (line != null);
}
finally
{
reader.close();
} }
active = false;
logger.log(Level.INFO, Util.i18n("backupFinished"));
} }
}); catch (InterruptedException ex)
} {
} LOGGER.log(Level.SEVERE, null, ex);
}); }
catch (IOException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
}
finally
{
ess.scheduleSyncDelayedTask(
new Runnable()
{
public void run()
{
server.dispatchCommand(cs, "save-on");
if (server.getOnlinePlayers().length == 0)
{
running = false;
if (taskId != -1)
{
server.getScheduler().cancelTask(taskId);
}
}
active = false;
LOGGER.log(Level.INFO, Util.i18n("backupFinished"));
}
});
}
}
});
} }
} }