tercul-backend/cmd/tools/enrich/main.go

76 lines
1.6 KiB
Go

package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"tercul/internal/enrich"
"tercul/internal/store"
"tercul/internal/platform/config"
)
func main() {
log.Println("Starting enrichment service...")
// Create a context that can be cancelled
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Set up signal handling
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigCh
log.Printf("Received signal %v, shutting down...", sig)
cancel()
}()
// Load configuration
config.LoadConfig()
// Connect to the database
dsn := os.Getenv("DATABASE_URL")
if dsn == "" {
dsn = config.Cfg.GetDSN()
}
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
// Create a store.DB
storeDB := &store.DB{DB: db}
// Create the enrichment registry
registry := enrich.DefaultRegistry()
// Process pending works
if err := store.ProcessPendingWorks(ctx, registry, storeDB); err != nil {
log.Fatalf("Failed to process pending works: %v", err)
}
// Set up a ticker to periodically process pending works
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
log.Println("Shutting down...")
return
case <-ticker.C:
log.Println("Processing pending works...")
if err := store.ProcessPendingWorks(ctx, registry, storeDB); err != nil {
log.Printf("Failed to process pending works: %v", err)
}
}
}
}