Added proper logging to BungeeRotator

This commit is contained in:
Jonathan Williams 2015-02-14 13:49:09 -08:00
parent 6ff0928fbd
commit c50e79355f

View File

@ -1,10 +1,16 @@
package mineplex.bungee;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import mineplex.bungee.api.ApiDeleteCall;
import mineplex.bungee.api.ApiGetCall;
@ -16,8 +22,10 @@ import mineplex.bungee.api.token.DomainRecords;
public class BungeeRotator
{
private static BungeeRepository _repository = null;
private static HashSet<ProcessRunner> _processes = new HashSet<ProcessRunner>();
private static SimpleDateFormat _dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
private static Logger _logger = Logger.getLogger("BungeeRotator");
public static void main(String args[])
{
try
@ -28,6 +36,25 @@ public class BungeeRotator
{
e1.printStackTrace();
}
try
{
FileHandler fileHandler = new FileHandler("rotator.log", true);
fileHandler.setFormatter(new Formatter()
{
@Override
public String format(LogRecord record)
{
return record.getMessage() + "\n";
}
});
_logger.addHandler(fileHandler);
_logger.setUseParentHandlers(false);
}
catch (SecurityException | IOException e1)
{
e1.printStackTrace();
}
_repository = new BungeeRepository();
BungeeSorter bungeeSorter = new BungeeSorter();
@ -50,7 +77,7 @@ public class BungeeRotator
if (usServers.size() >= 2 && server.Players > 900)
continue;
System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers);
log("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers);
usServers.add(server.Address);
}
else if (euServers.size() < maxRecordCount && !server.US)
@ -58,7 +85,7 @@ public class BungeeRotator
if (euServers.size() >= 2 && server.Players > 900)
continue;
System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers);
log("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers);
euServers.add(server.Address);
}
}
@ -92,19 +119,19 @@ public class BungeeRotator
for (String address : usServers)
{
recordsToAdd.add(new ARecord("us", address, 300));
System.out.println("Addding server address in DNS : " + "us " + address);
log("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);
log("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.");
log("Created " + recordsToAdd.size() + " records.");
}
if (recordsToDelete.size() > 0)
@ -120,61 +147,13 @@ public class BungeeRotator
}
new ApiDeleteCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records?" + idBuilder.toString()).Execute();
System.out.println("Deleted " + recordsToDelete.size() + " records.");
log("Deleted " + recordsToDelete.size() + " records.");
}
int processWaits = 0;
while (_processes.size() > 0)
{
for (Iterator<ProcessRunner> 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<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
{
iterator.next().abort();
iterator.remove();
}
}
processWaits++;
}
processWaits = 0;
try
{
Thread.sleep(30000);
Thread.sleep(15000);
log("Natural Sleep");
}
catch (InterruptedException e)
{
@ -183,7 +162,7 @@ public class BungeeRotator
}
catch (Exception ex)
{
System.out.println("Error doing something : " + ex.getMessage());
log("Error doing something : " + ex.getMessage());
try
{
@ -196,4 +175,17 @@ public class BungeeRotator
}
}
}
private static void log(String message)
{
log(message, false);
}
private static void log(String message, boolean fileOnly)
{
_logger.info("[" + _dateFormat.format(new Date()) + "] " + message);
if (!fileOnly)
System.out.println("[" + _dateFormat.format(new Date()) + "] " + message);
}
}