Log messages are lower-case event names with their details as key/value
attributes ("error" for errors), in place of formatted strings.
Loggable package take a WithLogger option as part of their "New*" call,
defaulting to slog.Default(). Each tags its lines with its `component`.
Some debug loggers print directly rather than using slog when in
terminal logging mode.
53 lines
1.4 KiB
Go
53 lines
1.4 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 {
|
|
w := util.Worker{
|
|
Logger: logger,
|
|
Name: "FTSMock",
|
|
Interval: conf.Sources.FTSMock.Interval,
|
|
Task: func(ctx context.Context, since time.Time) error {
|
|
return fetchFTSMock(ctx, logger, 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, logger *slog.Logger, conf *config.JobSearchConfig, since time.Time,
|
|
) error {
|
|
opportunities, err := util.AwaitTask(ctx, func() ([]*types.Opportunity, error) {
|
|
ft := findatender.New(conf, conf.Sources.FTSMock.APIRoot, findatender.WithLogger(logger))
|
|
defer ft.Shutdown()
|
|
return ft.With(ctx).FetchAll(since, true)
|
|
})
|
|
if errors.Is(err, findatender.ErrNoReleases) {
|
|
return ErrNoNewReleases
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
logger.Info("fetched opportunities", "count", len(opportunities))
|
|
|
|
return nil
|
|
}
|