Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Fix blocking call in shutdown hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Aug 1, 2020
1 parent 5efa482 commit 30e359c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}")
Expand All @@ -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) {

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
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
}
}
}
"test"
} else "")
"test"
} else "")

throw throwable
}
)
throw throwable
}
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -209,4 +224,3 @@ internal class TestCommand {
}
}
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -129,4 +134,3 @@ internal class SettingTest {
assertSame(reference(), delegation()) // check shadowing
}
}
*/

0 comments on commit 30e359c

Please sign in to comment.