mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Fix console stopping, fix #221
- Make StopCommand async - Close terminal reader only
This commit is contained in:
parent
6fe21b9286
commit
7dd4a9669b
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 },
|
||||
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user