From aaaf2f8013faaf9d283dca18d46ff601fcaef9c1 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Fri, 6 Sep 2024 10:55:42 +0800 Subject: [PATCH] . --- main/server/src/mill/main/server/Server.scala | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/main/server/src/mill/main/server/Server.scala b/main/server/src/mill/main/server/Server.scala index 588b1533dac..acb7ee9042a 100644 --- a/main/server/src/mill/main/server/Server.scala +++ b/main/server/src/mill/main/server/Server.scala @@ -31,7 +31,7 @@ abstract class Server[T]( val serverId: String = java.lang.Long.toHexString(scala.util.Random.nextLong()) def serverLog0(s: String): Unit = { - if (running) { + if (running && checkServerIdFile()) { os.write.append(serverDir / ServerFiles.serverLog, s"$s\n", createFolders = true) } } @@ -92,29 +92,27 @@ abstract class Server[T]( def watchServerIdFile(): Unit = { os.write.over(serverDir / ServerFiles.serverId, serverId) val serverIdThread = new Thread( - () => - while ( - running && { - Thread.sleep(100) - Try(os.read(serverDir / ServerFiles.serverId)).toOption match { - case None => - serverLog("serverId file missing") - exitServer() - false - case Some(s) => - if (s == serverId) true - else { - serverLog(s"serverId file contents $s does not match serverId $serverId") - exitServer() - false - } - } - } - ) (), + () => while (running && checkServerIdFile()) Thread.sleep(100), "Server ID Checker Thread" ) serverIdThread.start() } + def checkServerIdFile() = { + Try(os.read(serverDir / ServerFiles.serverId)) match { + case scala.util.Failure(e) => + serverLog(s"serverId file missing: $e") + exitServer() + false + case scala.util.Success(s) => + if (s == serverId) true + else { + serverLog(s"serverId file contents $s does not match serverId $serverId") + exitServer() + false + } + } + + } def interruptWithTimeout[T](close: () => Unit, t: () => T): Option[T] = { @volatile var interrupt = true