Added fetch service and a mock Find a Tender server
Fetching: - Add `fetch service`, which runs a worker per enabled source until SIGINT/SIGTERM (internal/fetcher) - Add util.Worker: runs a task on an interval with a since-checkpoint that only advances on success, and a shutdown timeout for in-flight work; plus AwaitTask/AwaitTaskE for calls that ignore cancellation - Add fts_mock and sleeper sources - Sleeper: sleeps periodically to emulate work being done. Can be instructed to block the shutdown signal or handle it cleanly Mock FTS server (cmd/mock-fts, internal/mock/findATender): - Serves GET /api/1.0/ocdsReleasePackages with FTS paging, query validation, UK local time handling and per-client rate limiting - Generates fictional releases from an embedded catalog, moving procedures through planning, tender and award, and keeps them in a JSON Lines store that survives restarts - Add logging.AccessLog for combined/common access logs
This commit is contained in:
1 parent
dcb714dc19
commit
5cb3601e1f
23 files changed
+97717
-3
No files matched your search
@@ -0,0 +1,45 @@
|
||||
package fetcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.avsdev.uk/Go/job-search/internal/config"
|
||||
"git.avsdev.uk/Go/job-search/internal/util"
|
||||
)
|
||||
|
||||
var ErrNoNewReleases = fmt.Errorf("%w: no new releases", util.ErrWorkerNoWork)
|
||||
|
||||
func Run(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
) {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
start := func(
|
||||
name string,
|
||||
run func(context.Context, *config.JobSearchConfig, time.Time) error,
|
||||
) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := run(ctx, conf, since); err != nil {
|
||||
slog.Error("source stopped", "source", name, "err", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
if conf.Sources.FindATender.Enabled {
|
||||
start("FindATender", runFindAOpportunity)
|
||||
}
|
||||
if conf.Sources.FTSMock.Enabled {
|
||||
start("FTSMock", runFTSMock)
|
||||
}
|
||||
if conf.Sources.Sleeper.Enabled {
|
||||
start("Sleeper", runSleeper)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package fetcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"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"
|
||||
)
|
||||
|
||||
func runFindAOpportunity(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
) error {
|
||||
w := util.Worker{
|
||||
Name: "FindAOpportunity",
|
||||
Interval: conf.Sources.FindATender.Interval,
|
||||
Task: func(ctx context.Context, since time.Time) error {
|
||||
return fetchFindAOpportunity(ctx, conf, since)
|
||||
},
|
||||
}
|
||||
return w.Run(ctx, since)
|
||||
}
|
||||
|
||||
func fetchFindAOpportunity(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
) error {
|
||||
opportunities, err := util.AwaitTask(ctx, func() ([]*types.Opportunity, error) {
|
||||
ft := findATender.New(conf, conf.Sources.FindATender.APIRoot)
|
||||
defer ft.Shutdown()
|
||||
return ft.With(ctx).FetchAll(since, true)
|
||||
})
|
||||
if errors.Is(err, findATender.ErrNoReleases) {
|
||||
return ErrNoNewReleases
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info(fmt.Sprintf("Found %d opportunities", len(opportunities)))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package fetcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"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"
|
||||
)
|
||||
|
||||
func runFTSMock(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
) error {
|
||||
w := util.Worker{
|
||||
Name: "FTSMock",
|
||||
Interval: conf.Sources.FTSMock.Interval,
|
||||
Task: func(ctx context.Context, since time.Time) error {
|
||||
return fetchFTSMock(ctx, conf, since)
|
||||
},
|
||||
}
|
||||
return w.Run(ctx, since)
|
||||
}
|
||||
|
||||
func fetchFTSMock(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
) error {
|
||||
opportunities, err := util.AwaitTask(ctx, func() ([]*types.Opportunity, error) {
|
||||
ft := findATender.New(conf, conf.Sources.FTSMock.APIRoot)
|
||||
defer ft.Shutdown()
|
||||
return ft.With(ctx).FetchAll(since, true)
|
||||
})
|
||||
if errors.Is(err, findATender.ErrNoReleases) {
|
||||
return ErrNoNewReleases
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info(fmt.Sprintf("Found %d opportunities", len(opportunities)))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package fetcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.avsdev.uk/Go/job-search/internal/config"
|
||||
"git.avsdev.uk/Go/job-search/internal/util"
|
||||
)
|
||||
|
||||
// sleepDuration is how long the sleeper's simulated work takes.
|
||||
const sleepDuration = 60 * time.Second
|
||||
|
||||
// Test source that just sleeps for 60 seconds to simulate doing work
|
||||
func runSleeper(
|
||||
ctx context.Context, conf *config.JobSearchConfig, since time.Time,
|
||||
) error {
|
||||
blockShutdown := conf.Sources.Sleeper.BlockShutdown
|
||||
w := util.Worker{
|
||||
Name: "Sleeper",
|
||||
Interval: conf.Sources.Sleeper.Interval,
|
||||
Task: func(taskCtx context.Context, since time.Time) error {
|
||||
if blockShutdown {
|
||||
return sleep(taskCtx, true, sleepDuration)
|
||||
} else {
|
||||
return sleep(ctx, false, sleepDuration)
|
||||
}
|
||||
},
|
||||
}
|
||||
return w.Run(ctx, since)
|
||||
}
|
||||
|
||||
func sleep(
|
||||
ctx context.Context, blockShutdown bool, duration time.Duration,
|
||||
) error {
|
||||
return util.AwaitTaskE(ctx, func() error {
|
||||
if blockShutdown {
|
||||
time.Sleep(duration)
|
||||
return nil
|
||||
}
|
||||
timer := time.NewTimer(duration)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-timer.C:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user