From 5a975a1195859972891dae4292b1656450e41d52 Mon Sep 17 00:00:00 2001 From: Jerry Date: Thu, 21 Dec 2023 18:18:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E7=94=A8systemd=E6=9D=A5=E5=85=B3?= =?UTF-8?q?=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alpt/vtools/listeners/ServerCloser.java | 55 +++---------------- 1 file changed, 7 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/alpt/vtools/listeners/ServerCloser.java b/src/main/java/com/alpt/vtools/listeners/ServerCloser.java index 6390c66..da897d1 100644 --- a/src/main/java/com/alpt/vtools/listeners/ServerCloser.java +++ b/src/main/java/com/alpt/vtools/listeners/ServerCloser.java @@ -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 { - try { - Runtime.getRuntime().exec(new String[]{"pkill", "pymcd"}); - } catch (IOException e) { - TGBridge.error("关闭 pymcd 时出现问题:" + e.getMessage()); - } - } + String apiUrl = plugin.getConfigOrDefault("azure_api_url", "https://example.com/"); + try { + 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(); }