Recommit up-time checking to prevent brand new empty servers from being prematurely killed by ServerMonitor.

This commit is contained in:
MrTwiggy 2014-09-15 13:22:11 -04:00
parent c7e0ebefd1
commit abea4223ab
3 changed files with 73 additions and 94 deletions

View File

@ -2,6 +2,7 @@ package mineplex.core.status;
import java.io.File;
import java.util.Collection;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
@ -20,8 +21,8 @@ import mineplex.serverdata.ServerRepository;
public class ServerStatusManager extends MiniPlugin
{
// The default timeout (in seconds) before the ServerStatus expires.
public final int DEFAULT_SERVER_TIMEOUT = 15;
// The default timeout (in milliseconds) before the ServerStatus expires.
public final int DEFAULT_SERVER_TIMEOUT = 15000;
private ServerRepository _repository;
private LagMeter _lagMeter;
@ -29,25 +30,35 @@ public class ServerStatusManager extends MiniPlugin
private String _name;
private boolean _us;
private boolean _alternateSeconds;
private boolean _enabled = true;
private long _startUpDate;
public ServerStatusManager(JavaPlugin plugin, LagMeter lagMeter)
{
super("Server Status Manager", plugin);
this._startUpDate = System.currentTimeMillis();
_lagMeter = lagMeter;
if (new File("IgnoreUpdates.dat").exists())
_enabled = false;
ServerListPingEvent event = new ServerListPingEvent(null, plugin.getServer().getMotd(), plugin.getServer().getOnlinePlayers().length, plugin.getServer().getMaxPlayers());
GetPluginManager().callEvent(event);
setupConfigValues();
String address = Bukkit.getServer().getIp().isEmpty() ? "localhost" : Bukkit.getServer().getIp();
_name = plugin.getConfig().getString("serverstatus.name");
_us = plugin.getConfig().getBoolean("serverstatus.us");
Region region = _us ? Region.US : Region.EU;
_repository = ServerManager.getServerRepository(region);
saveServerStatus();
}
private void setupConfigValues()
@ -106,6 +117,11 @@ public class ServerStatusManager extends MiniPlugin
if (!_enabled)
return;
_alternateSeconds = !_alternateSeconds;
if (!_alternateSeconds)
return;
saveServerStatus();
}
@ -120,7 +136,7 @@ public class ServerStatusManager extends MiniPlugin
{
public void run()
{
_repository.updataServerStatus(serverSnapshot, DEFAULT_SERVER_TIMEOUT);
_repository.updataServerStatus(serverSnapshot, 15000);
}
});
}
@ -131,11 +147,11 @@ public class ServerStatusManager extends MiniPlugin
*/
private MinecraftServer generateServerSnapshot()
{
ServerListPingEvent event = new ServerListPingEvent(null, GetPlugin().getServer().getMotd(), GetPlugin().getServer().getOnlinePlayers().size(), GetPlugin().getServer().getMaxPlayers());
ServerListPingEvent event = new ServerListPingEvent(null, GetPlugin().getServer().getMotd(), GetPlugin().getServer().getOnlinePlayers().length, GetPlugin().getServer().getMaxPlayers());
GetPluginManager().callEvent(event);
String motd = event.getMotd();
int playerCount = Bukkit.getOnlinePlayers().size();
int playerCount = Bukkit.getOnlinePlayers().length;
int maxPlayerCount = event.getMaxPlayers();
int tps = (int) _lagMeter.getTicksPerSecond();
String address = Bukkit.getServer().getIp().isEmpty() ? "localhost" : Bukkit.getServer().getIp();
@ -145,7 +161,7 @@ public class ServerStatusManager extends MiniPlugin
int maxRam = (int) (Runtime.getRuntime().maxMemory() / 1048576);
return new MinecraftServer(_name, group, motd, address, port, playerCount,
maxPlayerCount, tps, ram, maxRam);
maxPlayerCount, tps, ram, maxRam, _startUpDate);
}
public String getCurrentServerName()

View File

