From c2bec980638ca5835df5a77a1ba40ad09c71e5fa Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 17 Jan 2024 16:20:24 +0100 Subject: [PATCH] Checkpoint idle server --- BauSystem_Main/src/BauSystem.properties | 2 +- BauSystem_Main/src/BauSystem_de.properties | 2 +- .../src/de/steamwar/bausystem/BauSystem.java | 19 --- .../features/world/AFKStopperListener.java | 41 +++--- .../bausystem/features/world/RamUsage.java | 120 ------------------ 5 files changed, 19 insertions(+), 165 deletions(-) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index c7f98ce5..f9c2afde 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -902,7 +902,7 @@ REGION_TNT_NO_PERMS=§cYou are not allowed to toggle tnt damage here REGION_TNT_BUILD_DESTROY=§cAn explosion would have destroyed blocks in the building area REGION_TNT_TB_DESTROY=§cAn explosion would have destroyed blocks in the testblock area -AFK_KICK_MESSAGE=§cNothing happened on this server for 5 minutes. +AFK_KICK_MESSAGE=§cNothing happened on this server for 15 minutes. AFK_WARNING_MESSAGE=§cThis server will stop in one minute if you remain inactive SKIN_HELP = §8/§eskin §8[§7Shortform§8] §8[§7Creator§8|§epublic§8] §8[§7Name...§8] §8- §7Creates the skin schematic. Use 'public' as creator to have no creator, then copy the message to YoyoNow by clicking diff --git a/BauSystem_Main/src/BauSystem_de.properties b/BauSystem_Main/src/BauSystem_de.properties index 2186282a..871e66de 100644 --- a/BauSystem_Main/src/BauSystem_de.properties +++ b/BauSystem_Main/src/BauSystem_de.properties @@ -863,7 +863,7 @@ REGION_TNT_TB=§aTNT-Schaden außerhalb Baurahmen aktiviert REGION_TNT_NO_PERMS=§cDu darfst hier nicht TNT-Schaden (de-)aktivieren REGION_TNT_BUILD_DESTROY=§cEine Explosion hätte Blöcke im Baubereich zerstört REGION_TNT_TB_DESTROY=§cEine Explosion hätte Blöcke im Testblockbereich zerstört -AFK_KICK_MESSAGE=§cAuf diesem Server ist seit 5 Minuten nichts passiert. +AFK_KICK_MESSAGE=§cAuf diesem Server ist seit 15 Minuten nichts passiert. AFK_WARNING_MESSAGE=§cDieser Server wird bei weiterer Inaktivität in einer Minute gestoppt SKIN_HELP = §8/§eskin §8[§7Kuerzel§8] §8[§7Creator§8|§epublic§8] §8[§7Name...§8] §8- §7Erstellt die Skin Schematics. 'public' als Creator nutzen für keinen Creator, danach die nachricht an YoyoNow kopieren (mit Click kopieren) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 10b36297..4b62243f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -22,7 +22,6 @@ package de.steamwar.bausystem; import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.bausystem.configplayer.Config; import de.steamwar.bausystem.features.tpslimit.TPSFreezeUtils; -import de.steamwar.bausystem.features.world.RamUsage; import de.steamwar.bausystem.linkage.LinkageUtils; import de.steamwar.bausystem.region.loader.PrototypeLoader; import de.steamwar.bausystem.region.loader.RegionLoader; @@ -76,25 +75,7 @@ public class BauSystem extends JavaPlugin implements Listener { new Updater(RegionLoader.file, RegionLoader::load); LinkageUtils.link(); - RamUsage.init(); TickListener.impl.init(); - - // This could disable any watchdog stuff. We need to investigate if this is a problem. - /* - Thread thread = new Thread(() -> { - while (true) { - WatchdogThread.tick(); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - }); - thread.setName("WatchdogThread ticker"); - thread.setDaemon(true); - thread.start(); - */ } @Override diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java index 52249a23..70434d25 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java @@ -20,63 +20,56 @@ package de.steamwar.bausystem.features.world; import de.steamwar.bausystem.BauSystem; +import de.steamwar.core.CheckpointUtils; import de.steamwar.linkage.Linked; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; @Linked public class AFKStopperListener implements Listener { - // CPU > 50% - // RAM > 60% - private int afkTicks = 0; public AFKStopperListener() { Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { - // System.out.println("CPU: " + load + " RAM: " + usage); - if (Bukkit.getOnlinePlayers().isEmpty()) { - if (RamUsage.getLoad() >= 50.0 || RamUsage.getUsage() >= 0.6) { - Bukkit.shutdown(); - return; - } - } else if (RamUsage.getLoad() < 50.0 && RamUsage.getUsage() < 0.6) { - afkTicks = 0; - return; - } switch (afkTicks) { - case 90: - Bukkit.shutdown(); - break; case 15: for (Player p : Bukkit.getOnlinePlayers()) { p.kickPlayer(BauSystem.MESSAGE.parse("AFK_KICK_MESSAGE", p)); } - case 12: + case 14: BauSystem.MESSAGE.broadcast("AFK_WARNING_MESSAGE"); default: afkTicks++; } - }, 20*60, 20*60); //every minute + }, 1200, 1200); //every minute } @EventHandler public void onPlayerMove(PlayerMoveEvent event) { - if (event.getTo() == null) return; - if (event.getFrom().getPitch() != event.getTo().getPitch()) { + Location to = event.getTo(); + if (to == null) + return; + + Location from = event.getFrom(); + if (from.getPitch() != to.getPitch() || from.getYaw() != to.getYaw()) afkTicks = 0; - } - if (event.getFrom().getYaw() != event.getTo().getYaw()) { - afkTicks = 0; - } } @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { event.getPlayer().setOp(true); } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + if(Bukkit.getOnlinePlayers().isEmpty() || (Bukkit.getOnlinePlayers().size() == 1 && Bukkit.getOnlinePlayers().contains(event.getPlayer()))) + CheckpointUtils.freeze(); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java deleted file mode 100644 index 1d187444..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2021 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.bausystem.features.world; - -import lombok.experimental.UtilityClass; - -import java.io.*; -import java.util.concurrent.locks.LockSupport; - -@UtilityClass -public class RamUsage { - - private File meminfo = new File("/proc/meminfo"); - private File stat = new File("/proc/stat"); - - private double usageSelf = 0D; - private double usage = 0D; - private double load = 0D; - - public static void init() { - } - - static { - Thread thread = new Thread(() -> { - while (true) { - long maxMemory = Runtime.getRuntime().maxMemory(); - long totalMemory = Runtime.getRuntime().totalMemory(); - long freeMemory = Runtime.getRuntime().freeMemory(); - long usedMemory = totalMemory - freeMemory; - usageSelf = usedMemory / (double) totalMemory; - double usageSelfByMax = usedMemory / (double) maxMemory; - // System.out.println("Self: " + usageSelf + "/" + usageSelfByMax + /* " " + maxMemory + " " + totalMemory + " " + freeMemory + " " + usedMemory + */ " Ram: " + usage + " CPU: " + load); - - usage = _getUsage(); - load = _getLoad(); - Thread.yield(); - LockSupport.parkNanos(1000000000L); - } - }); - thread.setDaemon(true); - thread.setName("RamUsage"); - thread.start(); - } - - public static double getUsage() { - return usage; - } - - private static double _getUsage() { - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(meminfo)))) { - String memTotal = bufferedReader.readLine().replaceAll(" +", " "); - bufferedReader.readLine(); - String memAvailable = bufferedReader.readLine().replaceAll(" +", " "); - - long memTotalLong = getNumber(memTotal); - long memAvailableLong = getNumber(memAvailable); - return (memTotalLong - memAvailableLong) / (double) memTotalLong; - } catch (IOException e) { - return 1D; - } - } - - public static double getLoad() { - return load; - } - - private long lastCpuSecond = -1; - private long lastCpuForth = -1; - private long lastCpuFifth = -1; - - private static double _getLoad() { - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(stat)))) { - String[] strings = bufferedReader.readLine().split(" "); - - long cpuSecond = Long.parseLong(strings[2]); - long cpuForth = Long.parseLong(strings[4]); - long cpuFifth = Long.parseLong(strings[5]); - - if (lastCpuSecond == -1) { - lastCpuSecond = cpuSecond; - lastCpuForth = cpuForth; - lastCpuFifth = cpuFifth; - return 0D; - } - - long cpuSecondDiff = cpuSecond - lastCpuSecond; - long cpuForthDiff = cpuForth - lastCpuForth; - long cpuSixthDiff = cpuFifth - lastCpuFifth; - - lastCpuSecond = cpuSecond; - lastCpuForth = cpuForth; - lastCpuFifth = cpuFifth; - - return (cpuSecondDiff + cpuForthDiff) / (double) (cpuSecondDiff + cpuForthDiff + cpuSixthDiff); - } catch (IOException e) { - return 1D; - } - } - - private static long getNumber(String s) { - return Long.parseLong(s.split(" ")[1]); - } -}