package fetcher import ( "context" "errors" "fmt" "log/slog" "time" "git.avsdev.uk/Go/job-search/internal/config" "git.avsdev.uk/Go/job-search/internal/findATender" "git.avsdev.uk/Go/job-search/internal/types" "git.avsdev.uk/Go/job-search/internal/util" ) // runFTSMock polls the mock Find a Tender server every configured interval // until ctx is cancelled. func runFTSMock( ctx context.Context, conf *config.JobSearchConfig, since time.Time, ) error { w := util.Worker{ Name: "FTSMock", Interval: conf.Sources.FTSMock.Interval, Task: func(ctx context.Context, since time.Time) error { return fetchFTSMock(ctx, conf, since) }, } return w.Run(ctx, since) } // fetchFTSMock fetches everything updated since since from the mock Find a // Tender server. Finding nothing returns ErrNoNewReleases, so the worker still // moves its checkpoint on. func fetchFTSMock( ctx context.Context, conf *config.JobSearchConfig, since time.Time, ) error { opportunities, err := util.AwaitTask(ctx, func() ([]*types.Opportunity, error) { ft := findATender.New(conf, conf.Sources.FTSMock.APIRoot) defer ft.Shutdown() return ft.With(ctx).FetchAll(since, true) }) if errors.Is(err, findATender.ErrNoReleases) { return ErrNoNewReleases } if err != nil { return err } slog.Info(fmt.Sprintf("Found %d opportunities", len(opportunities))) return nil }