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.
This commit is contained in:
1 parent
eba6af680f
commit
b3d20c1db0
10 files changed
+407
-38
No files matched your search
@@ -12,12 +12,16 @@ import (
|
||||
"git.avsdev.uk/Go/job-search/internal/util"
|
||||
)
|
||||
|
||||
// ftsMockSource is the Source of the opportunities the mock source fetches.
|
||||
const ftsMockSource = "FTSMock"
|
||||
|
||||
// runFTSMock polls the mock Find a Tender server every configured interval
|
||||
// until ctx is cancelled.
|
||||
// until ctx is cancelled, passing what each run fetches to save.
|
||||
func runFTSMock(
|
||||
ctx context.Context,
|
||||
logger *slog.Logger,
|
||||
conf *config.JSCSourceFTSMock,
|
||||
save saveFunc,
|
||||
since time.Time,
|
||||
) error {
|
||||
if conf.APIRoot == "" {
|
||||
@@ -26,24 +30,30 @@ func runFTSMock(
|
||||
|
||||
worker := util.Worker{
|
||||
Logger: tagged(logger),
|
||||
Name: "FTSMock",
|
||||
Name: ftsMockSource,
|
||||
Interval: conf.Interval,
|
||||
Task: func(ctx context.Context, since time.Time) error {
|
||||
return fetchFTSMock(ctx, logger, conf, since)
|
||||
opportunities, err := fetchFTSMock(ctx, logger, conf, since)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return save(ctx, logger, opportunities)
|
||||
},
|
||||
}
|
||||
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.
|
||||
// Tender server. The mock serves the real API's format, so the converter
|
||||
// marks the opportunities as Find a Tender's; their Source is set to
|
||||
// ftsMockSource here instead. Finding nothing returns ErrNoNewReleases, so
|
||||
// the worker still moves its checkpoint on.
|
||||
func fetchFTSMock(
|
||||
ctx context.Context,
|
||||
logger *slog.Logger,
|
||||
conf *config.JSCSourceFTSMock,
|
||||
since time.Time,
|
||||
) error {
|
||||
) ([]*types.Opportunity, error) {
|
||||
opportunities, err := util.AwaitTask(
|
||||
ctx,
|
||||
func() ([]*types.Opportunity, error) {
|
||||
@@ -55,13 +65,17 @@ func fetchFTSMock(
|
||||
},
|
||||
)
|
||||
if errors.Is(err, findatender.ErrNoReleases) {
|
||||
return ErrNoNewReleases
|
||||
return nil, ErrNoNewReleases
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, opportunity := range opportunities {
|
||||
opportunity.Source = ftsMockSource
|
||||
}
|
||||
|
||||
tagged(logger).Info("fetched opportunities", "count", len(opportunities))
|
||||
|
||||
return nil
|
||||
return opportunities, nil
|
||||
}
|
||||
Reference in new issue
Block a user