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.
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"
|
|
)
|
|
|
|
// runFindATender polls Find a Tender every configured interval until ctx
|
|
// is cancelled.
|
|
func runFindATender(
|
|
ctx context.Context,
|
|
logger *slog.Logger,
|
|
conf *config.JSCSourceFindATender,
|
|
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 {
|
|
return fetchFindATender(ctx, logger, conf, since)
|
|
},
|
|
}
|
|
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,
|
|
) 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 ErrNoNewReleases
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
tagged(logger).Info("fetched opportunities", "count", len(opportunities))
|
|
|
|
return nil
|
|
}
|