tercul-backend/internal/app/server_factory.go
2025-09-05 21:37:42 +00:00

81 lines
2.3 KiB
Go

package app
import (
"tercul/internal/jobs/linguistics"
syncjob "tercul/internal/jobs/sync"
"tercul/internal/platform/config"
"tercul/internal/platform/log"
"github.com/hibiken/asynq"
)
// ServerFactory handles the creation of HTTP and background job servers
type ServerFactory struct {
appBuilder *ApplicationBuilder
}
// NewServerFactory creates a new ServerFactory
func NewServerFactory(appBuilder *ApplicationBuilder) *ServerFactory {
return &ServerFactory{
appBuilder: appBuilder,
}
}
// CreateBackgroundJobServers creates and configures background job servers
func (f *ServerFactory) CreateBackgroundJobServers() ([]*asynq.Server, error) {
log.LogInfo("Setting up background job servers")
redisOpt := asynq.RedisClientOpt{
Addr: config.Cfg.RedisAddr,
Password: config.Cfg.RedisPassword,
DB: config.Cfg.RedisDB,
}
var servers []*asynq.Server
// Setup data synchronization server
log.LogInfo("Setting up data synchronization server",
log.F("concurrency", config.Cfg.MaxRetries))
syncServer := asynq.NewServer(redisOpt, asynq.Config{Concurrency: config.Cfg.MaxRetries})
// Create sync job instance
syncJobInstance := syncjob.NewSyncJob(
f.appBuilder.GetDB(),
f.appBuilder.GetAsynq(),
)
// Register sync job handlers
syncjob.RegisterQueueHandlers(syncServer, syncJobInstance)
servers = append(servers, syncServer)
// Setup linguistic analysis server
log.LogInfo("Setting up linguistic analysis server",
log.F("concurrency", config.Cfg.MaxRetries))
// Create linguistic sync job
linguisticSyncJob := linguistics.NewLinguisticSyncJob(
f.appBuilder.GetDB(),
f.appBuilder.GetLinguisticsFactory().GetAnalyzer(),
f.appBuilder.GetAsynq(),
)
// Create linguistic server and register handlers
linguisticServer := asynq.NewServer(redisOpt, asynq.Config{Concurrency: config.Cfg.MaxRetries})
// Register linguistic handlers
linguisticMux := asynq.NewServeMux()
linguistics.RegisterLinguisticHandlers(linguisticMux, linguisticSyncJob)
// For now, we'll need to run the server with the mux when it's started
// This is a temporary workaround - in production, you'd want to properly configure the server
servers = append(servers, linguisticServer)
log.LogInfo("Background job servers created successfully",
log.F("serverCount", len(servers)))
return servers, nil
}