Logging: structured slog messages throughout, and WithLogger options so callers choose the logger
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.
This commit is contained in:
1 parent
0b5fde9563
commit
043e307663
49 files changed
+598
-248
No files matched your search
@@ -14,16 +14,60 @@ import (
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
// logger returns the logger Run logs to: the given one, or slog.Default().
|
||||
// It isn't tagged here; each worker adds its source.
|
||||
func (opts options) logger() *slog.Logger {
|
||||
if opts.log == nil {
|
||||
return slog.Default()
|
||||
}
|
||||
return opts.log
|
||||
}
|
||||
|
||||
// runFunc runs one source's worker until ctx is cancelled, logging to
|
||||
// logger.
|
||||
type runFunc func(
|
||||
ctx context.Context,
|
||||
logger *slog.Logger,
|
||||
conf *config.JobSearchConfig,
|
||||
since time.Time,
|
||||
) error
|
||||
|
||||
// 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.
|
||||
// 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,
|
||||
ctx context.Context,
|
||||
conf *config.JobSearchConfig,
|
||||
since time.Time,
|
||||
opts ...Option,
|
||||
) error {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
var settings options
|
||||
for _, opt := range opts {
|
||||
opt(&settings)
|
||||
}
|
||||
logger := settings.logger().With("component", "fetcher")
|
||||
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
@@ -32,13 +76,13 @@ func Run(
|
||||
|
||||
start := func(
|
||||
name string,
|
||||
run func(context.Context, *config.JobSearchConfig, time.Time) error,
|
||||
run runFunc,
|
||||
) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := run(ctx, conf, since); err != nil {
|
||||
slog.Error("source stopped, shutting down", "source", name, "err", err)
|
||||
if err := run(ctx, logger, conf, since); err != nil {
|
||||
logger.Error("source stopped, shutting down", "source", name, "error", err)
|
||||
mu.Lock()
|
||||
errs = append(errs, fmt.Errorf("%s: %w", name, err))
|
||||
mu.Unlock()
|
||||
|
||||
@@ -62,7 +62,7 @@ func confFor(apiRoot string) *config.JobSearchConfig {
|
||||
}
|
||||
|
||||
func TestFetchFunctions(t *testing.T) {
|
||||
fetchers := map[string]func(context.Context, *config.JobSearchConfig, time.Time) error{
|
||||
fetchers := map[string]runFunc{
|
||||
"FindAOpportunity": fetchFindAOpportunity,
|
||||
"FTSMock": fetchFTSMock,
|
||||
}
|
||||
@@ -91,7 +91,9 @@ func TestFetchFunctions(t *testing.T) {
|
||||
for _, tc := range tests {
|
||||
t.Run(fname+"/"+tc.name, func(t *testing.T) {
|
||||
srv, hits := ftsServer(t, tc.status, tc.body)
|
||||
err := fetch(context.Background(), confFor(srv.URL), time.Now().Add(-time.Hour))
|
||||
err := fetch(
|
||||
context.Background(), slog.Default(), confFor(srv.URL), time.Now().Add(-time.Hour),
|
||||
)
|
||||
if !tc.check(err) {
|
||||
t.Fatalf("err = %v, want %s", err, tc.wantErr)
|
||||
}
|
||||
@@ -110,11 +112,11 @@ func TestFetchUsesConfiguredAPIRoot(t *testing.T) {
|
||||
conf.Sources.FindATender.APIRoot = fat.URL
|
||||
conf.Sources.FTSMock.APIRoot = mock.URL
|
||||
|
||||
fetchFindAOpportunity(context.Background(), conf, time.Time{})
|
||||
fetchFindAOpportunity(context.Background(), slog.Default(), conf, time.Time{})
|
||||
if fatHits.Load() != 1 || mockHits.Load() != 0 {
|
||||
t.Fatalf("FindAOpportunity hits: fat=%d mock=%d", fatHits.Load(), mockHits.Load())
|
||||
}
|
||||
fetchFTSMock(context.Background(), conf, time.Time{})
|
||||
fetchFTSMock(context.Background(), slog.Default(), conf, time.Time{})
|
||||
if fatHits.Load() != 1 || mockHits.Load() != 1 {
|
||||
t.Fatalf("FTSMock hits: fat=%d mock=%d", fatHits.Load(), mockHits.Load())
|
||||
}
|
||||
@@ -126,7 +128,8 @@ func TestFetchCancelled(t *testing.T) {
|
||||
// Unroutable root: the request should never be waited on.
|
||||
conf := confFor("http://192.0.2.1:9")
|
||||
start := time.Now()
|
||||
if err := fetchFTSMock(ctx, conf, time.Time{}); !errors.Is(err, context.Canceled) {
|
||||
err := fetchFTSMock(ctx, slog.Default(), conf, time.Time{})
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("err = %v, want context.Canceled", err)
|
||||
}
|
||||
if d := time.Since(start); d > time.Second {
|
||||
@@ -163,7 +166,7 @@ func TestRunSleeperShutdown(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
runSleeper(ctx, conf, time.Time{})
|
||||
runSleeper(ctx, slog.Default(), conf, time.Time{})
|
||||
close(done)
|
||||
}()
|
||||
time.Sleep(10 * time.Millisecond) // let the first sleep start
|
||||
@@ -184,13 +187,13 @@ func TestRunSleeperShutdown(t *testing.T) {
|
||||
|
||||
func TestRunWorkersRejectZeroInterval(t *testing.T) {
|
||||
conf := &config.JobSearchConfig{}
|
||||
runs := map[string]func(context.Context, *config.JobSearchConfig, time.Time) error{
|
||||
runs := map[string]runFunc{
|
||||
"FindAOpportunity": runFindAOpportunity,
|
||||
"FTSMock": runFTSMock,
|
||||
"Sleeper": runSleeper,
|
||||
}
|
||||
for name, run := range runs {
|
||||
if err := run(context.Background(), conf, time.Time{}); err == nil {
|
||||
if err := run(context.Background(), slog.Default(), conf, time.Time{}); err == nil {
|
||||
t.Errorf("%s: expected an error for a zero interval", name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package fetcher
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
@@ -16,13 +15,14 @@ import (
|
||||
// runFindAOpportunity polls Find a Tender every configured interval until ctx
|
||||
// is cancelled.
|
||||
func runFindAOpportunity(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
ctx context.Context, logger *slog.Logger, conf *config.JobSearchConfig, since time.Time,
|
||||
) error {
|
||||
w := util.Worker{
|
||||
Logger: logger,
|
||||
Name: "FindAOpportunity",
|
||||
Interval: conf.Sources.FindATender.Interval,
|
||||
Task: func(ctx context.Context, since time.Time) error {
|
||||
return fetchFindAOpportunity(ctx, conf, since)
|
||||
return fetchFindAOpportunity(ctx, logger, conf, since)
|
||||
},
|
||||
}
|
||||
return w.Run(ctx, since)
|
||||
@@ -32,10 +32,10 @@ func runFindAOpportunity(
|
||||
// Tender. Finding nothing returns ErrNoNewReleases, so the worker still moves
|
||||
// its checkpoint on.
|
||||
func fetchFindAOpportunity(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
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.FindATender.APIRoot)
|
||||
ft := findatender.New(conf, conf.Sources.FindATender.APIRoot, findatender.WithLogger(logger))
|
||||
defer ft.Shutdown()
|
||||
return ft.With(ctx).FetchAll(since, true)
|
||||
})
|
||||
@@ -46,7 +46,7 @@ func fetchFindAOpportunity(
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info(fmt.Sprintf("Found %d opportunities", len(opportunities)))
|
||||
logger.Info("fetched opportunities", "count", len(opportunities))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package fetcher
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
@@ -16,13 +15,14 @@ import (
|
||||
// runFTSMock polls the mock Find a Tender server every configured interval
|
||||
// until ctx is cancelled.
|
||||
func runFTSMock(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
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, conf, since)
|
||||
return fetchFTSMock(ctx, logger, conf, since)
|
||||
},
|
||||
}
|
||||
return w.Run(ctx, since)
|
||||
@@ -32,10 +32,10 @@ func runFTSMock(
|
||||
// Tender server. Finding nothing returns ErrNoNewReleases, so the worker still
|
||||
// moves its checkpoint on.
|
||||
func fetchFTSMock(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
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)
|
||||
ft := findatender.New(conf, conf.Sources.FTSMock.APIRoot, findatender.WithLogger(logger))
|
||||
defer ft.Shutdown()
|
||||
return ft.With(ctx).FetchAll(since, true)
|
||||
})
|
||||
@@ -46,7 +46,7 @@ func fetchFTSMock(
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info(fmt.Sprintf("Found %d opportunities", len(opportunities)))
|
||||
logger.Info("fetched opportunities", "count", len(opportunities))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package fetcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"git.avsdev.uk/Go/job-search/internal/config"
|
||||
@@ -14,10 +15,11 @@ const sleepDuration = 60 * time.Second
|
||||
// runSleeper runs the sleeper test source until ctx is cancelled: every
|
||||
// configured interval it simulates work by sleeping for sleepDuration.
|
||||
func runSleeper(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
ctx context.Context, logger *slog.Logger, conf *config.JobSearchConfig, since time.Time,
|
||||
) error {
|
||||
blockShutdown := conf.Sources.Sleeper.BlockShutdown
|
||||
w := util.Worker{
|
||||
Logger: logger,
|
||||
Name: "Sleeper",
|
||||
Interval: conf.Sources.Sleeper.Interval,
|
||||
Task: func(taskCtx context.Context, since time.Time) error {
|
||||
|
||||
Reference in new issue
Block a user