Files
geo/server/cmd/worker-generate/main.go
T
root c1ef717d17
Deployment Config CI / Deployment Config (push) Successful in 24s
Backend CI / Backend (push) Successful in 14m33s
feat(cache,worker): overhaul cache layer and add generation task lease recovery
- shared/cache: add Options/L1/async/metrics/prefix decorators, multi-key ops, Redis pool tuning, and JSON readthrough metrics
- worker-generate: claim tasks via DB lease + heartbeat, requeue stale queued tasks, expire dead leases with refund/cache invalidation
- tenant: version article cache keys so worker recovery invalidations propagate cleanly
- shared/config: expand Redis (pool/timeouts/TLS) and Generation (lease/recovery) configs with defaults

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 02:02:39 +08:00

128 lines
2.9 KiB
Go

package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"github.com/geo-platform/tenant-api/internal/bootstrap"
tenantapp "github.com/geo-platform/tenant-api/internal/tenant/app"
"github.com/geo-platform/tenant-api/internal/tenant/repository"
generateworker "github.com/geo-platform/tenant-api/internal/worker/generate"
)
func main() {
configPath := "configs/config.yaml"
if p := os.Getenv("CONFIG_PATH"); p != "" {
configPath = p
}
app, err := bootstrap.New(configPath)
if err != nil {
log.Fatalf("bootstrap: %v", err)
}
defer app.Close()
generationProvider := tenantapp.NewGenerationStreamDisabledConfigProvider(app.ConfigStore)
generationCfg := app.Config().Generation
generationCfg.StreamEnabled = false
knowledgeSvc := tenantapp.NewKnowledgeService(
app.DB,
app.RetrievalProvider,
app.VectorStore,
app.ObjectStorage,
app.LLM,
app.Logger,
app.Config().Retrieval,
app.Config().LLM,
0,
0,
).WithConfigProvider(app.ConfigStore)
templateSvc := tenantapp.NewTemplateService(
app.DB,
repository.NewCachedTemplateRepository(
repository.NewTemplateRepository(app.DB),
app.Cache,
),
app.LLM,
nil,
knowledgeSvc,
app.GenerationStreams,
generationCfg,
app.Config().LLM.MaxOutputTokens,
).WithCache(app.Cache).WithConfigProvider(generationProvider)
promptRuleSvc := tenantapp.NewPromptRuleGenerationService(
app.DB,
app.LLM,
nil,
knowledgeSvc,
app.GenerationStreams,
generationCfg,
app.Config().LLM.MaxOutputTokens,
).WithCache(app.Cache).WithLogger(app.Logger).WithConfigProvider(generationProvider)
publishJobSvc := tenantapp.NewPublishJobService(
app.DB,
app.RabbitMQ,
app.Redis,
app.Logger,
).WithCache(app.Cache)
promptRuleSvc.WithPublishJobService(publishJobSvc)
imitationSvc := tenantapp.NewArticleImitationService(
app.DB,
app.LLM,
nil,
knowledgeSvc,
app.GenerationStreams,
generationCfg,
app.Config().LLM.MaxOutputTokens,
).WithCache(app.Cache).WithConfigProvider(generationProvider)
kolWorker := generateworker.NewKolGenerationWorker(
app.DB,
app.LLM,
knowledgeSvc,
app.Logger,
generationCfg,
app.Config().LLM.MaxOutputTokens,
).WithCache(app.Cache).WithConfigProvider(app.ConfigStore)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
app.StartConfigWatcher(ctx)
generateworker.NewArticleGenerationWorker(
app.DB,
app.RabbitMQ,
templateSvc,
promptRuleSvc,
imitationSvc,
kolWorker,
app.Logger,
app.Config().Generation,
).WithCache(app.Cache).WithConfigProvider(app.ConfigStore).Start(ctx)
generateworker.NewTemplateAssistWorker(
app.RabbitMQ,
templateSvc,
app.Logger,
app.Config().Generation.WorkerConcurrency,
app.Config().Generation.ArticleTimeout,
).WithConfigProvider(app.ConfigStore).Start(ctx)
app.Logger.Info("worker-generate started")
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
cancel()
app.Logger.Info("worker-generate shutting down...")
}