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
@@ -13,6 +13,7 @@ import (
"go.uber.org/zap"
sharedcache "github.com/geo-platform/tenant-api/internal/shared/cache"
"github.com/geo-platform/tenant-api/internal/shared/config"
"github.com/geo-platform/tenant-api/internal/shared/llm"
"github.com/geo-platform/tenant-api/internal/shared/messaging/rabbitmq"
tenantapp "github.com/geo-platform/tenant-api/internal/tenant/app"
@@ -30,6 +31,7 @@ type KolAssistWorker struct {
cache sharedcache.Cache
retryInterval time.Duration
processTimeout time.Duration
configProvider config.Provider
consumerPrefix string
workerConcurrency int
}
@@ -65,6 +67,11 @@ func NewKolAssistWorker(
}
}
func (w *KolAssistWorker) WithConfigProvider(provider config.Provider) *KolAssistWorker {
w.configProvider = provider
return w
}
func (w *KolAssistWorker) Start(ctx context.Context) {
if w == nil || w.rabbitMQ == nil || w.repo == nil || w.llm == nil {
return
@@ -166,7 +173,7 @@ func (w *KolAssistWorker) handleDelivery(parent context.Context, delivery amqp.D
return
}
ctx, cancel := context.WithTimeout(parent, w.processTimeout)
ctx, cancel := context.WithTimeout(parent, w.runtimeProcessTimeout())
defer cancel()
if err := w.Process(ctx, *job); err != nil {
@@ -184,6 +191,25 @@ func (w *KolAssistWorker) handleDelivery(parent context.Context, delivery amqp.D
}
}
func (w *KolAssistWorker) runtimeProcessTimeout() time.Duration {
if w != nil && w.configProvider != nil {
if cfg := w.configProvider.Current(); cfg != nil {
return kolAssistProcessTimeout(cfg.Generation)
}
}
if w != nil && w.processTimeout > 0 {
return w.processTimeout
}
return kolAssistProcessTimeout(config.GenerationConfig{})
}
func kolAssistProcessTimeout(cfg config.GenerationConfig) time.Duration {
if cfg.ArticleTimeout > 0 {
return cfg.ArticleTimeout
}
return 5 * time.Minute
}
func (w *KolAssistWorker) rejectDelivery(delivery amqp.Delivery, requeue bool, err error, taskID string, tenantID int64) {
if nackErr := delivery.Nack(false, requeue); nackErr != nil && w.logger != nil {
w.logger.Warn("kol assist nack failed",
@@ -34,6 +34,7 @@ type ArticleGenerationWorker struct {
logger *zap.Logger
retryInterval time.Duration
processTimeout time.Duration
configProvider config.Provider
consumerPrefix string
workerConcurrency int
}
@@ -53,11 +54,6 @@ func NewArticleGenerationWorker(
workerConcurrency = 1
}
processTimeout := cfg.ArticleTimeout + 2*time.Minute
if processTimeout <= 2*time.Minute {
processTimeout = 10 * time.Minute
}
return &ArticleGenerationWorker{
pool: pool,
rabbitMQ: rabbitMQClient,
@@ -67,12 +63,17 @@ func NewArticleGenerationWorker(
kolWorker: kolWorker,
logger: logger,
retryInterval: defaultArticleGenerationWorkerRetryInterval,
processTimeout: processTimeout,
processTimeout: articleGenerationProcessTimeout(cfg),
consumerPrefix: fmt.Sprintf("worker-generate-%d", os.Getpid()),
workerConcurrency: workerConcurrency,
}
}
func (w *ArticleGenerationWorker) WithConfigProvider(provider config.Provider) *ArticleGenerationWorker {
w.configProvider = provider
return w
}
func (w *ArticleGenerationWorker) Start(ctx context.Context) {
if w == nil || w.pool == nil || w.rabbitMQ == nil {
return
@@ -134,7 +135,7 @@ func (w *ArticleGenerationWorker) handleDelivery(parent context.Context, deliver
return
}
ctx, cancel := context.WithTimeout(parent, w.processTimeout)
ctx, cancel := context.WithTimeout(parent, w.runtimeProcessTimeout())
defer cancel()
task, terminal, err := w.loadTask(ctx, envelope.TaskID)
@@ -205,6 +206,26 @@ func (w *ArticleGenerationWorker) handleDelivery(parent context.Context, deliver
}
}
func (w *ArticleGenerationWorker) runtimeProcessTimeout() time.Duration {
if w != nil && w.configProvider != nil {
if cfg := w.configProvider.Current(); cfg != nil {
return articleGenerationProcessTimeout(cfg.Generation)
}
}
if w != nil && w.processTimeout > 0 {
return w.processTimeout
}
return articleGenerationProcessTimeout(config.GenerationConfig{})
}
func articleGenerationProcessTimeout(cfg config.GenerationConfig) time.Duration {
processTimeout := cfg.ArticleTimeout + 2*time.Minute
if processTimeout <= 2*time.Minute {
return 10 * time.Minute
}
return processTimeout
}
func (w *ArticleGenerationWorker) handleTaskFailure(ctx context.Context, task tenantapp.ClaimedGenerationTask, stage string, taskErr error) {
task.TaskType = strings.TrimSpace(task.TaskType)
switch {
@@ -23,13 +23,14 @@ import (
const kolGenerationTaskType = "kol"
type KolGenerationWorker struct {
pool *pgxpool.Pool
llm llm.Client
knowledge *tenantapp.KnowledgeService
logger *zap.Logger
articleTimeout time.Duration
maxOutputTokens int64
cache sharedcache.Cache
pool *pgxpool.Pool
llm llm.Client
knowledge *tenantapp.KnowledgeService
logger *zap.Logger
cfg config.GenerationConfig
llmCfg config.LLMConfig
provider config.Provider
cache sharedcache.Cache
}
func NewKolGenerationWorker(
@@ -40,18 +41,13 @@ func NewKolGenerationWorker(
cfg config.GenerationConfig,
llmMaxOutputTokens int64,
) *KolGenerationWorker {
articleTimeout := cfg.ArticleTimeout
if articleTimeout <= 0 {
articleTimeout = 8 * time.Minute
}
return &KolGenerationWorker{
pool: pool,
llm: llmClient,
knowledge: knowledgeSvc,
logger: logger,
articleTimeout: articleTimeout,
maxOutputTokens: llmMaxOutputTokens,
pool: pool,
llm: llmClient,
knowledge: knowledgeSvc,
logger: logger,
cfg: cfg,
llmCfg: config.LLMConfig{MaxOutputTokens: llmMaxOutputTokens},
}
}
@@ -60,6 +56,23 @@ func (w *KolGenerationWorker) WithCache(c sharedcache.Cache) *KolGenerationWorke
return w
}
func (w *KolGenerationWorker) WithConfigProvider(provider config.Provider) *KolGenerationWorker {
w.provider = provider
return w
}
func (w *KolGenerationWorker) runtimeConfig() (time.Duration, int64) {
if w != nil && w.provider != nil {
if cfg := w.provider.Current(); cfg != nil {
return kolArticleTimeout(cfg.Generation), cfg.LLM.MaxOutputTokens
}
}
if w == nil {
return 8 * time.Minute, 0
}
return kolArticleTimeout(w.cfg), w.llmCfg.MaxOutputTokens
}
func (w *KolGenerationWorker) Process(ctx context.Context, task tenantapp.ClaimedGenerationTask) {
renderedPrompt := strings.TrimSpace(kolStringInput(task.InputParams, "rendered_prompt"))
if renderedPrompt == "" {
@@ -114,10 +127,11 @@ func (w *KolGenerationWorker) Process(ctx context.Context, task tenantapp.Claime
prompt = strings.TrimSpace(renderedPrompt + "\n\n" + knowledgePrompt)
}
articleTimeout, maxOutputTokens := w.runtimeConfig()
generateReq := llm.GenerateRequest{
Prompt: prompt,
Timeout: w.articleTimeout,
MaxOutputTokens: w.maxOutputTokens,
Timeout: articleTimeout,
MaxOutputTokens: maxOutputTokens,
}
if kolBoolInput(task.InputParams, "enable_web_search") {
generateReq.WebSearch = &llm.WebSearchOptions{Enabled: true}
@@ -236,6 +250,13 @@ var (
errEmptyGeneratedContent = errors.New("kol generation returned empty content")
)
func kolArticleTimeout(cfg config.GenerationConfig) time.Duration {
if cfg.ArticleTimeout > 0 {
return cfg.ArticleTimeout
}
return 8 * time.Minute
}
func kolStringInput(input map[string]interface{}, key string) string {
if input == nil {
return ""
@@ -10,6 +10,7 @@ import (
amqp "github.com/rabbitmq/amqp091-go"
"go.uber.org/zap"
"github.com/geo-platform/tenant-api/internal/shared/config"
"github.com/geo-platform/tenant-api/internal/shared/messaging/rabbitmq"
tenantapp "github.com/geo-platform/tenant-api/internal/tenant/app"
)
@@ -22,6 +23,7 @@ type TemplateAssistWorker struct {
logger *zap.Logger
retryInterval time.Duration
processTimeout time.Duration
configProvider config.Provider
consumerPrefix string
workerConcurrency int
}
@@ -51,6 +53,11 @@ func NewTemplateAssistWorker(
}
}
func (w *TemplateAssistWorker) WithConfigProvider(provider config.Provider) *TemplateAssistWorker {
w.configProvider = provider
return w
}
func (w *TemplateAssistWorker) Start(ctx context.Context) {
if w == nil || w.rabbitMQ == nil || w.templateService == nil {
return
@@ -112,7 +119,7 @@ func (w *TemplateAssistWorker) handleDelivery(parent context.Context, delivery a
return
}
ctx, cancel := context.WithTimeout(parent, w.processTimeout)
ctx, cancel := context.WithTimeout(parent, w.runtimeProcessTimeout())
defer cancel()
terminal, err := w.templateService.IsAssistTaskTerminal(ctx, *job)
@@ -143,6 +150,25 @@ func (w *TemplateAssistWorker) handleDelivery(parent context.Context, delivery a
}
}
func (w *TemplateAssistWorker) runtimeProcessTimeout() time.Duration {
if w != nil && w.configProvider != nil {
if cfg := w.configProvider.Current(); cfg != nil {
return templateAssistProcessTimeout(cfg.Generation)
}
}
if w != nil && w.processTimeout > 0 {
return w.processTimeout
}
return templateAssistProcessTimeout(config.GenerationConfig{})
}
func templateAssistProcessTimeout(cfg config.GenerationConfig) time.Duration {
if cfg.ArticleTimeout > 0 {
return cfg.ArticleTimeout
}
return 5 * time.Minute
}
func (w *TemplateAssistWorker) rejectDelivery(delivery amqp.Delivery, requeue bool, err error, taskID, taskType string) {
if nackErr := delivery.Nack(false, requeue); nackErr != nil && w.logger != nil {
w.logger.Warn("template assist nack failed",