Added fixed time restart

This commit is contained in:
Norbi Peti 2016-10-27 22:43:44 +02:00
parent 8270c7396d
commit e864ceeb5d

View file

@ -5,6 +5,8 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import jline.console.ConsoleReader; import jline.console.ConsoleReader;
import jline.console.CursorBuffer; import jline.console.CursorBuffer;
@ -12,6 +14,8 @@ import jline.console.CursorBuffer;
public class ServerRunner { public class ServerRunner {
private static final int RESTART_MESSAGE_COUNT = 30; private static final int RESTART_MESSAGE_COUNT = 30;
private static final int interval = 24; // hours
private static volatile String server_version = "1.9.2"; private static volatile String server_version = "1.9.2";
private static volatile boolean stop = false; private static volatile boolean stop = false;
@ -60,13 +64,9 @@ public class ServerRunner {
try { try {
String readLine; String readLine;
while (!stop && (readLine = reader.readLine()) != null) { while (!stop && (readLine = reader.readLine()) != null) {
/*
* if (readLine.equalsIgnoreCase("restart")) output.println("stop"); else {
*/
if (readLine.equalsIgnoreCase("stop")) if (readLine.equalsIgnoreCase("stop"))
ServerRunner.stop(); ServerRunner.stop();
serveroutput.println(readLine); serveroutput.println(readLine);
// } // TODO: RunnerStates, stop Input- and OutputThread and restart them after backup?
serveroutput.flush(); serveroutput.flush();
} }
} catch (IOException e) { } catch (IOException e) {
@ -83,7 +83,7 @@ public class ServerRunner {
public void run() { public void run() {
try { try {
BufferedReader serverinput = new BufferedReader( BufferedReader serverinput = new BufferedReader(
new InputStreamReader(serverprocess.getInputStream())); new InputStreamReader(serverprocess.getInputStream(), StandardCharsets.UTF_8));
String line; String line;
while (true) { while (true) {
if ((line = serverinput.readLine()) != null) { if ((line = serverinput.readLine()) != null) {
@ -116,16 +116,20 @@ public class ServerRunner {
ServerRunner.stop(); ServerRunner.stop();
writeToScreen("Stopped " + Thread.currentThread().getName()); writeToScreen("Stopped " + Thread.currentThread().getName());
} }
}; // TODO: Rename start.sh and put empty one };
ot.setName("OutputThread"); ot.setName("OutputThread");
ot.start(); ot.start();
Thread.currentThread().setName("RestarterThread"); Thread.currentThread().setName("RestarterThread");
long starttime = syncStart(3);
boolean firstrun = true;
while (!stop) { while (!stop) {
try { try {
if (restartcounter >= 0) { if (restartcounter >= 0) {
if (restartcounter == RESTART_MESSAGE_COUNT) if (restartcounter == RESTART_MESSAGE_COUNT)
Thread.sleep(24 * 60 * 60 * 1000); if (firstrun)
// Thread.sleep(10000); Thread.sleep(starttime);
else
Thread.sleep(interval * 3600000);
else if (restartcounter > 0) { else if (restartcounter > 0) {
sendMessage(serveroutput, "red", "-- Server restarting in " + restartcounter + " seconds!"); sendMessage(serveroutput, "red", "-- Server restarting in " + restartcounter + " seconds!");
Thread.sleep(1000); // TODO: Change to bossbar? (plugin) Thread.sleep(1000); // TODO: Change to bossbar? (plugin)
@ -143,8 +147,8 @@ public class ServerRunner {
} }
private static Process startServer(String minmem, String maxmem) throws IOException { private static Process startServer(String minmem, String maxmem) throws IOException {
return Runtime.getRuntime().exec(new String[] { "java", "-Xms" + minmem, "-Xmx" + maxmem, return Runtime.getRuntime().exec(new String[] { "java", "-Djline.terminal=jline.UnixTerminal", "-Xms" + minmem,
"-XX:MaxPermSize=128M", "-jar", "spigot-" + server_version + ".jar" }); "-Xmx" + maxmem, "-XX:MaxPermSize=128M", "-jar", "spigot-" + server_version + ".jar" });
} }
private static void sendMessage(PrintWriter output, String color, String text) { private static void sendMessage(PrintWriter output, String color, String text) {
@ -183,4 +187,20 @@ public class ServerRunner {
// ignore // ignore
} }
} }
private static double hoursOf(Date parsedTime) {
return parsedTime.getHours() + parsedTime.getMinutes() / 60. + parsedTime.getSeconds() / 3600.;
}
private static long syncStart(double startHour) { // Copied original code from SimpleBackup
double now = hoursOf(new Date());
double diff = now - startHour;
if (diff < 0) {
diff += 24;
}
double intervalPart = diff - Math.floor(diff / interval) * interval;
double remaining = interval - intervalPart;
return (long) (remaining * 3600000);
}
} }