package acquire import ( "context" "fmt" "github.com/jackc/pgx/v5/pgxpool" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/release" ) // Build makes a Service from the settings as they stand: every enabled // search source behind one paced Fanout, the first enabled download client, // and the quality profile. Built afresh for each pass and each request, so a // changed setting takes effect on the next one. func Build(ctx context.Context, pool *pgxpool.Pool) (*Service, dbq.AcquisitionSetting, error) { q := dbq.New(pool) settings, err := q.GetAcquisitionSettings(ctx) if err != nil { return nil, settings, fmt.Errorf("acquire: read settings: %w", err) } sources, err := Sources(ctx, pool) if err != nil { return nil, settings, fmt.Errorf("acquire: read search sources: %w", err) } clients, err := q.ListUsableAcquisitionServices(ctx, JobDownload) if err != nil { return nil, settings, fmt.Errorf("acquire: read download clients: %w", err) } s := &Service{Profile: release.NewProfile(settings.AllowedQualities), Memory: NewMemory(pool)} fan := &Fanout{Sources: sources, Pace: SearchPace} if len(sources) > 0 { s.Search = fan } if len(clients) > 0 { row := clients[0] client, err := DownloadClient(row) if err != nil { return nil, settings, fmt.Errorf("acquire: download client %s: %w", row.Code, err) } s.Grabber = &Downloader{Client: client, Code: row.Code, Landing: row.LocalPath, Fetch: fan} } return s, settings, nil }