uprava tridy pro restart serveru - nyní přes restart service
parent
8be20b9278
commit
72bffeebd9
|
|
@ -4,12 +4,12 @@ import io.netty.channel.ChannelHandlerContext;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import jnet.lib.LogFile;
|
||||
import jnet.lib.Message;
|
||||
import jnet.server.Server;
|
||||
|
||||
|
||||
public class RegisterServerRestart implements Register {
|
||||
|
||||
public RegisterServerRestart() {
|
||||
|
|
@ -17,32 +17,56 @@ public class RegisterServerRestart implements Register {
|
|||
|
||||
@Override
|
||||
public void processing(ChannelHandlerContext ctx, Message msg) {
|
||||
restartServer();
|
||||
restartService();
|
||||
}
|
||||
|
||||
private void restartServer() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
try {
|
||||
String java = System.getProperty("java.home") + "/bin/java";
|
||||
File jarFile = new File(Server.class
|
||||
.getProtectionDomain().getCodeSource().getLocation().getPath());
|
||||
|
||||
List<String> command = new ArrayList<>();
|
||||
command.add(java);
|
||||
command.add("-jar");
|
||||
command.add(jarFile.getAbsolutePath());
|
||||
public static void restartService() {
|
||||
try {
|
||||
ProcessBuilder builder = new ProcessBuilder(
|
||||
"sudo", "systemctl", "restart", "jnetserver.service"
|
||||
);
|
||||
builder.inheritIO(); // pro viditelnost výstupu v konzoli
|
||||
builder.start();
|
||||
|
||||
new ProcessBuilder(command).start();
|
||||
System.exit(0);
|
||||
} catch (IOException ex) {
|
||||
LogFile.printErr("Restart failed! " + ex.getMessage());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
LogFile.printErr("Restart failed! " + e.getMessage());
|
||||
}
|
||||
}).start();
|
||||
// Po zavolání restartu službu ukončíme
|
||||
// systemd službu automaticky znovu spustí
|
||||
//System.exit(0);
|
||||
|
||||
} catch (IOException e) {
|
||||
LogFile.printErr("Restart error: " +e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void restartServer() {
|
||||
try {
|
||||
// Získání cesty k aktuálnímu JAR souboru
|
||||
String jarPath = new File(Server.class.getProtectionDomain()
|
||||
.getCodeSource()
|
||||
.getLocation()
|
||||
.toURI())
|
||||
.getAbsolutePath();
|
||||
|
||||
// Získání původních argumentů (pokud byly použity)
|
||||
String[] originalArgs = System.getProperty("sun.java.command").split(" ");
|
||||
|
||||
// Sestavení příkazu
|
||||
List<String> command = new ArrayList<>();
|
||||
command.add(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java");
|
||||
command.add("-jar");
|
||||
command.add(jarPath);
|
||||
command.addAll(Arrays.asList(originalArgs)); // přidání původních argumentů
|
||||
|
||||
// Spuštění nového procesu
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(command);
|
||||
processBuilder.inheritIO(); // převzetí stdout/stderr
|
||||
processBuilder.start();
|
||||
|
||||
// Ukončení aktuálního procesu
|
||||
System.exit(0);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
// Chyba - nelze restartovat
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue