feat: split worker into separate binary (cmd/worker)
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>
This commit is contained in:
58
backend/cmd/worker/init.go
Normal file
58
backend/cmd/worker/init.go
Normal file
@@ -0,0 +1,58 @@
|
||||
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
|
||||
}
|
||||
58
backend/cmd/worker/main.go
Normal file
58
backend/cmd/worker/main.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/food-ai/backend/internal/infra/database"
|
||||
"github.com/food-ai/backend/internal/infra/locale"
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelInfo,
|
||||
}))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
if runError := run(); runError != nil {
|
||||
slog.Error("fatal error", "err", runError)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
workerConfig, configError := loadConfig()
|
||||
if configError != nil {
|
||||
return fmt.Errorf("load config: %w", configError)
|
||||
}
|
||||
|
||||
applicationContext, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
pool, poolError := database.NewPool(applicationContext, workerConfig.DatabaseURL)
|
||||
if poolError != nil {
|
||||
return fmt.Errorf("connect to database: %w", poolError)
|
||||
}
|
||||
defer pool.Close()
|
||||
slog.Info("connected to database")
|
||||
|
||||
if loadError := locale.LoadFromDB(applicationContext, pool); loadError != nil {
|
||||
return fmt.Errorf("load languages: %w", loadError)
|
||||
}
|
||||
|
||||
workerApp, initError := initWorker(workerConfig, pool)
|
||||
if initError != nil {
|
||||
return fmt.Errorf("init worker: %w", initError)
|
||||
}
|
||||
|
||||
workerApp.Start(applicationContext)
|
||||
slog.Info("worker started")
|
||||
|
||||
<-applicationContext.Done()
|
||||
slog.Info("worker shutting down...")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user