@@ -4,6 +4,8 @@ import cf.wayzer.placehold.PlaceHoldApi.with
44import cf.wayzer.scriptAgent.state.ConditionState
55
66suspend inline fun runIgnoreCancel (sync : Boolean , crossinline body : suspend () -> Unit ) {
7+ // Need new Job, as it may restart this script.
8+ @Suppress(" CoroutineContextWithJob" )
79 val job = launch(Job ()) { body() }
810 if (sync) job.join()
911}
@@ -36,12 +38,12 @@ command("listFailed", "列出所有故障脚本".with(), commands = Commands.con
3638 body {
3739 val prefix = arg.firstOrNull().orEmpty()
3840 val scripts = ScriptRegistry .allScripts {
39- it.id.startsWith(prefix) && ! it.transaction. ready()
41+ it.id.startsWith(prefix) && ! it.ready()
4042 }
4143 for (info in scripts) {
4244 reply(buildString {
4345 appendLine(" [${info.scriptState} ] ${info.id} " )
44- info.conditions .filter { it.status != ConditionState .Status .Success }.forEach { c ->
46+ info.lastConditions .filter { it.status != ConditionState .Status .Success }.forEach { c ->
4547 c.display().forEach {
4648 append(" " )
4749 appendLine(it)
@@ -74,8 +76,8 @@ command("list", "列出所有模块或模块内所有脚本".with(), commands =
7476 val list = ScriptRegistry .allScripts {
7577 it.id.startsWith(module + Config .idSeparator)
7678 }.map { script ->
77- val color = if (script.transaction. ready()) " green" else " red"
78- val conditions = script.conditions .filter { it.status != ConditionState .Status .Success }
79+ val color = if (script.ready()) " green" else " red"
80+ val conditions = script.lastConditions .filter { it.status != ConditionState .Status .Success }
7981 .joinToString(" " ) { " ${it.type}${it.status} " }
8082 " [$color ]${script.id.padEnd(30 )} [reset] [${script.scriptState} ] $conditions "
8183 }
@@ -94,23 +96,21 @@ command("load", "(重新)加载一个脚本或者模块".with(), commands = Comm
9496 onComplete(0 ) { ScriptRegistry .allScripts { true }.map { it.id } }
9597 }
9698 body {
97- val noCache = checkArg(" --noCache" )
9899 var noEnable = checkArg(" --noEnable" )
99100 val async = checkArg(" --async" )
100101
101102 if (arg.isEmpty()) replyUsage()
102103 val script = ScriptRegistry .getScriptInfo(arg[0 ])
103104 ? : returnReply(" [red]找不到模块或者脚本" .with ())
104- if (noCache) {
105- val file = Config .cacheFile(script.id)
106- reply(" [yellow]清理cache文件{name}" .with (" name" to file.name))
107- file.delete()
108- }
109105 runIgnoreCancel(! async) {
110106 ScriptManager .transactionV2 {
111- unload(script)
112- execute()
113- (if (noEnable) load else enable)(script)
107+ if (script.scriptState.loaded) {
108+ reload(script)
109+ } else if (! noEnable) {
110+ enable(script)
111+ } else {
112+ load(script)
113+ }
114114 }.printResult()
115115 }
116116 }
@@ -129,12 +129,9 @@ command("compile", "(实验性)编译单个脚本(不影响运行中实例)".wit
129129 val script = ScriptRegistry .getScriptInfo(arg[0 ])
130130 ? : returnReply(" [red]找不到模块或者脚本" .with ())
131131 runIgnoreCancel(! async) {
132- ConditionState (" Compile" , script.id).apply {
133- run {
134- script.transaction.compileScript()
135- }
136- reply(display().joinToString(" \n " ).asPlaceHoldString())
137- }
132+ ScriptManager .transactionV2 {
133+ compile(script)
134+ }.printResult()
138135 }
139136 }
140137}
@@ -146,15 +143,8 @@ command("retry", "(实验性)重试事务".with(), commands = Commands.controlCo
146143 val async = checkArg(" --async" )
147144 runIgnoreCancel(! async) {
148145 ScriptManager .transactionV2 {
149- ScriptRegistry .allScripts { ! it.transaction.ready() }.forEach {
150- put(it, it.userDemand)
151- }
152- // Clear conditions, and retry
153- onEach {
154- it.key.transaction.queueRun {
155- setUserDemand(UserDemandState .CompileOnly );
156- setUserDemand(it.value)
157- }
146+ ScriptRegistry .allScripts { ! it.ready() }.forEach {
147+ compile(it)
158148 }
159149 }.printResult()
160150 }
0 commit comments