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:
dbastrikin
2026-03-18 20:09:33 +02:00
parent 0f533ccaeb
commit 48fd2baa8c
9 changed files with 186 additions and 28 deletions

View 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
}