Files
job-search/internal/fetcher/worker_findatender.go
T
bwilliams b3d20c1db0 Made the fetcher save what it fetches, with fetcher.WithStore
With WithStore, the Find a Tender and mock workers save each run's
opportunities with SaveOpportunities and log what changed; a failed save
fails the run, so the checkpoint stays put and the next run retries.
Without a store nothing is persisted. The mock's opportunities are saved
with Source "FTSMock" rather than the converter's "FindATender".

fetcher service and job-search serve open the store at database.file
(cli.OpenConfiguredStore), or warn and run without one. A missing file
isn't created, and a database needing migration says to run job-search
migrate.

Fixed upsertOpportunity, which never wrote the source column, so every
saved opportunity had an empty Source.
2026-10-16 11:26:16 +01:00

73 lines
1.8 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"
)
// runFindATender polls Find a Tender every configured interval until ctx
// is cancelled, passing what each run fetches to save.
func runFindATender(
ctx context.Context,
logger *slog.Logger,
conf *config.JSCSourceFindATender,
save saveFunc,
since time.Time,
) error {
if conf.APIRoot == "" {
return errors.New("fetcher: sources.find_a_tender.api_root is not set")
}
worker := util.Worker{
Logger: tagged(logger),
Name: "FindATender",
Interval: conf.Interval,
Task: func(ctx context.Context, since time.Time) error {
opportunities, err := fetchFindATender(ctx, logger, conf, since)
if err != nil {
return err
}
return save(ctx, logger, opportunities)
},
}
return worker.Run(ctx, since)
}
// fetchFindATender fetches everything updated since since from Find a
// Tender. Finding nothing returns ErrNoNewReleases, so the worker still moves
// its checkpoint on.
func fetchFindATender(
ctx context.Context,
logger *slog.Logger,
conf *config.JSCSourceFindATender,
since time.Time,
) ([]*types.Opportunity, error) {
opportunities, err := util.AwaitTask(
ctx,
func() ([]*types.Opportunity, error) {
findATender := findatender.New(
conf.APIRoot, findatender.WithLogger(logger),
)
defer findATender.Shutdown()
return findATender.With(ctx).FetchAll(since, true)
},
)
if errors.Is(err, findatender.ErrNoReleases) {
return nil, ErrNoNewReleases
}
if err != nil {
return nil, err
}
tagged(logger).Info("fetched opportunities", "count", len(opportunities))
return opportunities, nil
}