Support staring console-pure as a daemon

This commit is contained in:
Him188 2020-08-27 22:10:05 +08:00
parent 1414ff35e7
commit dad62058db
3 changed files with 28 additions and 7 deletions

View File

@ -54,7 +54,7 @@ internal abstract class JvmPluginInternal(
final override val logger: MiraiLogger by lazy { final override val logger: MiraiLogger by lazy {
MiraiConsole.newLogger( MiraiConsole.newLogger(
this._description.name "Plugin ${this._description.name}"
) )
} }
@ -124,9 +124,16 @@ internal abstract class JvmPluginInternal(
@JvmField @JvmField
internal val coroutineContextInitializer = { internal val coroutineContextInitializer = {
CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } CoroutineExceptionHandler { _, throwable ->
if (throwable !is CancellationException) logger.error(throwable)
}
.plus(parentCoroutineContext) .plus(parentCoroutineContext)
.plus(NamedSupervisorJob("Plugin $name", parentCoroutineContext[Job])) .plus(
NamedSupervisorJob(
"Plugin $name",
parentCoroutineContext[Job] ?: JarPluginLoaderImpl.coroutineContext[Job]!!
)
)
.also { .also {
JarPluginLoaderImpl.coroutineContext[Job]!!.invokeOnCompletion { JarPluginLoaderImpl.coroutineContext[Job]!!.invokeOnCompletion {
this.cancel() this.cancel()
@ -139,7 +146,7 @@ internal abstract class JvmPluginInternal(
return coroutineContextInitializer().also { _coroutineContext = it }.also { return coroutineContextInitializer().also { _coroutineContext = it }.also {
job.invokeOnCompletion { e -> job.invokeOnCompletion { e ->
if (e != null) { if (e != null) {
logger.error(e) if (e !is CancellationException) logger.error(e)
if (this.isEnabled) safeLoader.disable(this) if (this.isEnabled) safeLoader.disable(this)
} }
} }

View File

@ -38,6 +38,7 @@ import net.mamoe.mirai.console.plugin.DeferredPluginLoader
import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.PluginLoader
import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader
import net.mamoe.mirai.console.pure.ConsoleInputImpl.requestInput 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.ConsoleInput
import net.mamoe.mirai.console.util.ConsoleInternalAPI import net.mamoe.mirai.console.util.ConsoleInternalAPI
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
@ -60,7 +61,8 @@ import java.util.*
* *
* @see MiraiConsolePureLoader CLI 入口点 * @see MiraiConsolePureLoader CLI 入口点
*/ */
internal class MiraiConsoleImplementationPure @ConsoleExperimentalAPI
class MiraiConsoleImplementationPure
@JvmOverloads constructor( @JvmOverloads constructor(
override val rootPath: Path = Paths.get("."), override val rootPath: Path = Paths.get("."),
override val builtInPluginLoaders: List<PluginLoader<*, *>> = Collections.unmodifiableList( override val builtInPluginLoaders: List<PluginLoader<*, *>> = Collections.unmodifiableList(

View File

@ -20,8 +20,12 @@
package net.mamoe.mirai.console.pure 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.MiraiConsoleImplementation.Companion.start
import net.mamoe.mirai.console.command.ConsoleCommandSender 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.console.util.ConsoleInternalAPI
import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.DefaultLogger
@ -33,11 +37,19 @@ import java.io.PrintStream
object MiraiConsolePureLoader { object MiraiConsolePureLoader {
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
startup() startAsDaemon()
try {
runBlocking {
MiraiConsole.job.join()
}
} catch (e: CancellationException) {
// ignored
}
} }
@Suppress("MemberVisibilityCanBePrivate") @Suppress("MemberVisibilityCanBePrivate")
internal fun startup(instance: MiraiConsoleImplementationPure = MiraiConsoleImplementationPure()) { @ConsoleExperimentalAPI
fun startAsDaemon(instance: MiraiConsoleImplementationPure = MiraiConsoleImplementationPure()) {
instance.start() instance.start()
overrideSTD() overrideSTD()
startupConsoleThread() startupConsoleThread()