From beac0b482815a3435c5daf1360e4f4fb39b567e2 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Sat, 14 Feb 2015 00:47:03 -0800 Subject: [PATCH] Added try catch in BungeeRotator and ServerMonitor so they don't crash out. --- .../src/mineplex/bungee/BungeeRotator.java | 292 ++++++------ .../mineplex/servermonitor/ServerMonitor.java | 424 +++++++++--------- 2 files changed, 374 insertions(+), 342 deletions(-) diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRotator.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRotator.java index 2b8c2d3ea..75b6ea9f9 100644 --- a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRotator.java +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRotator.java @@ -35,148 +35,164 @@ public class BungeeRotator while (true) { - List bungeeServers = _repository.getBungeeServers(); - HashSet usServers = new HashSet(); - HashSet euServers = new HashSet(); - - Collections.sort(bungeeServers, bungeeSorter); - - for (BungeeServer server : bungeeServers) - { - if (usServers.size() < maxRecordCount && server.US) - { - if (usServers.size() >= 2 && server.Players > 900) - continue; - - System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers); - usServers.add(server.Address); - } - else if (euServers.size() < maxRecordCount && !server.US) - { - if (euServers.size() >= 2 && server.Players > 900) - continue; - - System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers); - euServers.add(server.Address); - } - } - - DomainRecords records = new ApiGetCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, - "/records", "").Execute(DomainRecords.class); - List recordsToDelete = new ArrayList(); - List recordsToAdd = new ArrayList(); - - for (DnsRecord record : records.data) - { - if (record.type.equalsIgnoreCase("A")) - { - if (record.name.equalsIgnoreCase("us")) - { - if (usServers.contains(record.value)) - usServers.remove(record.value); - else - recordsToDelete.add(record); - } - else if (record.name.equalsIgnoreCase("eu")) - { - if (euServers.contains(record.value)) - euServers.remove(record.value); - else - recordsToDelete.add(record); - } - } - } - - for (String address : usServers) - { - recordsToAdd.add(new ARecord("us", address, 300)); - System.out.println("Addding server address in DNS : " + "us " + address); - } - - for (String address : euServers) - { - recordsToAdd.add(new ARecord("eu", address, 300)); - System.out.println("Addding server address in DNS : " + "eu " + address); - } - - if (recordsToAdd.size() > 0) - { - new ApiPostCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records/", "createMulti").Execute(recordsToAdd); - System.out.println("Created " + recordsToAdd.size() + " records."); - } - - if (recordsToDelete.size() > 0) - { - StringBuilder idBuilder = new StringBuilder(); - - for (DnsRecord record : recordsToDelete) - { - if (idBuilder.length() != 0) - idBuilder.append("&"); - - idBuilder.append("ids=" + record.id); - } - - new ApiDeleteCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records?" + idBuilder.toString()).Execute(); - System.out.println("Deleted " + recordsToDelete.size() + " records."); - } - - int processWaits = 0; - - while (_processes.size() > 0) - { - for (Iterator iterator = _processes.iterator(); iterator.hasNext();) - { - ProcessRunner pr = iterator.next(); - - try - { - pr.join(100); - } - catch (InterruptedException e) - { - e.printStackTrace(); - } - - if (pr.isDone()) - iterator.remove(); - } - - if (_processes.size() > 0) - { - try - { - Thread.sleep(6000); - } - catch (InterruptedException e) - { - e.printStackTrace(); - } - } - - if (processWaits >= 60) - { - System.out.println("Killing stale processes."); - - for (Iterator iterator = _processes.iterator(); iterator.hasNext();) - { - iterator.next().abort(); - iterator.remove(); - } - } - - processWaits++; - } - - processWaits = 0; - try { - Thread.sleep(30000); + List bungeeServers = _repository.getBungeeServers(); + HashSet usServers = new HashSet(); + HashSet euServers = new HashSet(); + + Collections.sort(bungeeServers, bungeeSorter); + + for (BungeeServer server : bungeeServers) + { + if (usServers.size() < maxRecordCount && server.US) + { + if (usServers.size() >= 2 && server.Players > 900) + continue; + + System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers); + usServers.add(server.Address); + } + else if (euServers.size() < maxRecordCount && !server.US) + { + if (euServers.size() >= 2 && server.Players > 900) + continue; + + System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers); + euServers.add(server.Address); + } + } + + DomainRecords records = new ApiGetCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, + "/records", "").Execute(DomainRecords.class); + List recordsToDelete = new ArrayList(); + List recordsToAdd = new ArrayList(); + + for (DnsRecord record : records.data) + { + if (record.type.equalsIgnoreCase("A")) + { + if (record.name.equalsIgnoreCase("us")) + { + if (usServers.contains(record.value)) + usServers.remove(record.value); + else + recordsToDelete.add(record); + } + else if (record.name.equalsIgnoreCase("eu")) + { + if (euServers.contains(record.value)) + euServers.remove(record.value); + else + recordsToDelete.add(record); + } + } + } + + for (String address : usServers) + { + recordsToAdd.add(new ARecord("us", address, 300)); + System.out.println("Addding server address in DNS : " + "us " + address); + } + + for (String address : euServers) + { + recordsToAdd.add(new ARecord("eu", address, 300)); + System.out.println("Addding server address in DNS : " + "eu " + address); + } + + if (recordsToAdd.size() > 0) + { + new ApiPostCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records/", "createMulti").Execute(recordsToAdd); + System.out.println("Created " + recordsToAdd.size() + " records."); + } + + if (recordsToDelete.size() > 0) + { + StringBuilder idBuilder = new StringBuilder(); + + for (DnsRecord record : recordsToDelete) + { + if (idBuilder.length() != 0) + idBuilder.append("&"); + + idBuilder.append("ids=" + record.id); + } + + new ApiDeleteCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records?" + idBuilder.toString()).Execute(); + System.out.println("Deleted " + recordsToDelete.size() + " records."); + } + + int processWaits = 0; + + while (_processes.size() > 0) + { + for (Iterator iterator = _processes.iterator(); iterator.hasNext();) + { + ProcessRunner pr = iterator.next(); + + try + { + pr.join(100); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + + if (pr.isDone()) + iterator.remove(); + } + + if (_processes.size() > 0) + { + try + { + Thread.sleep(6000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + + if (processWaits >= 60) + { + System.out.println("Killing stale processes."); + + for (Iterator iterator = _processes.iterator(); iterator.hasNext();) + { + iterator.next().abort(); + iterator.remove(); + } + } + + processWaits++; + } + + processWaits = 0; + + try + { + Thread.sleep(30000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } } - catch (InterruptedException e) + catch (Exception ex) { - e.printStackTrace(); + System.out.println("Error doing something : " + ex.getMessage()); + + try + { + Thread.sleep(1000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } } } } diff --git a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java index cd25e2279..beab18166 100644 --- a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java +++ b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java @@ -89,247 +89,263 @@ public class ServerMonitor while (true) { - _totalPlayers = 0; - _serverStatuses = _repository.getServerStatuses(); - _serverGroups = _repository.getServerGroups(_serverStatuses); - _serverGroupMap = new HashMap(); - _dedicatedServers = new ArrayList(_repository.getDedicatedServers()); - - calculateTotalPlayers(); - killDeadServers(); - - double totalCPU = 0.0; - double totalRAM = 0.0; - double availableCPU = 0.0; - double availableRAM = 0.0; - - for (DedicatedServer server : _dedicatedServers) + try { - totalCPU += server.getAvailableCpu(); - totalRAM += server.getAvailableRam(); - } - - for (MinecraftServer minecraftServer : _serverStatuses) - { - if (minecraftServer.getMotd().contains("Finished") || (minecraftServer.getGroup().equalsIgnoreCase("UltraHardcore") && minecraftServer.getMotd().contains("Restarting") && minecraftServer.getPlayerCount() == 0)) - { - killServer(minecraftServer.getName(), minecraftServer.getPublicAddress(), minecraftServer.getPlayerCount(), "[KILLED] [FINISHED] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress(), true); - - handleUserServerGroup(_serverGroupMap.get(minecraftServer.getGroup())); - continue; - } + _totalPlayers = 0; + _serverStatuses = _repository.getServerStatuses(); + _serverGroups = _repository.getServerGroups(_serverStatuses); + _serverGroupMap = new HashMap(); + _dedicatedServers = new ArrayList(_repository.getDedicatedServers()); + calculateTotalPlayers(); + killDeadServers(); + + double totalCPU = 0.0; + double totalRAM = 0.0; + double availableCPU = 0.0; + double availableRAM = 0.0; + for (DedicatedServer server : _dedicatedServers) { - if (_serverGroupMap.containsKey(minecraftServer.getGroup()) && minecraftServer.getPublicAddress().equalsIgnoreCase(server.getPrivateAddress())) - { - ServerGroup serverGroup = _serverGroupMap.get(minecraftServer.getGroup()); - server.incrementServerCount(serverGroup); - } - } - } - - if (_count % 15 == 0) - { - _badServers.clear(); - - for (DedicatedServer serverData : _dedicatedServers) - { - if (isServerOffline(serverData)) - { - log("------=[OFFLINE]=------=[" + serverData.getName() + ":" + serverData.getPublicAddress() + "]=------=[OFFLINE]=------"); - _badServers.put(serverData.getName(), true); - } + totalCPU += server.getAvailableCpu(); + totalRAM += server.getAvailableRam(); } - log(_badServers.size() + " bad servers."); - } - - for (Iterator iterator = _dedicatedServers.iterator(); iterator.hasNext();) - { - DedicatedServer serverData = iterator.next(); - - if (_badServers.containsKey(serverData.getName())) - iterator.remove(); - else + for (MinecraftServer minecraftServer : _serverStatuses) { - availableCPU += serverData.getAvailableCpu(); - availableRAM += serverData.getAvailableRam(); - } - } - - double usedCpuPercent = Math.round((1 - availableCPU / totalCPU) * 10000.0) / 100.0; - double usedRamPercent = Math.round((1 - availableRAM / totalRAM) * 10000.0) / 100.0; - - log("Using " + usedCpuPercent + "% of available CPU (" + availableCPU + " Free) and " + usedRamPercent + "% of available RAM (" + availableRAM / 1000 + "GB Free)"); - - _historyRepository.saveDedicatedServerStats(_dedicatedServers); - log("Saved Dedicated Server Stats."); - _historyRepository.saveServerGroupStats((int)totalCPU, (int)totalRAM, _serverGroupMap.values()); - log("Saved ServerGroup Stats."); - _historyRepository.saveNetworkStats(usedCpuPercent, usedRamPercent, availableCPU, availableRAM, _region); - log("Saved Network Stats."); - - for (ServerGroup groupStatus : _serverGroups) - { - NautHashMap serverMap = new NautHashMap(); - - for (Iterator serverIterator = groupStatus.getServers().iterator(); serverIterator.hasNext();) - { - try + if (minecraftServer.getMotd().contains("Finished") || (minecraftServer.getGroup().equalsIgnoreCase("UltraHardcore") && minecraftServer.getMotd().contains("Restarting") && minecraftServer.getPlayerCount() == 0)) { - MinecraftServer server = serverIterator.next(); - int serverNum = Integer.parseInt(server.getName().split("-")[1]); + killServer(minecraftServer.getName(), minecraftServer.getPublicAddress(), minecraftServer.getPlayerCount(), "[KILLED] [FINISHED] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress(), true); - if (serverMap.containsKey(serverNum)) + handleUserServerGroup(_serverGroupMap.get(minecraftServer.getGroup())); + continue; + } + + for (DedicatedServer server : _dedicatedServers) + { + if (_serverGroupMap.containsKey(minecraftServer.getGroup()) && minecraftServer.getPublicAddress().equalsIgnoreCase(server.getPrivateAddress())) { - killServer(server.getName(), server.getPublicAddress(), server.getPlayerCount(), "[KILLED] [DUPLICATE] " + server.getName() + ":" + server.getPublicAddress(), true); - serverIterator.remove(); + ServerGroup serverGroup = _serverGroupMap.get(minecraftServer.getGroup()); + server.incrementServerCount(serverGroup); + } + } + } + + if (_count % 15 == 0) + { + _badServers.clear(); + + for (DedicatedServer serverData : _dedicatedServers) + { + if (isServerOffline(serverData)) + { + log("------=[OFFLINE]=------=[" + serverData.getName() + ":" + serverData.getPublicAddress() + "]=------=[OFFLINE]=------"); + _badServers.put(serverData.getName(), true); + } + } + + log(_badServers.size() + " bad servers."); + } + + for (Iterator iterator = _dedicatedServers.iterator(); iterator.hasNext();) + { + DedicatedServer serverData = iterator.next(); + + if (_badServers.containsKey(serverData.getName())) + iterator.remove(); + else + { + availableCPU += serverData.getAvailableCpu(); + availableRAM += serverData.getAvailableRam(); + } + } + + double usedCpuPercent = Math.round((1 - availableCPU / totalCPU) * 10000.0) / 100.0; + double usedRamPercent = Math.round((1 - availableRAM / totalRAM) * 10000.0) / 100.0; + + log("Using " + usedCpuPercent + "% of available CPU (" + availableCPU + " Free) and " + usedRamPercent + "% of available RAM (" + availableRAM / 1000 + "GB Free)"); + + _historyRepository.saveDedicatedServerStats(_dedicatedServers); + log("Saved Dedicated Server Stats."); + _historyRepository.saveServerGroupStats((int)totalCPU, (int)totalRAM, _serverGroupMap.values()); + log("Saved ServerGroup Stats."); + _historyRepository.saveNetworkStats(usedCpuPercent, usedRamPercent, availableCPU, availableRAM, _region); + log("Saved Network Stats."); + + for (ServerGroup groupStatus : _serverGroups) + { + NautHashMap serverMap = new NautHashMap(); + + for (Iterator serverIterator = groupStatus.getServers().iterator(); serverIterator.hasNext();) + { + try + { + MinecraftServer server = serverIterator.next(); + int serverNum = Integer.parseInt(server.getName().split("-")[1]); + + if (serverMap.containsKey(serverNum)) + { + killServer(server.getName(), server.getPublicAddress(), server.getPlayerCount(), "[KILLED] [DUPLICATE] " + server.getName() + ":" + server.getPublicAddress(), true); + serverIterator.remove(); + } + else + { + serverMap.put(serverNum, server); + } + } + catch (Exception exception) + { + exception.printStackTrace(); + } + } + + /* + if (groupStatus.getHost() == null || groupStatus.getHost().isEmpty()) + { + if (groupStatus.getName().startsWith("0")) + { + int serverCount = groupStatus.getServers().size(); + log(groupStatus.getName() + " : " + groupStatus.getPlayerCount() + " players on " + serverCount + " servers " + String.format("%.2f", ((double)serverCount * (double)groupStatus.getRequiredCpu() / totalCPU)) + "% CPU," + String.format("%.2f", ((double)serverCount * (double)groupStatus.getRequiredRam() / totalRAM)) + "% RAM", false); + } + } + */ + } + + HashSet onlineServers = new HashSet(); + HashSet laggyServers = new HashSet(); + laggyServers.addAll(_laggyServers); + _laggyServers.clear(); + + for (MinecraftServer minecraftServer : _serverStatuses) + { + onlineServers.add(minecraftServer.getName()); + + if (minecraftServer.getTps() <= 17) + { + if (minecraftServer.getTps() <= 10) + { + if (laggyServers.contains(minecraftServer.getName())) + { + new RestartCommand(minecraftServer.getName(), _region).publish(); + log("[RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress()); + } + else + { + _laggyServers.add(minecraftServer.getName()); + log("[IMPENDING RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress()); + } } else - { - serverMap.put(serverNum, server); - } + log("[Performance] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress() + "] Running poorly at " + minecraftServer.getTps() + " TPS"); + } + } + + for (Iterator>> iterator = serverTracker.entrySet().iterator(); iterator.hasNext();) + { + Entry> entry = iterator.next(); + + if (onlineServers.contains(entry.getKey())) + iterator.remove(); + else if (System.currentTimeMillis() - entry.getValue().getValue() > 35000) + { + String serverName = entry.getKey(); + String serverAddress = entry.getValue().getKey(); + killServer(serverName, serverAddress, 0, "[KILLED] [SLOW-STARTUP] " + serverName + ":" + serverAddress, true); + iterator.remove(); + } + } + + for (ServerGroup serverGroup : _serverGroups) + { + if (serverGroup.getName().equals("Testing")) + continue; + + try + { + handleGroupChanges(serverTracker, serverGroup, false); } catch (Exception exception) { - exception.printStackTrace(); + log(exception.getMessage()); + log("Can't handle group changes for " + serverGroup.getName()); } } - - /* - if (groupStatus.getHost() == null || groupStatus.getHost().isEmpty()) - { - if (groupStatus.getName().startsWith("0")) - { - int serverCount = groupStatus.getServers().size(); - log(groupStatus.getName() + " : " + groupStatus.getPlayerCount() + " players on " + serverCount + " servers " + String.format("%.2f", ((double)serverCount * (double)groupStatus.getRequiredCpu() / totalCPU)) + "% CPU," + String.format("%.2f", ((double)serverCount * (double)groupStatus.getRequiredRam() / totalRAM)) + "% RAM", false); - } - } - */ - } - HashSet onlineServers = new HashSet(); - HashSet laggyServers = new HashSet(); - laggyServers.addAll(_laggyServers); - _laggyServers.clear(); - - for (MinecraftServer minecraftServer : _serverStatuses) - { - onlineServers.add(minecraftServer.getName()); + int processWaits = 0; - if (minecraftServer.getTps() <= 17) + while (_processes.size() > 0) { - if (minecraftServer.getTps() <= 10) + for (Iterator iterator = _processes.iterator(); iterator.hasNext();) { - if (laggyServers.contains(minecraftServer.getName())) + ProcessRunner pr = iterator.next(); + + try { - new RestartCommand(minecraftServer.getName(), _region).publish(); - log("[RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress()); + pr.join(100); + } + catch (InterruptedException e) + { + e.printStackTrace(); } - else + + if (pr.isDone()) + iterator.remove(); + } + + if (_processes.size() > 0) + { + try { - _laggyServers.add(minecraftServer.getName()); - log("[IMPENDING RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress()); + log("Sleeping while processes run..."); + Thread.sleep(6000); + } + catch (InterruptedException e) + { + e.printStackTrace(); } } - else - log("[Performance] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress() + "] Running poorly at " + minecraftServer.getTps() + " TPS"); + + if (processWaits >= 10) + { + log("Killing stale processes."); + + for (Iterator iterator = _processes.iterator(); iterator.hasNext();) + { + iterator.next().abort(); + iterator.remove(); + } + } + + processWaits++; } - } - for (Iterator>> iterator = serverTracker.entrySet().iterator(); iterator.hasNext();) - { - Entry> entry = iterator.next(); - - if (onlineServers.contains(entry.getKey())) - iterator.remove(); - else if (System.currentTimeMillis() - entry.getValue().getValue() > 35000) - { - String serverName = entry.getKey(); - String serverAddress = entry.getValue().getKey(); - killServer(serverName, serverAddress, 0, "[KILLED] [SLOW-STARTUP] " + serverName + ":" + serverAddress, true); - iterator.remove(); - } - } - - for (ServerGroup serverGroup : _serverGroups) - { - if (serverGroup.getName().equals("Testing")) - continue; + processWaits = 0; try { - handleGroupChanges(serverTracker, serverGroup, false); - } - catch (Exception exception) + log("Natural sleep."); + Thread.sleep(10000); + } + catch (InterruptedException e) { - log(exception.getMessage()); - log("Can't handle group changes for " + serverGroup.getName()); - } - } - - int processWaits = 0; - - while (_processes.size() > 0) - { - for (Iterator iterator = _processes.iterator(); iterator.hasNext();) - { - ProcessRunner pr = iterator.next(); - - try - { - pr.join(100); - } - catch (InterruptedException e) - { - e.printStackTrace(); - } - - if (pr.isDone()) - iterator.remove(); + e.printStackTrace(); } - if (_processes.size() > 0) - { - try - { - log("Sleeping while processes run..."); - Thread.sleep(6000); - } - catch (InterruptedException e) - { - e.printStackTrace(); - } - } - - if (processWaits >= 10) - { - log("Killing stale processes."); - - for (Iterator iterator = _processes.iterator(); iterator.hasNext();) - { - iterator.next().abort(); - iterator.remove(); - } - } - - processWaits++; + _count++; } - - processWaits = 0; - - try + catch (Exception ex) { - log("Natural sleep."); - Thread.sleep(10000); - } - catch (InterruptedException e) - { - e.printStackTrace(); + log("Error doing something : " + ex.getMessage()); + + try + { + Thread.sleep(1000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } } - - _count++; } }