feat(jobs): background jobs for artist photos and descriptions

Add one engine, internal/jobs, that runs the jobs which go through the whole
library: start, stop, progress, one at a time, give up after five failures in
a row, end at once when a provider is off.

- The covers job now runs on the engine; its API is unchanged.
- POST/GET/DELETE /library/artist-images looks for the picture of every
  artist of the library that has a MusicBrainz id.
- POST/GET/DELETE /library/descriptions does the same for the Wikipedia
  description.
- ?refresh=true searches everything again and never replaces a stored result
  by a failure; artists without an id are never touched, nothing is looked up
  by name.
- images.Service.EnsureArtistImage and about.Service.Ensure do the work for one
  artist; the lists come from ImageRepo and DiscoveryRepo.
- jobs.Runner binds a job to the application context, replacing the two
  hand-written controls.
- OpenAPI, contract test and README describe the new endpoints and say that
  they are bulk requests.
This commit is contained in:
tgiachi 2026-11-12 09:58:45 +01:00
commit e3843ad7d6
2 changed files with 2124 additions and 719 deletions

View file

@ -23,6 +23,7 @@ import (
"codeberg.org/squidd/sonda/internal/discovery"
"codeberg.org/squidd/sonda/internal/feedback"
"codeberg.org/squidd/sonda/internal/images"
"codeberg.org/squidd/sonda/internal/jobs"
"codeberg.org/squidd/sonda/internal/library"
"codeberg.org/squidd/sonda/internal/listening"
"codeberg.org/squidd/sonda/internal/providers"
@ -224,6 +225,8 @@ func run() error {
}
return discoveryRepo.Purge(ctx, name)
})
descriptions := about.NewService(discoveryRepo, about.WikipediaSource{MusicBrainz: musicbrainzClient, Wikipedia: wikipediaClient},
providerSettings, cfg.Wikipedia.Languages, logger)
discoveries := discovery.NewService(discoveryRepo, lastfmClient, providerSettings, logger)
tags := discovery.NewTagService(discoveryRepo, map[string]discovery.TagSource{
providers.LastFM: discovery.LastfmTags{Client: lastfmClient},
@ -240,26 +243,27 @@ func run() error {
}
router := httpapi.NewRouter(httpapi.Deps{
Logger: logger,
Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, cfg.Auth.RefreshTTL()),
Tokens: tokens,
Listening: listening.NewService(storage.NewListeningRepo(db), cfg.Listening.MinListen()),
Scan: scanControl{ctx: ctx, m: scans},
Libraries: libraries,
Catalog: catalog.NewService(storage.NewCatalogRepo(db)),
Providers: providerSettings,
Discovery: discoveries,
Tags: tags,
Taste: tasteService,
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),
Settings: userSettings,
Streams: streaming.NewService(libraryRepo),
Logger: logger,
Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, cfg.Auth.RefreshTTL()),
Tokens: tokens,
Listening: listening.NewService(storage.NewListeningRepo(db), cfg.Listening.MinListen()),
Scan: scanControl{ctx: ctx, m: scans},
Libraries: libraries,
Catalog: catalog.NewService(storage.NewCatalogRepo(db)),
Providers: providerSettings,
Discovery: discoveries,
Tags: tags,
Taste: tasteService,
Discover: discover.NewService(tasteService, discoveries, tags, tasteRepo, userSettings, feedbacks, weights),
Feedback: feedbacks,
Images: pictures,
CoversJob: jobs.Runner{Ctx: ctx, Job: images.NewCoversJob(pictures, imageRepo)},
ArtistImagesJob: jobs.Runner{Ctx: ctx, Job: images.NewArtistImagesJob(pictures, imageRepo)},
DescriptionsJob: jobs.Runner{Ctx: ctx, Job: about.NewJob(descriptions, discoveryRepo)},
ArtistImages: pictures,
About: descriptions,
Settings: userSettings,
Streams: streaming.NewService(libraryRepo),
})
// No WriteTimeout: audio streaming (later) needs long responses.

View file

@ -5,7 +5,6 @@ import (
"go.uber.org/zap"
"codeberg.org/squidd/sonda/internal/images"
"codeberg.org/squidd/sonda/internal/library"
)
@ -62,17 +61,3 @@ 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() }