sonda/internal/about/job.go
tgiachi 9323702b1e fix(jobs): a stop or a panic never wedges a job, a bad download never replaces a good picture
- A panic while counting no longer leaves the job marked as running; a panic in the
  work of one item is a failure of that item, and a panic in the list ends the job
  with a reason.
- A stop while the items are counted is recorded as a stop instead of a 500, and a
  count that fails leaves the status as the previous job left it.
- An artist that lost its MusicBrainz id since it was listed is skipped by the
  pictures job, as the spec says, instead of counting as a failure.
- A picture that cannot be used now (refused download, bytes that are not an image)
  is a failure when a good picture is stored, so a refresh never throws it away.
- The outcomes the artist jobs report share their constants in the jobs package.
- The spec no longer calls each job "the one request" that goes through the library.
- The test that waited with a sleep for the count now waits for a signal.
2026-11-12 10:09:46 +01:00

60 lines
2.1 KiB
Go

package about
import (
"context"
"errors"
"time"
"codeberg.org/squidd/sonda/internal/discovery"
"codeberg.org/squidd/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)
}