Record generation outcomes and summaries for history while keeping article content transient. Refs #7 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"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 {
|
|
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 {
|
|
return &Scheduler{
|
|
cron: cron.New(),
|
|
registry: registry,
|
|
pipeline: pipeline,
|
|
repo: repo,
|
|
users: repo,
|
|
}
|
|
}
|
|
|
|
func (s *Scheduler) Start() error {
|
|
if err := s.loadSchedule(); err != nil {
|
|
return err
|
|
}
|
|
s.cron.Start()
|
|
return nil
|
|
}
|
|
|
|
func (s *Scheduler) Stop() {
|
|
s.cron.Stop()
|
|
}
|
|
|
|
func (s *Scheduler) Reload() error {
|
|
for _, id := range s.entryIDs {
|
|
s.cron.Remove(id)
|
|
}
|
|
s.entryIDs = nil
|
|
return s.loadSchedule()
|
|
}
|
|
|
|
func (s *Scheduler) loadSchedule() error {
|
|
slots, err := s.repo.ListScheduleSlots()
|
|
if err != nil {
|
|
return fmt.Errorf("load schedule: %w", err)
|
|
}
|
|
if len(slots) == 0 {
|
|
fmt.Println("scheduler: no schedule configured, scraping disabled")
|
|
return nil
|
|
}
|
|
|
|
tz, _ := s.repo.GetSetting("timezone")
|
|
if tz == "" {
|
|
tz = "UTC"
|
|
}
|
|
|
|
for _, slot := range slots {
|
|
// TZ= prefix permet à robfig/cron d'interpréter les heures dans le fuseau configuré
|
|
spec := fmt.Sprintf("TZ=%s %d %d * * %d", tz, slot.Minute, slot.Hour, slot.DayOfWeek)
|
|
id, err := s.cron.AddFunc(spec, s.run)
|
|
if err != nil {
|
|
fmt.Printf("scheduler: invalid cron spec %q: %v\n", spec, err)
|
|
continue
|
|
}
|
|
s.entryIDs = append(s.entryIDs, id)
|
|
}
|
|
|
|
fmt.Printf("scheduler: %d time slots loaded (timezone: %s)\n", len(s.entryIDs), tz)
|
|
return nil
|
|
}
|
|
|
|
func (s *Scheduler) run() {
|
|
if !s.running.TryLock() {
|
|
fmt.Println("scheduler: previous cycle still running, skipping")
|
|
return
|
|
}
|
|
defer s.running.Unlock()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
fmt.Printf("scheduler: panic recovered: %v\n", r)
|
|
}
|
|
}()
|
|
|
|
if s.generationUseCase == nil {
|
|
return
|
|
}
|
|
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")
|
|
}
|
|
}
|
|
}
|
|
func (s *Scheduler) SetGenerationUseCase(uc generation.UseCase) { s.generationUseCase = uc }
|