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:
@@ -31,39 +31,46 @@ import (
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Config *config.Config
|
||||
Logger *zap.Logger
|
||||
DB *pgxpool.Pool
|
||||
MonitoringDB *pgxpool.Pool
|
||||
RabbitMQ *rabbitmq.Client
|
||||
Redis *goredis.Client
|
||||
Engine *gin.Engine
|
||||
JWT *auth.Manager
|
||||
Sessions *auth.SessionStore
|
||||
LoginGuard *auth.LoginGuard
|
||||
LLM llm.Client
|
||||
RetrievalProvider retrieval.Provider
|
||||
VectorStore retrieval.VectorStore
|
||||
ObjectStorage objectstorage.Client
|
||||
AuditLogs *auditlog.AsyncWriter
|
||||
GenerationStreams *stream.GenerationHub
|
||||
DesktopTaskStreams *stream.DesktopTaskHub
|
||||
DesktopDispatch *stream.DesktopDispatchHub
|
||||
Cache cache.Cache
|
||||
KolProfiles repository.KolProfileRepository
|
||||
KolPackages repository.KolPackageRepository
|
||||
KolMarketplace repository.KolMarketplaceRepository
|
||||
KolPrompts repository.KolPromptRepository
|
||||
KolSubscriptions repository.KolSubscriptionRepository
|
||||
KolAssists repository.KolAssistRepository
|
||||
KolPromptAsset *tenantapp.KolPromptAsset
|
||||
ConfigStore *config.Store
|
||||
Logger *zap.Logger
|
||||
DB *pgxpool.Pool
|
||||
MonitoringDB *pgxpool.Pool
|
||||
RabbitMQ *rabbitmq.Client
|
||||
Redis *goredis.Client
|
||||
Engine *gin.Engine
|
||||
JWT *auth.Manager
|
||||
Sessions *auth.SessionStore
|
||||
LoginGuard *auth.LoginGuard
|
||||
LLM llm.Client
|
||||
ReloadableLLM *llm.ReloadableClient
|
||||
RetrievalProvider retrieval.Provider
|
||||
ReloadableRetrieval *retrieval.ReloadableProvider
|
||||
VectorStore retrieval.VectorStore
|
||||
ReloadableVectorStore *retrieval.ReloadableVectorStore
|
||||
ObjectStorage objectstorage.Client
|
||||
ReloadableObjectStorage *objectstorage.ReloadableClient
|
||||
AuditLogs *auditlog.AsyncWriter
|
||||
GenerationStreams *stream.GenerationHub
|
||||
DesktopTaskStreams *stream.DesktopTaskHub
|
||||
DesktopDispatch *stream.DesktopDispatchHub
|
||||
Cache cache.Cache
|
||||
BrandService *tenantapp.BrandService
|
||||
MonitoringService *tenantapp.MonitoringService
|
||||
KolProfiles repository.KolProfileRepository
|
||||
KolPackages repository.KolPackageRepository
|
||||
KolMarketplace repository.KolMarketplaceRepository
|
||||
KolPrompts repository.KolPromptRepository
|
||||
KolSubscriptions repository.KolSubscriptionRepository
|
||||
KolAssists repository.KolAssistRepository
|
||||
KolPromptAsset *tenantapp.KolPromptAsset
|
||||
}
|
||||
|
||||
func New(configPath string) (*App, error) {
|
||||
cfg, err := config.Load(configPath)
|
||||
configStore, err := config.NewStore(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
cfg := configStore.Current()
|
||||
prompts.SetConfigFile(filepath.Join(filepath.Dir(configPath), "prompts.yml"))
|
||||
|
||||
logger, err := observability.NewLogger(cfg.Log.Level, cfg.Log.Format)
|
||||
@@ -106,10 +113,10 @@ func New(configPath string) (*App, error) {
|
||||
jwtMgr := auth.NewManager(cfg.JWT.Secret, cfg.JWT.AccessTTL, cfg.JWT.RefreshTTL)
|
||||
sessions := auth.NewSessionStore(rdb)
|
||||
loginGuard := auth.NewLoginGuard(rdb, auth.DefaultLoginGuardConfig("tenant"))
|
||||
llmClient := llm.New(cfg.LLM)
|
||||
retrievalProvider := retrieval.NewProvider(cfg.Retrieval, logger)
|
||||
vectorStore := retrieval.NewQdrantStore(cfg.Qdrant, logger)
|
||||
objectStorageClient := objectstorage.New(cfg.ObjectStorage, logger)
|
||||
llmClient := llm.NewReloadableClient(llm.New(cfg.LLM))
|
||||
retrievalProvider := retrieval.NewReloadableProvider(retrieval.NewProvider(cfg.Retrieval, logger))
|
||||
vectorStore := retrieval.NewReloadableVectorStore(retrieval.NewQdrantStore(cfg.Qdrant, logger))
|
||||
objectStorageClient := objectstorage.NewReloadableClient(objectstorage.New(cfg.ObjectStorage, logger))
|
||||
auditLogs := auditlog.NewAsyncWriter(pool, logger)
|
||||
generationStreams := stream.NewGenerationHub(mqClient)
|
||||
desktopTaskStreams := stream.NewDesktopTaskHub(mqClient)
|
||||
@@ -120,6 +127,8 @@ func New(configPath string) (*App, error) {
|
||||
}
|
||||
desktopDispatch := stream.NewDesktopDispatchHub(mqClient, logger, desktopDispatchBindings)
|
||||
appCache := cache.New(cfg.Cache.Driver, rdb)
|
||||
brandService := tenantapp.NewBrandService(pool, monitoringPool, auditLogs, cfg.BrandLibrary).WithCache(appCache)
|
||||
monitoringService := tenantapp.NewMonitoringService(pool, monitoringPool, mqClient, cfg.MonitoringDispatch, cfg.BrandLibrary, logger).WithRedis(rdb)
|
||||
kolProfiles := repository.NewKolProfileRepository(pool)
|
||||
kolPackages := repository.NewKolPackageRepository(pool)
|
||||
kolMarketplace := repository.NewKolMarketplaceRepository(pool)
|
||||
@@ -164,35 +173,93 @@ func New(configPath string) (*App, error) {
|
||||
})
|
||||
|
||||
return &App{
|
||||
Config: cfg,
|
||||
Logger: logger,
|
||||
DB: pool,
|
||||
MonitoringDB: monitoringPool,
|
||||
RabbitMQ: mqClient,
|
||||
Redis: rdb,
|
||||
Engine: engine,
|
||||
JWT: jwtMgr,
|
||||
Sessions: sessions,
|
||||
LoginGuard: loginGuard,
|
||||
LLM: llmClient,
|
||||
RetrievalProvider: retrievalProvider,
|
||||
VectorStore: vectorStore,
|
||||
ObjectStorage: objectStorageClient,
|
||||
AuditLogs: auditLogs,
|
||||
GenerationStreams: generationStreams,
|
||||
DesktopTaskStreams: desktopTaskStreams,
|
||||
DesktopDispatch: desktopDispatch,
|
||||
Cache: appCache,
|
||||
KolProfiles: kolProfiles,
|
||||
KolPackages: kolPackages,
|
||||
KolMarketplace: kolMarketplace,
|
||||
KolPrompts: kolPrompts,
|
||||
KolSubscriptions: kolSubscriptions,
|
||||
KolAssists: kolAssists,
|
||||
KolPromptAsset: kolPromptAsset,
|
||||
ConfigStore: configStore,
|
||||
Logger: logger,
|
||||
DB: pool,
|
||||
MonitoringDB: monitoringPool,
|
||||
RabbitMQ: mqClient,
|
||||
Redis: rdb,
|
||||
Engine: engine,
|
||||
JWT: jwtMgr,
|
||||
Sessions: sessions,
|
||||
LoginGuard: loginGuard,
|
||||
LLM: llmClient,
|
||||
ReloadableLLM: llmClient,
|
||||
RetrievalProvider: retrievalProvider,
|
||||
ReloadableRetrieval: retrievalProvider,
|
||||
VectorStore: vectorStore,
|
||||
ReloadableVectorStore: vectorStore,
|
||||
ObjectStorage: objectStorageClient,
|
||||
ReloadableObjectStorage: objectStorageClient,
|
||||
AuditLogs: auditLogs,
|
||||
GenerationStreams: generationStreams,
|
||||
DesktopTaskStreams: desktopTaskStreams,
|
||||
DesktopDispatch: desktopDispatch,
|
||||
Cache: appCache,
|
||||
BrandService: brandService,
|
||||
MonitoringService: monitoringService,
|
||||
KolProfiles: kolProfiles,
|
||||
KolPackages: kolPackages,
|
||||
KolMarketplace: kolMarketplace,
|
||||
KolPrompts: kolPrompts,
|
||||
KolSubscriptions: kolSubscriptions,
|
||||
KolAssists: kolAssists,
|
||||
KolPromptAsset: kolPromptAsset,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *App) Config() *config.Config {
|
||||
if a == nil || a.ConfigStore == nil {
|
||||
return nil
|
||||
}
|
||||
return a.ConfigStore.Current()
|
||||
}
|
||||
|
||||
func (a *App) StartConfigWatcher(ctx context.Context) {
|
||||
if a == nil || a.ConfigStore == nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
if err := a.ConfigStore.Watch(ctx, a.Logger, a.applyReloadedConfig); err != nil && a.Logger != nil {
|
||||
a.Logger.Warn("config hot reload watcher stopped", zap.Error(err))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (a *App) applyReloadedConfig(event config.ReloadEvent) {
|
||||
if a == nil || event.Current == nil {
|
||||
return
|
||||
}
|
||||
|
||||
a.JWT.Update(event.Current.JWT.Secret, event.Current.JWT.AccessTTL, event.Current.JWT.RefreshTTL)
|
||||
if a.ReloadableLLM != nil {
|
||||
a.ReloadableLLM.Update(llm.New(event.Current.LLM))
|
||||
}
|
||||
if a.ReloadableRetrieval != nil {
|
||||
a.ReloadableRetrieval.Update(retrieval.NewProvider(event.Current.Retrieval, a.Logger))
|
||||
}
|
||||
if a.ReloadableVectorStore != nil {
|
||||
a.ReloadableVectorStore.Update(retrieval.NewQdrantStore(event.Current.Qdrant, a.Logger))
|
||||
}
|
||||
if a.ReloadableObjectStorage != nil {
|
||||
a.ReloadableObjectStorage.Update(objectstorage.New(event.Current.ObjectStorage, a.Logger))
|
||||
}
|
||||
if a.BrandService != nil {
|
||||
a.BrandService.UpdateConfig(event.Current.BrandLibrary)
|
||||
}
|
||||
if a.MonitoringService != nil {
|
||||
a.MonitoringService.UpdateConfig(event.Current.MonitoringDispatch, event.Current.BrandLibrary)
|
||||
}
|
||||
|
||||
if err := syncMembershipPlans(context.Background(), a.DB, event.Current.Membership); err != nil {
|
||||
a.Logger.Warn("config reload membership plan sync failed", zap.Error(err))
|
||||
}
|
||||
|
||||
if config.RestartRequired(event.Changes) && a.Logger != nil {
|
||||
a.Logger.Warn("config reload changed connection or listener settings; restart process to apply those fields")
|
||||
}
|
||||
}
|
||||
|
||||
// syncMembershipPlans upserts all configured plans in a single transaction so
|
||||
// that a partial failure (or concurrent startup) never leaves the `plans` table
|
||||
// in a half-synced state.
|
||||
@@ -212,6 +279,9 @@ func syncMembershipPlans(ctx context.Context, pool *pgxpool.Pool, membership con
|
||||
}
|
||||
|
||||
func (a *App) Close() {
|
||||
if a.ConfigStore != nil {
|
||||
a.ConfigStore.Close()
|
||||
}
|
||||
if a.AuditLogs != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
if err := a.AuditLogs.Close(ctx); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user