Made the fetcher a Fetcher type with per-source config, and ran it in job-search serve

fetcher.Run(ctx, conf, since) becomes New(&conf.Sources, opts...) and
Run(ctx, since). Each worker now takes only its own source's config, and
Run starts each one through a closure that binds it. Cancelling ctx stops
the workers through the Fetcher's background context, and Run always waits
for every worker before returning their joined errors.

The findatender client no longer holds the configuration: New takes only
the API root, ReloadConfig is gone, and request tracing follows
logging.IsDebugEnabled(). The FindAOpportunity worker is now FindATender.

job-search serve runs the fetcher alongside the HTTP server. A source that
fails stops the server, and the server stopping stops the sources; with no
sources enabled the server keeps running. fetcher service and fetcher
fts-once use the new APIs.
This commit is contained in:
bwilliams committed 2026-10-16 10:44:38 +01:00
1 parent f0c171c8a8
commit 18db462d89
12 files changed
+269 -214

No files matched your search

+68 -45
View File
@@ -16,30 +16,25 @@ import (
// to fetch; it wraps util.ErrWorkerNoWork.
var ErrNoNewReleases = fmt.Errorf("%w: no new releases", util.ErrWorkerNoWork)
// Option configures Run.
type Option func(*options)
// options holds the settings Options change.
type options struct {
log *slog.Logger
}
// Option configures a Fetcher.
type Option func(*Fetcher)
// WithLogger makes Run, its workers and their clients log to logger instead
// of slog.Default().
func WithLogger(logger *slog.Logger) Option {
return func(opts *options) {
opts.log = logger
return func(f *Fetcher) {
f.log = logger
}
}
// logger returns the logger Run logs to: the given one, or slog.Default().
// It isn't tagged here, as it is also given to the clients the workers
// create, which add their own component; see tagged.
func (opts options) logger() *slog.Logger {
if opts.log == nil {
func (f *Fetcher) logger() *slog.Logger {
if f.log == nil {
return slog.Default()
}
return opts.log
return f.log
}
// tagged returns logger tagged with the fetcher's component, for the lines
@@ -48,35 +43,52 @@ func tagged(logger *slog.Logger) *slog.Logger {
return logger.With("component", "fetcher")
}
// runFunc runs one source's worker until ctx is cancelled. logger is
// untagged: the worker tags its own lines with tagged, and passes logger as
// it is to any client it creates.
type runFunc func(
ctx context.Context,
logger *slog.Logger,
conf *config.JobSearchConfig,
since time.Time,
) error
// Fetcher runs a worker for each enabled source. Create it with New and
// start it with Run.
type Fetcher struct {
conf *config.JSCSources
log *slog.Logger
started time.Time
// backgroundCtx is the context the workers run under. backgroundCancel
// stops them: Run calls it when its ctx is cancelled, when a worker
// fails, and as it returns.
backgroundCtx context.Context
backgroundCancel context.CancelFunc
}
// New returns a Fetcher for the sources conf configures. It logs to
// slog.Default() unless given WithLogger.
func New(conf *config.JSCSources, opts ...Option) *Fetcher {
fetcher := &Fetcher{
conf: conf,
started: time.Now(),
}
for _, option := range opts {
option(fetcher)
}
fetcher.backgroundCtx, fetcher.backgroundCancel = context.WithCancel(
context.Background(),
)
return fetcher
}
// Run starts a worker for each enabled source and blocks until every worker
// has stopped. Workers stop when ctx is cancelled, or when any worker stops
// with an error, which starts a shutdown of all of them. The errors are
// logged as they happen and returned joined; nil means a clean shutdown. It
// logs to slog.Default() unless given WithLogger.
func Run(
ctx context.Context,
conf *config.JobSearchConfig,
since time.Time,
opts ...Option,
) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// logged as they happen and returned joined; nil means a clean shutdown. Run
// cancels the Fetcher's background context as it returns, so it can only be
// called once.
func (f *Fetcher) Run(ctx context.Context, since time.Time) error {
defer f.backgroundCancel()
var settings options
for _, opt := range opts {
opt(&settings)
}
logger := settings.logger()
// Cancelling ctx stops the workers, through the background context.
stopAfter := context.AfterFunc(ctx, f.backgroundCancel)
defer stopAfter()
logger := f.logger()
var (
wg sync.WaitGroup
@@ -84,11 +96,14 @@ func Run(
errs []error
)
start := func(name string, run runFunc) {
// start runs one source's worker until f.backgroundCtx is cancelled. Each
// worker is given logger untagged: it tags its own lines with tagged, and
// passes logger as it is to any client it creates.
start := func(name string, run func(ctx context.Context) error) {
wg.Add(1)
go func() {
defer wg.Done()
if err := run(ctx, logger, conf, since); err != nil {
if err := run(f.backgroundCtx); err != nil {
tagged(logger).Error(
"source stopped, shutting down",
"source", name,
@@ -97,21 +112,29 @@ func Run(
mu.Lock()
errs = append(errs, fmt.Errorf("%s: %w", name, err))
mu.Unlock()
cancel()
f.backgroundCancel()
}
}()
}
if conf.Sources.FindATender.Enabled {
start("FindATender", runFindAOpportunity)
if f.conf.FindATender.Enabled {
start("FindATender", func(ctx context.Context) error {
return runFindATender(ctx, logger, &f.conf.FindATender, since)
})
}
if conf.Sources.FTSMock.Enabled {
start("FTSMock", runFTSMock)
if f.conf.FTSMock.Enabled {
start("FTSMock", func(ctx context.Context) error {
return runFTSMock(ctx, logger, &f.conf.FTSMock, since)
})
}
if conf.Sources.Sleeper.Enabled {
start("Sleeper", runSleeper)
if f.conf.Sleeper.Enabled {
start("Sleeper", func(ctx context.Context) error {
return runSleeper(ctx, logger, &f.conf.Sleeper, since)
})
}
// Every worker has stopped once Wait returns, so errs is no longer
// written.
wg.Wait()
return errors.Join(errs...)