53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
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"
|
|
)
|
|
|
|
// runFindAOpportunity polls Find a Tender every configured interval until ctx
|
|
// is cancelled.
|
|
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)
|
|
}
|
|
|
|
// fetchFindAOpportunity fetches everything updated since since from Find a
|
|
// Tender. Finding nothing returns ErrNoNewReleases, so the worker still moves
|
|
// its checkpoint on.
|
|
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
|
|
}
|