From 1e6ca6b3796e0f115c863ce751f3940597d87abf Mon Sep 17 00:00:00 2001 From: ceikry Date: Sat, 1 Jan 2022 02:12:23 -0600 Subject: [PATCH] Reintroduce watchdog --- Server/src/main/kotlin/rs09/Server.kt | 36 +++++++++++++++++++ .../src/main/kotlin/rs09/worker/WorldClock.kt | 1 + 2 files changed, 37 insertions(+) diff --git a/Server/src/main/kotlin/rs09/Server.kt b/Server/src/main/kotlin/rs09/Server.kt index 453ea5a1f..f6ace5dd6 100644 --- a/Server/src/main/kotlin/rs09/Server.kt +++ b/Server/src/main/kotlin/rs09/Server.kt @@ -10,6 +10,9 @@ import core.net.IoEventHandler import core.net.IoSession import core.net.ServerSocketConnection import core.plugin.CorePluginTypes.StartupPlugin +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import rs09.game.node.entity.state.newsys.StateRepository import rs09.game.system.SystemLogger import rs09.game.system.config.ConfigParser @@ -22,7 +25,10 @@ import rs09.game.world.repository.Repository.players import rs09.net.ms.ManagementServer import rs09.plugin.PluginManager import java.io.File +import java.io.FileWriter import java.io.IOException +import java.lang.management.ManagementFactory +import java.lang.management.ThreadMXBean import java.net.InetSocketAddress import java.nio.channels.SelectionKey import java.nio.channels.Selector @@ -42,6 +48,7 @@ object Server { private var service: ExecutorService? = null private var channel: ServerSocketConnection? = null private var eventHandler: IoEventHandler? = null + var lastHeartbeat = System.currentTimeMillis() /** * The current server state. @@ -69,6 +76,25 @@ object Server { ServerConfigParser.parse("worldprops" + File.separator + "default.conf") } + GlobalScope.launch { + delay(20000) + lastHeartbeat = System.currentTimeMillis() + while(true){ + if(System.currentTimeMillis() - lastHeartbeat > 7200 && state == ServerState.RUNNING){ + SystemLogger.logErr("Triggering reboot due to heartbeat timeout") + SystemLogger.logErr("Creating thread dump...") + val dump = threadDump(true, true) + FileWriter("latestdump.txt").use { + it.write(dump) + it.flush() + it.close() + } + exitProcess(0) + } + delay(625) + } + } + while (state != ServerState.CLOSED) { when (state) { @@ -172,4 +198,14 @@ object Server { Runtime.getRuntime().removeShutdownHook(ServerConstants.SHUTDOWN_HOOK) exitProcess(0) } + + + private fun threadDump(lockedMonitors: Boolean, lockedSynchronizers: Boolean): String? { + val threadDump = StringBuffer(System.lineSeparator()) + val threadMXBean: ThreadMXBean = ManagementFactory.getThreadMXBean() + for (threadInfo in threadMXBean.dumpAllThreads(lockedMonitors, lockedSynchronizers)) { + threadDump.append(threadInfo.toString()) + } + return threadDump.toString() + } } diff --git a/Server/src/main/kotlin/rs09/worker/WorldClock.kt b/Server/src/main/kotlin/rs09/worker/WorldClock.kt index e7dabfffd..fc11a7ba7 100644 --- a/Server/src/main/kotlin/rs09/worker/WorldClock.kt +++ b/Server/src/main/kotlin/rs09/worker/WorldClock.kt @@ -67,6 +67,7 @@ class WorldClock { val end = System.currentTimeMillis() //ServerMonitor.eventQueue.add(GuiEvent.UpdateTickTime(end - start)) //ServerMonitor.eventQueue.add(GuiEvent.UpdatePulseCount(World.Pulser.TASKS.size)) + Server.lastHeartbeat = System.currentTimeMillis() Thread.sleep(max(600 - (end - start), 0)) } }