Fix console stopping, fix #221

- Make StopCommand async
- Close terminal reader only
This commit is contained in:
Karlatemp 2020-11-12 12:38:49 +08:00
parent 6fe21b9286
commit 7dd4a9669b
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
3 changed files with 29 additions and 31 deletions

View File

@ -102,29 +102,31 @@ public object BuiltInCommands {
@Handler
public suspend fun CommandSender.handle() {
kotlin.runCatching {
closingLock.withLock {
sendMessage("Stopping mirai-console")
kotlin.runCatching {
runIgnoreException<CancellationException> { MiraiConsole.job.cancelAndJoin() }
}.fold(
onSuccess = {
runIgnoreException<EventCancelledException> { sendMessage("mirai-console stopped successfully.") }
},
onFailure = {
if (it is CancellationException) return@fold
@OptIn(ConsoleInternalApi::class)
MiraiConsole.mainLogger.error("Exception in stop", it)
runIgnoreException<EventCancelledException> {
sendMessage(
it.localizedMessage ?: it.message ?: it.toString()
)
GlobalScope.launch {
kotlin.runCatching {
closingLock.withLock {
if (!MiraiConsole.isActive) return@withLock
sendMessage("Stopping mirai-console")
kotlin.runCatching {
MiraiConsole.job.cancelAndJoin()
}.fold(
onSuccess = {
runIgnoreException<EventCancelledException> { sendMessage("mirai-console stopped successfully.") }
},
onFailure = {
@OptIn(ConsoleInternalApi::class)
MiraiConsole.mainLogger.error("Exception in stop", it)
runIgnoreException<EventCancelledException> {
sendMessage(
it.localizedMessage ?: it.message ?: it.toString()
)
}
}
}
)
}
}.exceptionOrNull()?.let(MiraiConsole.mainLogger::error)
exitProcess(0)
)
}
}.exceptionOrNull()?.let(MiraiConsole.mainLogger::error)
exitProcess(0)
}
}
}

View File

@ -94,9 +94,7 @@ internal constructor(
private val impl = SemVersionInternal.parseRangeRequirement(rule)
/** 在 [version] 满足此要求时返回 true */
public fun test(version: SemVersion): Boolean {
return impl.test(version)
}
public fun test(version: SemVersion): Boolean = impl.test(version)
public object RequirementAsStringSerializer : KSerializer<Requirement> by String.serializer().map(
serializer = { it.rule },

View File

@ -9,10 +9,7 @@
package net.mamoe.mirai.console.terminal
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.command.*
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
@ -30,12 +27,13 @@ internal fun startupConsoleThread() {
if (terminal is NoConsole) return
MiraiConsole.launch(CoroutineName("Input Cancelling Daemon")) {
while (true) {
while (isActive) {
delay(2000)
}
}.invokeOnCompletion {
runCatching<Unit> {
terminal.close()
// 应该仅关闭用户输入
terminal.reader().shutdown()
ConsoleInputImpl.thread.shutdownNow()
runCatching {
ConsoleInputImpl.executingCoroutine?.cancel(EndOfFileException())