refactor(bootstrap): move runtime init from router to cmd layer

Extract SyncEvents and InitLogWriter from router.Serve into bootstrap.Init
called from cmd entry points with trace-aware context. Preserve existing
Register* wiring for task and push domain integrations.
This commit is contained in:
ryan
2026-06-18 10:38:08 +08:00
parent 50c45db561
commit 03fa5d948d
9 changed files with 152 additions and 15 deletions
+3 -4
View File
@@ -21,13 +21,13 @@ const (
)
// InitLogWriter 初始化日志写入通道和后台写入协程
func InitLogWriter() {
func InitLogWriter(ctx context.Context) {
if !config.Config.ClickHouse.Enabled {
return
}
logChan = make(chan *UserAccessLog, defaultQueueSize)
go startBatchWorker()
go startBatchWorker(context.WithoutCancel(ctx))
}
// IsBufferFull 检查当前本地缓冲队列是否已满
@@ -52,7 +52,7 @@ func QueueAccessLog(logItem *UserAccessLog) {
}
}
func startBatchWorker() {
func startBatchWorker(ctx context.Context) {
ticker := time.NewTicker(flushInterval)
defer ticker.Stop()
@@ -67,7 +67,6 @@ func startBatchWorker() {
return
}
ctx := context.Background()
b, err := db.ChConn.PrepareBatch(ctx, "INSERT INTO w_user_access_logs (id, user_id, path, method, ip, user_agent, headers, status, latency, created_at)")
if err != nil {
logger.ErrorF(ctx, "[RiskControl] Prepare ClickHouse batch failed: %v", err)