sonda/internal/about/job.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

60 lines
2.1 KiB
Go

package about
import (
"context"
"errors"
"time"
"git.kmalloc.org/squid/sonda/internal/discovery"
"git.kmalloc.org/squid/sonda/internal/jobs"
)
// Outcomes of the descriptions job, as the summary counts them.
const (
OutcomeFound = jobs.Found // a description was found
OutcomeNone = jobs.None // Wikipedia has none for the artist
)
// Lister lists the artists that need their description looked for, so the job does not read
// them all at once.
type Lister interface {
// DescriptionsToBuild returns up to limit ids greater than after, in order, of the artists
// of the library that have a MusicBrainz id and no answer about their description that is
// still good (or all of them with refresh). Artists with no id are never listed: nothing
// is looked up by name.
DescriptionsToBuild(ctx context.Context, after int64, limit int, refresh bool, now time.Time) ([]int64, error)
// CountDescriptionsToBuild says how many there are.
CountDescriptionsToBuild(ctx context.Context, refresh bool, now time.Time) (int, error)
}
// NewJob builds the job that looks for the description of every artist in the background. It
// does for each artist what asking for the description does, and leaves alone those whose
// answer is still good. It ends at once when a provider it needs is off.
func NewJob(svc *Service, artists Lister) *jobs.Job {
return jobs.New(jobs.Task{
Noun: "artists",
Count: artists.CountDescriptionsToBuild,
List: artists.DescriptionsToBuild,
Do: func(ctx context.Context, id int64, refresh bool) (string, error) {
res, err := svc.Ensure(ctx, id, refresh)
switch {
case errors.Is(err, discovery.ErrNotFound):
return "", jobs.ErrSkip
case err != nil:
return "", err
}
switch res.Status {
case discovery.StatusDisabled:
return "", jobs.Off("the wikipedia or the musicbrainz provider is off")
case discovery.StatusNoID:
return "", jobs.ErrSkip // the artist lost its id since it was listed
case discovery.StatusError:
return "", errors.New("Wikipedia could not be reached")
}
if res.Description == nil {
return OutcomeNone, nil
}
return OutcomeFound, nil
},
}, svc.now)
}