mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Fix blocking call in shutdown hook
This commit is contained in:
parent
5efa4824bf
commit
30e359c282
@ -9,9 +9,8 @@
|
||||
|
||||
package net.mamoe.mirai.console.command
|
||||
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.cancelAndJoin
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import net.mamoe.mirai.Bot
|
||||
@ -86,12 +85,6 @@ public object BuiltInCommands {
|
||||
ConsoleCommandOwner, "help",
|
||||
description = "Gets help about the console."
|
||||
), BuiltInCommand {
|
||||
init {
|
||||
Runtime.getRuntime().addShutdownHook(thread(false) {
|
||||
runBlocking { Stop.execute(ConsoleCommandSender.instance) }
|
||||
})
|
||||
}
|
||||
|
||||
@Handler
|
||||
public suspend fun CommandSender.handle() {
|
||||
sendMessage("现在有指令: ${allRegisteredCommands.joinToString { it.primaryName }}")
|
||||
@ -105,57 +98,56 @@ public object BuiltInCommands {
|
||||
), BuiltInCommand {
|
||||
init {
|
||||
Runtime.getRuntime().addShutdownHook(thread(false) {
|
||||
if (!MiraiConsole.isActive) {
|
||||
return@thread
|
||||
}
|
||||
runBlocking { Stop.execute(ConsoleCommandSender.instance) }
|
||||
MiraiConsole.cancel()
|
||||
})
|
||||
}
|
||||
|
||||
private val closingLock = Mutex()
|
||||
|
||||
@Handler
|
||||
public suspend fun CommandSender.handle(): Unit = closingLock.withLock {
|
||||
sendMessage("Stopping mirai-console")
|
||||
kotlin.runCatching {
|
||||
MiraiConsole.job.cancelAndJoin()
|
||||
}.fold(
|
||||
onSuccess = { sendMessage("mirai-console stopped successfully.") },
|
||||
onFailure = {
|
||||
MiraiConsole.mainLogger.error(it)
|
||||
sendMessage(it.localizedMessage ?: it.message ?: it.toString())
|
||||
}
|
||||
)
|
||||
public suspend fun CommandSender.handle(): Unit {
|
||||
closingLock.withLock {
|
||||
sendMessage("Stopping mirai-console")
|
||||
kotlin.runCatching {
|
||||
MiraiConsole.job.cancelAndJoin()
|
||||
}.fold(
|
||||
onSuccess = { sendMessage("mirai-console stopped successfully.") },
|
||||
onFailure = {
|
||||
MiraiConsole.mainLogger.error(it)
|
||||
sendMessage(it.localizedMessage ?: it.message ?: it.toString())
|
||||
}
|
||||
)
|
||||
}
|
||||
exitProcess(0)
|
||||
}
|
||||
}
|
||||
|
||||
public object Login : SimpleCommand(
|
||||
ConsoleCommandOwner, "login",
|
||||
description = "Log in a bot account."
|
||||
), BuiltInCommand {
|
||||
@Handler
|
||||
public suspend fun CommandSender.handle(id: Long, password: String) {
|
||||
public object Login : SimpleCommand(
|
||||
ConsoleCommandOwner, "login",
|
||||
description = "Log in a bot account."
|
||||
), BuiltInCommand {
|
||||
@Handler
|
||||
public suspend fun CommandSender.handle(id: Long, password: String) {
|
||||
|
||||
kotlin.runCatching {
|
||||
MiraiConsole.addBot(id, password).alsoLogin()
|
||||
}.fold(
|
||||
onSuccess = { sendMessage("${it.nick} ($id) Login succeed") },
|
||||
onFailure = { throwable ->
|
||||
sendMessage(
|
||||
"Login failed: ${throwable.localizedMessage ?: throwable.message ?: throwable.toString()}" +
|
||||
if (this is MessageEventContextAware<*>) {
|
||||
this.fromEvent.selectMessagesUnit {
|
||||
"stacktrace" reply {
|
||||
throwable.stacktraceString
|
||||
kotlin.runCatching {
|
||||
MiraiConsole.addBot(id, password).alsoLogin()
|
||||
}.fold(
|
||||
onSuccess = { sendMessage("${it.nick} ($id) Login succeed") },
|
||||
onFailure = { throwable ->
|
||||
sendMessage(
|
||||
"Login failed: ${throwable.localizedMessage ?: throwable.message ?: throwable.toString()}" +
|
||||
if (this is MessageEventContextAware<*>) {
|
||||
this.fromEvent.selectMessagesUnit {
|
||||
"stacktrace" reply {
|
||||
throwable.stacktraceString
|
||||
}
|
||||
}
|
||||
}
|
||||
"test"
|
||||
} else "")
|
||||
"test"
|
||||
} else "")
|
||||
|
||||
throw throwable
|
||||
}
|
||||
)
|
||||
throw throwable
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,23 @@
|
||||
|
||||
package net.mamoe.mirai.console.command
|
||||
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.mamoe.mirai.console.MiraiConsole
|
||||
import net.mamoe.mirai.console.Testing
|
||||
import net.mamoe.mirai.console.Testing.withTesting
|
||||
import net.mamoe.mirai.console.command.description.CommandArgParser
|
||||
import net.mamoe.mirai.console.command.description.CommandParserContext
|
||||
import net.mamoe.mirai.console.command.internal.InternalCommandManager
|
||||
import net.mamoe.mirai.console.command.internal.flattenCommandComponents
|
||||
import net.mamoe.mirai.console.initTestEnvironment
|
||||
import net.mamoe.mirai.message.data.Image
|
||||
import net.mamoe.mirai.message.data.SingleMessage
|
||||
import net.mamoe.mirai.message.data.toMessage
|
||||
import org.junit.jupiter.api.AfterAll
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.*
|
||||
|
||||
object TestCompositeCommand : CompositeCommand(
|
||||
ConsoleCommandOwner,
|
||||
@ -33,7 +49,7 @@ object TestSimpleCommand : RawCommand(owner, "testSimple", "tsS") {
|
||||
internal val sender by lazy { ConsoleCommandSender.instance }
|
||||
internal val owner by lazy { ConsoleCommandOwner }
|
||||
|
||||
/*
|
||||
|
||||
internal class TestCommand {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
@ -45,8 +61,7 @@ internal class TestCommand {
|
||||
@AfterAll
|
||||
@JvmStatic
|
||||
fun destroy() {
|
||||
// Runtime.getRuntime().halt(0) // TODO: 2020/8/1 fix exitProcess
|
||||
exitProcess(0)
|
||||
MiraiConsole.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
@ -209,4 +224,3 @@ internal class TestCommand {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
@ -9,7 +9,12 @@
|
||||
|
||||
package net.mamoe.mirai.console.setting
|
||||
|
||||
/*
|
||||
import kotlinx.serialization.json.Json
|
||||
import net.mamoe.mirai.console.utils.ConsoleInternalAPI
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertSame
|
||||
|
||||
@OptIn(ConsoleInternalAPI::class)
|
||||
internal class SettingTest {
|
||||
|
||||
@ -129,4 +134,3 @@ internal class SettingTest {
|
||||
assertSame(reference(), delegation()) // check shadowing
|
||||
}
|
||||
}
|
||||
*/
|
Loading…
Reference in New Issue
Block a user