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,17 +102,18 @@ public object BuiltInCommands {
@Handler @Handler
public suspend fun CommandSender.handle() { public suspend fun CommandSender.handle() {
GlobalScope.launch {
kotlin.runCatching { kotlin.runCatching {
closingLock.withLock { closingLock.withLock {
if (!MiraiConsole.isActive) return@withLock
sendMessage("Stopping mirai-console") sendMessage("Stopping mirai-console")
kotlin.runCatching { kotlin.runCatching {
runIgnoreException<CancellationException> { MiraiConsole.job.cancelAndJoin() } MiraiConsole.job.cancelAndJoin()
}.fold( }.fold(
onSuccess = { onSuccess = {
runIgnoreException<EventCancelledException> { sendMessage("mirai-console stopped successfully.") } runIgnoreException<EventCancelledException> { sendMessage("mirai-console stopped successfully.") }
}, },
onFailure = { onFailure = {
if (it is CancellationException) return@fold
@OptIn(ConsoleInternalApi::class) @OptIn(ConsoleInternalApi::class)
MiraiConsole.mainLogger.error("Exception in stop", it) MiraiConsole.mainLogger.error("Exception in stop", it)
runIgnoreException<EventCancelledException> { runIgnoreException<EventCancelledException> {
@ -127,6 +128,7 @@ public object BuiltInCommands {
exitProcess(0) exitProcess(0)
} }
} }
}
public object LoginCommand : SimpleCommand( public object LoginCommand : SimpleCommand(
ConsoleCommandOwner, "login", "登录", ConsoleCommandOwner, "login", "登录",

View File

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

View File

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