feat(generation): persist summary outcomes without articles
Record generation outcomes and summaries for history while keeping article content transient. Refs #7 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
35
backend/internal/scheduler/generation_test.go
Normal file
35
backend/internal/scheduler/generation_test.go
Normal file
@ -0,0 +1,35 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/tradarr/backend/internal/generation"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
)
|
||||
|
||||
type scheduledUsers struct{}
|
||||
|
||||
func (scheduledUsers) ListUsers() ([]models.User, error) {
|
||||
return []models.User{{ID: "alpha"}, {ID: "beta"}}, nil
|
||||
}
|
||||
|
||||
type scheduledGenerator struct{ ids []string }
|
||||
|
||||
func (g *scheduledGenerator) Generate(_ context.Context, id string) (generation.Result, error) {
|
||||
g.ids = append(g.ids, id)
|
||||
if id == "alpha" {
|
||||
return generation.Result{}, errors.New("failure")
|
||||
}
|
||||
return generation.Result{}, nil
|
||||
}
|
||||
func TestScheduledGenerationContinuesIndependentlyPerUser(t *testing.T) {
|
||||
g := &scheduledGenerator{}
|
||||
s := &Scheduler{users: scheduledUsers{}, generationUseCase: g}
|
||||
s.run()
|
||||
if !reflect.DeepEqual(g.ids, []string{"alpha", "beta"}) {
|
||||
t.Fatalf("scheduled identities=%v", g.ids)
|
||||
}
|
||||
}
|
||||
@ -7,17 +7,22 @@ import (
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/tradarr/backend/internal/ai"
|
||||
"github.com/tradarr/backend/internal/generation"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
)
|
||||
|
||||
type userLister interface{ ListUsers() ([]models.User, error) }
|
||||
|
||||
type Scheduler struct {
|
||||
cron *cron.Cron
|
||||
registry *scraper.Registry
|
||||
pipeline *ai.Pipeline
|
||||
repo *models.Repository
|
||||
entryIDs []cron.EntryID
|
||||
running sync.Mutex
|
||||
users userLister
|
||||
generationUseCase generation.UseCase
|
||||
cron *cron.Cron
|
||||
registry *scraper.Registry
|
||||
pipeline *ai.Pipeline
|
||||
repo *models.Repository
|
||||
entryIDs []cron.EntryID
|
||||
running sync.Mutex
|
||||
}
|
||||
|
||||
func New(registry *scraper.Registry, pipeline *ai.Pipeline, repo *models.Repository) *Scheduler {
|
||||
@ -26,6 +31,7 @@ func New(registry *scraper.Registry, pipeline *ai.Pipeline, repo *models.Reposit
|
||||
registry: registry,
|
||||
pipeline: pipeline,
|
||||
repo: repo,
|
||||
users: repo,
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,14 +97,18 @@ func (s *Scheduler) run() {
|
||||
}
|
||||
}()
|
||||
|
||||
fmt.Println("scheduler: starting scraping cycle")
|
||||
if err := s.registry.RunAll(); err != nil {
|
||||
fmt.Printf("scheduler scrape error: %v\n", err)
|
||||
if s.generationUseCase == nil {
|
||||
return
|
||||
}
|
||||
fmt.Println("scheduler: starting AI summaries")
|
||||
if err := s.pipeline.GenerateForAll(context.Background()); err != nil {
|
||||
fmt.Printf("scheduler summary error: %v\n", err)
|
||||
users, err := s.users.ListUsers()
|
||||
if err != nil {
|
||||
fmt.Println("scheduler: unable to load accounts")
|
||||
return
|
||||
}
|
||||
for _, user := range users {
|
||||
if _, err := s.generationUseCase.Generate(context.Background(), user.ID); err != nil {
|
||||
fmt.Println("scheduler: generation failed")
|
||||
}
|
||||
}
|
||||
fmt.Println("scheduler: cycle complete")
|
||||
}
|
||||
func (s *Scheduler) SetGenerationUseCase(uc generation.UseCase) { s.generationUseCase = uc }
|
||||
|
||||
33
backend/internal/scheduler/scheduler_contract_test.go
Normal file
33
backend/internal/scheduler/scheduler_contract_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestScheduledCycleDoesNotPersistArticlesThroughRegistryRunAll(t *testing.T) {
|
||||
fset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fset, "scheduler.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, declaration := range file.Decls {
|
||||
function, ok := declaration.(*ast.FuncDecl)
|
||||
if !ok || function.Name.Name != "run" {
|
||||
continue
|
||||
}
|
||||
ast.Inspect(function.Body, func(node ast.Node) bool {
|
||||
call, ok := node.(*ast.CallExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
selector, ok := call.Fun.(*ast.SelectorExpr)
|
||||
if ok && selector.Sel.Name == "RunAll" {
|
||||
t.Errorf("scheduled generation still invokes Registry.RunAll at %s", fset.Position(call.Pos()))
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user