refactor(config): run init after watching started

This commit is contained in:
2026-04-02 22:19:37 +08:00
parent f37bef57ba
commit 03532d3d97

View File

@@ -2,6 +2,7 @@ package work.slhaf.partner.api.agent.runtime.config
import com.alibaba.fastjson2.JSON import com.alibaba.fastjson2.JSON
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import work.slhaf.partner.api.agent.runtime.exception.AgentLaunchFailedException
import work.slhaf.partner.api.common.support.DirectoryWatchSupport import work.slhaf.partner.api.common.support.DirectoryWatchSupport
import java.io.IOException import java.io.IOException
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
@@ -52,7 +53,7 @@ object ConfigCenter : AutoCloseable {
executor, executor,
-1 -1
) { ) {
reconcileAll() initAll()
}.onCreate(this::handleUpsert) }.onCreate(this::handleUpsert)
.onModify(this::handleUpsert) .onModify(this::handleUpsert)
.onDelete(this::handleDelete) .onDelete(this::handleDelete)
@@ -64,6 +65,18 @@ object ConfigCenter : AutoCloseable {
log.info("ConfigCenter 文件监听注册完毕: {}", paths.configDir) log.info("ConfigCenter 文件监听注册完毕: {}", paths.configDir)
} }
fun initAll() {
registrations.forEach { (path, registration) ->
try {
val config = loadConfig(path, registration)
registration.init(config)
} catch (e: Exception) {
throw AgentLaunchFailedException("Failed to init config", e)
}
}
}
private fun handleUpsert(thisDir: Path, context: Path?) { private fun handleUpsert(thisDir: Path, context: Path?) {
if (context == null || !Files.isRegularFile(context) || !isJsonFile(context)) { if (context == null || !Files.isRegularFile(context) || !isJsonFile(context)) {
return return