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

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())