@ -50,6 +50,8 @@ public class MinecraftServer
private int _port;
public int getPort() { return _port; }
private long _startUpDate;
/**
* Class constructor
* @param name
@ -64,7 +66,7 @@ public class MinecraftServer
* @param maxRam
*/
public MinecraftServer(String name, String group, String motd, String publicAddress, int port,
int playerCount, int maxPlayerCount, int tps, int ram, int maxRam)
int playerCount, int maxPlayerCount, int tps, int ram, int maxRam, long startUpDate)
{
this._name = name;
this._group = group;
@ -76,6 +78,7 @@ public class MinecraftServer
this._maxRam = maxRam;
this._publicAddress = publicAddress;
this._port = port;
this._startUpDate = startUpDate;
}
/**
@ -86,6 +89,14 @@ public class MinecraftServer
return _playerCount == 0;
}
/**
* @return the amount of time (in seconds) that this {@link MinecraftServer} has been online for.
*/
public double getUptime()
{
return (System.currentTimeMillis() - _startUpDate) / 1000d;
}
/**
* @return true, if this server is currently joinable by players, false otherwise.
*/

View File

@ -51,13 +51,13 @@ public class ServerGroup
public boolean getArcadeGroup() { return _arcadeGroup; }
private String _worldZip;
public String getWorldZip() { return _worldZip; }
public String getWorldZip() { return _serverType; }
private String _plugin;
public String getPlugin() { return _plugin; }
public String getPlugin() { return _serverType; }
private String _configPath;
public String getConfigPath() { return _configPath; }
public String getConfigPath() { return _serverType; }
private int _minPlayers;
public int getMinPlayers() { return _minPlayers; }
@ -71,42 +71,6 @@ public class ServerGroup
private boolean _tournament;
public boolean getTournament() { return _tournament; }
private boolean _teamRejoin;
public boolean getTeamRejoin() { return _teamRejoin; }
private boolean _teamAutoJoin;
public boolean getTeamAutoJoin() { return _teamAutoJoin; }
private boolean _teamForceBalance;
public boolean getTeamForceBalance() { return _teamForceBalance; }
private boolean _gameAutoStart;
public boolean getGameAutoStart() { return _gameAutoStart; }
private boolean _gameTimeout;
public boolean getGameTimeout() { return _gameTimeout; }
private boolean _rewardGems;
public boolean getRewardGems() { return _rewardGems; }
private boolean _rewardItems;
public boolean getRewardItems() { return _rewardItems; }
private boolean _rewardStats;
public boolean getRewardStats() { return _rewardStats; }
private boolean _rewardAchievements;
public boolean getRewardAchievements() { return _rewardAchievements; }
private boolean _hotbarInventory;
public boolean getHotbarInventory() { return _hotbarInventory; }
private boolean _hotbarHubClock;
public boolean getHotbarHubClock() { return _hotbarHubClock; }
private boolean _playerKickIdle;
public boolean getPlayerKickIdle() { return _playerKickIdle; }
private boolean _generateFreeVersions;
public boolean getGenerateFreeVersions() { return _generateFreeVersions; }
@ -120,8 +84,7 @@ public class ServerGroup
public boolean getAddNoCheat() { return _addNoCheat; }
// The set of active MinecraftServers that belong to this server group
private Set<MinecraftServer> _servers;
public Set<MinecraftServer> getServers() { return _servers; }
private Set<MinecraftServer> servers;
/**
* Class constructor
@ -131,38 +94,26 @@ public class ServerGroup
*/
public ServerGroup(Map<String, String> data, Region region)
{
_name = data.get("name");
_prefix = data.get("prefix");
_scriptName = data.get("scriptName");
_requiredRam = Integer.valueOf(data.get("ram"));
_requiredCpu = Integer.valueOf(data.get("cpu"));
_requiredTotalServers = Integer.valueOf(data.get("totalServers"));
_requiredJoinableServers = Integer.valueOf(data.get("joinableServers"));
_portSection = Integer.valueOf(data.get("portSection"));
_arcadeGroup = Boolean.valueOf(data.get("arcadeGroup"));
_worldZip = data.get("worldZip");
_plugin = data.get("plugin");
_configPath = data.get("configPath");
_minPlayers = Integer.valueOf(data.get("minPlayers"));
_maxPlayers = Integer.valueOf(data.get("maxPlayers"));
_pvp = Boolean.valueOf(data.get("pvp"));
_tournament = Boolean.valueOf(data.get("tournament"));
_generateFreeVersions = Boolean.valueOf(data.get("generateFreeVersions"));
_games = data.get("games");
_serverType = data.get("serverType");
_addNoCheat = Boolean.valueOf(data.get("addNoCheat"));
_teamRejoin = Boolean.valueOf(data.get("teamRejoin"));
_teamAutoJoin = Boolean.valueOf(data.get("teamAutoJoin"));
_teamForceBalance = Boolean.valueOf(data.get("teamForceBalance"));
_gameAutoStart = Boolean.valueOf(data.get("gameAutoStart"));
_gameTimeout = Boolean.valueOf(data.get("gameTimeout"));
_rewardGems = Boolean.valueOf(data.get("rewardGems"));
_rewardItems = Boolean.valueOf(data.get("rewardItems"));
_rewardStats = Boolean.valueOf(data.get("rewardStats"));
_rewardAchievements = Boolean.valueOf(data.get("rewardAchievements"));
_hotbarInventory = Boolean.valueOf(data.get("hotbarInventory"));
_hotbarHubClock = Boolean.valueOf(data.get("hotbarHubClock"));
_playerKickIdle = Boolean.valueOf(data.get("playerKickIdle"));
this._name = data.get("name");
this._prefix = data.get("prefix");
this._scriptName = data.get("scriptName");
this._requiredRam = Integer.valueOf(data.get("ram"));
this._requiredCpu = Integer.valueOf(data.get("cpu"));
this._requiredTotalServers = Integer.valueOf(data.get("totalServers"));
this._requiredJoinableServers = Integer.valueOf(data.get("joinableServers"));
this._portSection = Integer.valueOf(data.get("portSection"));
this._arcadeGroup = Boolean.valueOf(data.get("arcadeGroup"));
this._worldZip = data.get("worldZip");
this._plugin = data.get("plugin");
this._configPath = data.get("configPath");
this._minPlayers = Integer.valueOf(data.get("minPlayers"));
this._maxPlayers = Integer.valueOf(data.get("maxPlayers"));
this._pvp = Boolean.valueOf(data.get("pvp"));
this._tournament = Boolean.valueOf(data.get("tournament"));
this._generateFreeVersions = Boolean.valueOf(data.get("generateFreeVersions"));
this._games = data.get("games");
this._serverType = data.get("serverType");
this._addNoCheat = Boolean.valueOf(data.get("addNoCheat"));
fetchServers(region);
}
@ -173,7 +124,7 @@ public class ServerGroup
*/
public int getServerCount()
{
return _servers.size();
return servers.size();
}
/**
@ -184,7 +135,7 @@ public class ServerGroup
{
int joinable = 0;
for (MinecraftServer server : _servers)
for (MinecraftServer server : servers)
{
if (server.isJoinable())
{
@ -203,7 +154,7 @@ public class ServerGroup
{
int playerCount = 0;
for (MinecraftServer server : _servers)
for (MinecraftServer server : servers)
{
playerCount += server.getPlayerCount();
}
@ -219,7 +170,7 @@ public class ServerGroup
{
int maxPlayerCount = 0;
for (MinecraftServer server : _servers)
for (MinecraftServer server : servers)
{
maxPlayerCount += server.getMaxPlayerCount();
}
@ -235,9 +186,9 @@ public class ServerGroup
{
Collection<MinecraftServer> emptyServers = new HashSet<MinecraftServer>();
for (MinecraftServer server : _servers)
for (MinecraftServer server : servers)
{
if (server.isEmpty())
if (server.isEmpty() && server.getUptime() <= 150) // Only return empty servers that have been online for >150 seconds
{
emptyServers.add(server);
}
@ -252,14 +203,14 @@ public class ServerGroup
*/
private void fetchServers(Region region)
{
this._servers = new HashSet<MinecraftServer>();
this.servers = new HashSet<MinecraftServer>();
ServerRepository repository = ServerManager.getServerRepository(region);
for (MinecraftServer server : repository.getServerStatuses())
{
if (_name.equalsIgnoreCase(server.getGroup()))
if (_name.equals(server.getGroup()))
{
_servers.add(server);
servers.add(server);
}
}
}
@ -267,15 +218,15 @@ public class ServerGroup
/**
* @return a unique server name suffix id, unique to any servers in this ServerGroup.
*/
public int generateUniqueId(int startId)
public int generateUniqueId()
{
int id = startId;
int id = 0;
while (true)
{
boolean uniqueId = true;
for (MinecraftServer server : _servers)
for (MinecraftServer server : servers)
{
String serverName = server.getName();
try
@ -304,4 +255,5 @@ public class ServerGroup
}
}
}
}