An enabled Find a Tender or mock source with no api_root used to start and then fail on every fetch. Its worker now returns an error naming the missing key before it starts, which stops Run. "fetch once" checks sources.find_a_tender.api_root the same way. TestRunMissingAPIRoot covers both sources, and TestRunLogsSourceErrors sets an api_root so it still tests the zero interval. The section docs and internal/config/README.md say what an empty api_root means.
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package fetcher
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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,
|
|
logger *slog.Logger,
|
|
conf *config.JobSearchConfig,
|
|
since time.Time,
|
|
) error {
|
|
if conf.Sources.FTSMock.APIRoot == "" {
|
|
return errors.New("fetcher: sources.fts_mock.api_root is not set")
|
|
}
|
|
|
|
worker := util.Worker{
|
|
Logger: tagged(logger),
|
|
Name: "FTSMock",
|
|
Interval: conf.Sources.FTSMock.Interval,
|
|
Task: func(ctx context.Context, since time.Time) error {
|
|
return fetchFTSMock(ctx, logger, conf, since)
|
|
},
|
|
}
|
|
return worker.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,
|
|
logger *slog.Logger,
|
|
conf *config.JobSearchConfig,
|
|
since time.Time,
|
|
) error {
|
|
opportunities, err := util.AwaitTask(
|
|
ctx,
|
|
func() ([]*types.Opportunity, error) {
|
|
findATender := findatender.New(
|
|
conf, conf.Sources.FTSMock.APIRoot, findatender.WithLogger(logger),
|
|
)
|
|
defer findATender.Shutdown()
|
|
return findATender.With(ctx).FetchAll(since, true)
|
|
},
|
|
)
|
|
if errors.Is(err, findatender.ErrNoReleases) {
|
|
return ErrNoNewReleases
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
tagged(logger).Info("fetched opportunities", "count", len(opportunities))
|
|
|
|
return nil
|
|
}
|