feat(server): hot-reload config store and reloadable infra clients

- Replace single Load() with a watching Store that re-reads config files
  (and config.local.yaml) and fans out a ReloadEvent with a per-field diff
  so consumers can decide whether the change is hot-applicable or requires
  a process restart.
- Wrap llm, retrieval, vector store, and object storage clients in
  Reloadable* shells so the bootstrap can swap their underlying impls when
  config changes without re-instantiating handlers.
- Make jwt.Manager and ops TokenIssuer mutable under a lock so secrets and
  TTLs can be rotated live; thread default plan code through a setter on
  the ops AdminUserService.
- Wire ConfigStore through bootstrap and every cmd/main.go, scheduler /
  worker / tenant-api / ops-api start the watcher; services and handlers
  take a config.Provider so they always read current values for things
  like generation.stream_enabled, scheduler dispatch, retrieval, etc.
- Switch shared/config decoding off viper to a Kratos-derived runtime
  package so env placeholders (\${VAR:default}) resolve consistently and
  the same source machinery powers both the loader and the watcher.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-01 16:01:23 +08:00
parent ce2d8a2907
commit 618399f86d
61 changed files with 3186 additions and 496 deletions
+21 -1
View File
@@ -30,10 +30,12 @@ func main() {
configPath = p
}
cfg, err := opsconfig.Load(configPath)
configStore, err := opsconfig.NewStore(configPath)
if err != nil {
log.Fatalf("ops-api load config: %v", err)
}
defer configStore.Close()
cfg := configStore.Current()
logger, err := observability.NewLogger(cfg.Log.Level, cfg.Log.Format)
if err != nil {
@@ -92,6 +94,23 @@ func main() {
logger.Sugar().Fatalf("ops-api seed default admin: %v", err)
}
watcherCtx, watcherCancel := context.WithCancel(context.Background())
defer watcherCancel()
go func() {
if err := configStore.Watch(watcherCtx, logger, func(event opsconfig.ReloadEvent) {
if event.Current == nil {
return
}
issuer.Update(event.Current.JWT.Secret, event.Current.JWT.AccessTTL)
adminUserSvc.UpdateDefaultPlanCode(event.Current.AdminUsers.DefaultPlanCode)
if opsconfig.RestartRequired(event.Changes) {
logger.Warn("ops config hot reload changed fields that require process restart")
}
}); err != nil {
logger.Warn("ops config hot reload watcher stopped", zap.Error(err))
}
}()
if cfg.Server.Mode == "release" {
gin.SetMode(gin.ReleaseMode)
}
@@ -130,6 +149,7 @@ func main() {
<-quit
logger.Info("ops-api shutting down...")
watcherCancel()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {