1
0
Fork 0
forked from mc/VTools

调用systemd来关机

This commit is contained in:
JerryXiao 2023-12-21 18:18:08 +08:00 committed by Sodium-Aluminate
parent 513ba7ad8c
commit 5a975a1195

View file

@ -6,7 +6,6 @@ import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.proxy.ProxyServer;
import de.strifel.VTools.VTools;
import de.strifel.VTools.listeners.TGBridge;
import okhttp3.*;
import java.io.IOException;
import java.util.Objects;
@ -14,11 +13,7 @@ import java.util.concurrent.LinkedBlockingQueue;
public class ServerCloser {
private static final long MINUTE = 60L * 1000;
private static final OkHttpClient CLIENT = new OkHttpClient();
private static final String ACTION_STOP_JSON = "{\"action\":\"stop\"}";
@SuppressWarnings("java:S1068")
private static final String ACTION_START_JSON = "{\"action\":\"start\"}";
private final VTools plugin;
private final ProxyServer server;
@ -31,28 +26,6 @@ public class ServerCloser {
this.server = plugin.getServer();
}
private boolean executeAzure() {
return executeAzure(ACTION_STOP_JSON);
}
private boolean executeAzure(String action) {
RequestBody requestBody = RequestBody.create(action, MediaType.parse("application/json"));
Request request = new Request.Builder()
.url(plugin.getConfigOrDefault("azure_api_url", "https://example.com/"))
.post(requestBody)
.addHeader("Content-Type", "application/json")
.build();
try (Response response = CLIENT.newCall(request).execute()) {
String body = response.body() == null ? "null" : response.body().string();
plugin.logger.info("ServerCloser: http request response: {} ({}).", body, response.code());
return (response.code() >= 200 && response.code() < 300);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private void close() {
if (!INSTANCE.server.getAllPlayers().isEmpty()) {
@ -60,24 +33,10 @@ public class ServerCloser {
TGBridge.error("ServerCloser: #bug @NaAlOH4 定时器到点时发现服务器有人。这不应发生,因为定时器本应该被打断。");
return;
}
boolean httpSuccess = false;
for (int i = 0; i < 3; i++) {
httpSuccess = executeAzure();
if (httpSuccess) {
TGBridge.log("ServerCloser: 向 azure 发送关机命令成功,正在关闭 pymcd.");
TGBridge.setShuttingDown(0);
break;
}
}
if (!httpSuccess) {
TGBridge.error("服务器关机 http 请求失效,服务器可能没有正常关闭。");
} else {
String apiUrl = plugin.getConfigOrDefault("azure_api_url", "https://example.com/");
try {
Runtime.getRuntime().exec(new String[]{"pkill", "pymcd"});
} catch (IOException e) {
TGBridge.error("关闭 pymcd 时出现问题:" + e.getMessage());
}
}
Runtime.getRuntime().exec(new String[]{"systemd-run", "--description=pymcd_stop", "--quiet", "--user", "--collect", "-p", "StandardInput=data", "-p", "StandardInputData=c2V0IC1lCnA9IiQocGdyZXAgLWYgcHltY2QucHkgLUFvKSIKa2lsbCAtSU5UICIkcCIKdGFpbCAtLXBpZD0iJHAiIC1mIC9kZXYvbnVsbApjdXJsIC1zIC1IICJDb250ZW50LVR5cGU6IGFwcGxpY2F0aW9uL2pzb24iIC1kICJ7XCJhY3Rpb25cIjpcInN0b3BcIn0iICIkMSIK", "bash", "-s", apiUrl});
} catch (IOException ignored) {}
}
private boolean firstInit;
@ -142,7 +101,7 @@ public class ServerCloser {
while (!lock.isEmpty()) {
lock.poll().cancel();
}
lock.add(new Counter(INSTANCE, 1,"收到关机命令已经过了 %s 分钟,即将关机。").start());
lock.add(new Counter(INSTANCE, 1, "收到关机命令已经过了 %s 分钟,即将关机。").start());
}
}
@ -155,8 +114,8 @@ public class ServerCloser {
}
}
public void noShutdown(){
synchronized (lock){
public void noShutdown() {
synchronized (lock) {
while (!lock.isEmpty()) {
lock.poll().cancel();
}