feat(images): a background job that builds all the covers

- POST /library/covers (admin) starts, in the background and one at a time like a scan,
  a job that builds the cover of every album that has something to play and no cover that
  is still good; GET /library/covers shows the progress and how the last job ended;
  DELETE /library/covers stops it. ?refresh=true searches every album again.
- For each album it does what opening its cover does: the user's own files first, then the
  Cover Art Archive if the provider is on. It gives up, and says why, when the provider
  fails for five albums in a row; an album that vanished is skipped, not a failure.
- This is the one request that goes through the whole library, and the README and the
  provider text say so: with the archive on it sends the release group id of each album with
  no picture in its files, one after the other, spaced out.
- NewRouter now refuses to build when a service was not wired, naming it, instead of
  panicking at the first request (the wiring of this job in main had been missed, which no
  test could see).
- The outbound spacing test measures where the request leaves the client, so a busy
  machine cannot make it fail.
- Spec, contract and HTTP tests, README updated.

Closes #45
This commit is contained in:
tgiachi 2026-11-12 09:34:27 +01:00
commit 548ee03d83
2 changed files with 1155 additions and 24 deletions

View file

@ -254,6 +254,7 @@ func run() error {
Discover: discover.NewService(tasteService, discoveries, tags, tasteRepo, userSettings, feedbacks, weights),
Feedback: feedbacks,
Images: pictures,
CoversJob: coversControl{ctx: ctx, job: images.NewJob(pictures, imageRepo)},
ArtistImages: pictures,
About: about.NewService(discoveryRepo, about.WikipediaSource{MusicBrainz: musicbrainzClient, Wikipedia: wikipediaClient},
providerSettings, cfg.Wikipedia.Languages, logger),

View file

@ -5,6 +5,7 @@ import (
"go.uber.org/zap"
"codeberg.org/squidd/sonda/internal/images"
"codeberg.org/squidd/sonda/internal/library"
)
@ -61,3 +62,17 @@ func warnAboutLibraries(ctx context.Context, logger *zap.Logger, libs *library.L
logger.Warn("library problem: review GET /api/v1/libraries", zap.String("problem", p))
}
}
// coversControl lets the HTTP layer start the covers job on a context that lives as long as
// the application, not as long as a single request.
type coversControl struct {
ctx context.Context
job *images.Job
}
func (c coversControl) Start(refresh bool) error {
return c.job.Start(c.ctx, images.JobOptions{Refresh: refresh})
}
func (c coversControl) Stop() bool { return c.job.Stop() }
func (c coversControl) Status() images.JobStatus { return c.job.Status() }