Kafka consumers and WorkerPool are moved out of the server process into a dedicated worker binary. Server now handles HTTP + SSE only; worker handles Kafka consumption and AI processing. - cmd/worker/main.go + init.go: new binary with minimal config (DATABASE_URL, OPENAI_API_KEY, KAFKA_BROKERS) - cmd/server: remove WorkerPool, paidConsumer, freeConsumer - Dockerfile: builds both server and worker binaries - docker-compose.yml: add worker service - Makefile: add run-worker and docker-logs-worker targets - README.md: document worker startup and env vars Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/food-ai/backend/internal/adapters/kafka"
|
|
"github.com/food-ai/backend/internal/adapters/openai"
|
|
"github.com/food-ai/backend/internal/domain/dish"
|
|
"github.com/food-ai/backend/internal/domain/recognition"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/kelseyhightower/envconfig"
|
|
)
|
|
|
|
type workerConfig struct {
|
|
DatabaseURL string `envconfig:"DATABASE_URL" required:"true"`
|
|
OpenAIAPIKey string `envconfig:"OPENAI_API_KEY" required:"true"`
|
|
KafkaBrokers []string `envconfig:"KAFKA_BROKERS" default:"kafka:9092"`
|
|
}
|
|
|
|
func loadConfig() (*workerConfig, error) {
|
|
var workerCfg workerConfig
|
|
if configError := envconfig.Process("", &workerCfg); configError != nil {
|
|
return nil, configError
|
|
}
|
|
return &workerCfg, nil
|
|
}
|
|
|
|
// WorkerApp bundles background services that need lifecycle management.
|
|
type WorkerApp struct {
|
|
workerPool *recognition.WorkerPool
|
|
}
|
|
|
|
// Start launches the worker pool goroutines.
|
|
func (workerApp *WorkerApp) Start(applicationContext context.Context) {
|
|
workerApp.workerPool.Start(applicationContext)
|
|
}
|
|
|
|
func initWorker(workerCfg *workerConfig, pool *pgxpool.Pool) (*WorkerApp, error) {
|
|
openaiClient := openai.NewClient(workerCfg.OpenAIAPIKey)
|
|
dishRepository := dish.NewRepository(pool)
|
|
jobRepository := recognition.NewJobRepository(pool)
|
|
|
|
paidConsumer, paidConsumerError := kafka.NewConsumer(
|
|
workerCfg.KafkaBrokers, "dish-recognition-workers", recognition.TopicPaid,
|
|
)
|
|
if paidConsumerError != nil {
|
|
return nil, paidConsumerError
|
|
}
|
|
freeConsumer, freeConsumerError := kafka.NewConsumer(
|
|
workerCfg.KafkaBrokers, "dish-recognition-workers", recognition.TopicFree,
|
|
)
|
|
if freeConsumerError != nil {
|
|
return nil, freeConsumerError
|
|
}
|
|
|
|
workerPool := recognition.NewWorkerPool(jobRepository, openaiClient, dishRepository, paidConsumer, freeConsumer)
|
|
return &WorkerApp{workerPool: workerPool}, nil
|
|
}
|