From dad62058db8ebc91c177f25b721bda6353e95acd Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 27 Aug 2020 22:10:05 +0800 Subject: [PATCH] Support staring console-pure as a daemon --- .../console/internal/plugin/JvmPluginInternal.kt | 15 +++++++++++---- .../pure/MiraiConsoleImplementationPure.kt | 4 +++- .../mirai/console/pure/MiraiConsolePureLoader.kt | 16 ++++++++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt index dd0f43fc3..7841c04b0 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt @@ -54,7 +54,7 @@ internal abstract class JvmPluginInternal( final override val logger: MiraiLogger by lazy { MiraiConsole.newLogger( - this._description.name + "Plugin ${this._description.name}" ) } @@ -124,9 +124,16 @@ internal abstract class JvmPluginInternal( @JvmField internal val coroutineContextInitializer = { - CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } + CoroutineExceptionHandler { _, throwable -> + if (throwable !is CancellationException) logger.error(throwable) + } .plus(parentCoroutineContext) - .plus(NamedSupervisorJob("Plugin $name", parentCoroutineContext[Job])) + .plus( + NamedSupervisorJob( + "Plugin $name", + parentCoroutineContext[Job] ?: JarPluginLoaderImpl.coroutineContext[Job]!! + ) + ) .also { JarPluginLoaderImpl.coroutineContext[Job]!!.invokeOnCompletion { this.cancel() @@ -139,7 +146,7 @@ internal abstract class JvmPluginInternal( return coroutineContextInitializer().also { _coroutineContext = it }.also { job.invokeOnCompletion { e -> if (e != null) { - logger.error(e) + if (e !is CancellationException) logger.error(e) if (this.isEnabled) safeLoader.disable(this) } } diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt index 1dad2ed5d..f19e1de90 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt @@ -38,6 +38,7 @@ import net.mamoe.mirai.console.plugin.DeferredPluginLoader import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import net.mamoe.mirai.console.pure.ConsoleInputImpl.requestInput +import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleInput import net.mamoe.mirai.console.util.ConsoleInternalAPI import net.mamoe.mirai.utils.* @@ -60,7 +61,8 @@ import java.util.* * * @see MiraiConsolePureLoader CLI 入口点 */ -internal class MiraiConsoleImplementationPure +@ConsoleExperimentalAPI +class MiraiConsoleImplementationPure @JvmOverloads constructor( override val rootPath: Path = Paths.get("."), override val builtInPluginLoaders: List> = Collections.unmodifiableList( diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt index d037ac284..afb5b9ef2 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt @@ -20,8 +20,12 @@ package net.mamoe.mirai.console.pure +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.runBlocking +import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start import net.mamoe.mirai.console.command.ConsoleCommandSender +import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleInternalAPI import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.utils.DefaultLogger @@ -33,11 +37,19 @@ import java.io.PrintStream object MiraiConsolePureLoader { @JvmStatic fun main(args: Array) { - startup() + startAsDaemon() + try { + runBlocking { + MiraiConsole.job.join() + } + } catch (e: CancellationException) { + // ignored + } } @Suppress("MemberVisibilityCanBePrivate") - internal fun startup(instance: MiraiConsoleImplementationPure = MiraiConsoleImplementationPure()) { + @ConsoleExperimentalAPI + fun startAsDaemon(instance: MiraiConsoleImplementationPure = MiraiConsoleImplementationPure()) { instance.start() overrideSTD() startupConsoleThread()