diff --git a/src/main/java/org/ultramine/server/WatchdogThread.java b/src/main/java/org/ultramine/server/WatchdogThread.java index 5ad2a6a..e6d8176 100644 --- a/src/main/java/org/ultramine/server/WatchdogThread.java +++ b/src/main/java/org/ultramine/server/WatchdogThread.java @@ -15,34 +15,26 @@ @SideOnly(Side.SERVER) public class WatchdogThread extends Thread { - Logger log = LogManager.getLogger(); + private static final Logger log = LogManager.getLogger(); private static WatchdogThread instance; - private final long timeoutTime; - private final boolean restart; private volatile long lastTick; private volatile boolean stopping; - private WatchdogThread(long timeoutTime, boolean restart) + private WatchdogThread() { - super("Spigot Watchdog Thread"); - this.timeoutTime = timeoutTime; - this.restart = restart; + super("Watchdog Thread"); } - public static void doStart(int timeoutTime, boolean restart) + public static void doStart() { if(instance == null) { - instance = new WatchdogThread(timeoutTime * 1000L, restart); + instance = new WatchdogThread(); instance.start(); } } - - public static void doStart() - { - doStart(ConfigurationHandler.getServerConfig().settings.watchdogThread.timeout, ConfigurationHandler.getServerConfig().settings.watchdogThread.restart); - } + public static void tick() { @@ -63,48 +55,34 @@ while(!stopping) { // - if(lastTick != 0 && System.currentTimeMillis() > lastTick + timeoutTime) + if(lastTick != 0 && System.currentTimeMillis() > lastTick + ConfigurationHandler.getServerConfig().settings.watchdogThread.timeout*1000) { log.log(Level.FATAL, "The server has stopped responding!"); log.log(Level.FATAL, "Current Thread State:"); ThreadInfo[] threads = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true); + for(ThreadInfo thread : threads) //main thread first + { + if(thread.getThreadName().equals("Server thread")) + displayThreadInfo(thread); + } + for(ThreadInfo thread : threads) { - if(thread.getThreadState() != State.WAITING) - { - log.log(Level.FATAL, "------------------------------"); - // - log.log(Level.FATAL, "Current Thread: " + thread.getThreadName()); - log.log(Level.FATAL, "\tPID: " + thread.getThreadId() + " | Suspended: " + thread.isSuspended() + " | Native: " + thread.isInNative() + " | State: " - + thread.getThreadState()); - - if(thread.getLockedMonitors().length != 0) - { - log.log(Level.FATAL, "\tThread is waiting on monitor(s):"); - - for(MonitorInfo monitor : thread.getLockedMonitors()) - { - log.log(Level.FATAL, "\t\tLocked on:" + monitor.getLockedStackFrame()); - } - } - - log.log(Level.FATAL, "\tStack:"); - // - StackTraceElement[] stack = thread.getStackTrace(); - - for(int line = 0; line < stack.length; line++) - { - log.log(Level.FATAL, "\t\t" + stack[line].toString()); - } - } + if(!thread.getThreadName().equals("Server thread")) + displayThreadInfo(thread); } log.log(Level.FATAL, "------------------------------"); - if(restart) + if(ConfigurationHandler.getServerConfig().settings.watchdogThread.restart) { + try + { + sleep(2000); //await log output + } catch (InterruptedException ex) {} + Thread.currentThread().interrupt(); FMLCommonHandler.instance().handleExit(0); } @@ -121,4 +99,35 @@ } } } + + private static void displayThreadInfo(ThreadInfo thread) + { + if(thread.getThreadState() != State.WAITING) + { + log.log(Level.FATAL, "------------------------------"); + // + log.log(Level.FATAL, "Current Thread: " + thread.getThreadName()); + log.log(Level.FATAL, "\tPID: " + thread.getThreadId() + " | Suspended: " + thread.isSuspended() + " | Native: " + thread.isInNative() + " | State: " + + thread.getThreadState()); + + if(thread.getLockedMonitors().length != 0) + { + log.log(Level.FATAL, "\tThread is waiting on monitor(s):"); + + for(MonitorInfo monitor : thread.getLockedMonitors()) + { + log.log(Level.FATAL, "\t\tLocked on:" + monitor.getLockedStackFrame()); + } + } + + log.log(Level.FATAL, "\tStack:"); + // + StackTraceElement[] stack = thread.getStackTrace(); + + for(int line = 0; line < stack.length; line++) + { + log.log(Level.FATAL, "\t\t" + stack[line].toString()); + } + } + } }