From 43b8be3ff3094ffd2c0639a47a195b135bdf92ce Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 16 May 2021 19:58:53 +0100 Subject: [PATCH] Add shutdown hook Signed-off-by: Graham --- .../src/main/kotlin/org/openrs2/game/GameServer.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/game/src/main/kotlin/org/openrs2/game/GameServer.kt b/game/src/main/kotlin/org/openrs2/game/GameServer.kt index 93359c5c..6e481d54 100644 --- a/game/src/main/kotlin/org/openrs2/game/GameServer.kt +++ b/game/src/main/kotlin/org/openrs2/game/GameServer.kt @@ -13,16 +13,28 @@ public class GameServer @Inject constructor( private val serviceManager = ServiceManager(services) public fun run(start: Long) { - serviceManager.startAsync().awaitHealthy() + val shutdownHook = Thread(::stop) + + serviceManager.startAsync() + runtime.addShutdownHook(shutdownHook) + serviceManager.awaitHealthy() val elapsed = System.nanoTime() - start logger.info { "Started OpenRS2 in ${elapsed / NANOS_PER_MILLI} milliseconds" } serviceManager.awaitStopped() + runtime.removeShutdownHook(shutdownHook) + } + + private fun stop() { + logger.info { "Stopping OpenRS2..." } + serviceManager.stopAsync().awaitStopped() + logger.info { "Stopped OpenRS2" } } private companion object { private val logger = InlineLogger() + private val runtime = Runtime.getRuntime() private const val NANOS_PER_MILLI = 1_000_000 } }