sonda/cmd/sondad/scan.go
tgiachi e3843ad7d6 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.
2026-11-12 09:58:45 +01:00

63 lines
2 KiB
Go

package main
import (
"context"
"go.uber.org/zap"
"codeberg.org/squidd/sonda/internal/library"
)
// scanControl lets the HTTP layer start scans that live as long as the
// application, not as long as a single request.
type scanControl struct {
ctx context.Context
m *library.Manager
}
func (c scanControl) Start(full bool) error { return c.m.Start(c.ctx, scanOptions(full)...) }
func (c scanControl) StartLibrary(id int64, full bool) error {
return c.m.StartLibrary(c.ctx, id, scanOptions(full)...)
}
func (c scanControl) Status() library.Status { return c.m.Status() }
func scanOptions(full bool) []library.ScanOption {
if full {
return []library.ScanOption{library.FullRescan}
}
return nil
}
// logScanResults writes one log line per finished scan. File paths are left
// out on purpose: audio paths are private.
func logScanResults(logger *zap.Logger, m *library.Manager) {
m.OnFinish(func(s library.Summary, err error) {
fields := []zap.Field{
zap.Int("found", s.Found), zap.Int("added", s.Added), zap.Int("updated", s.Updated),
zap.Int("unchanged", s.Unchanged), zap.Int("renamed", s.Renamed),
zap.Int("restored", s.Restored), zap.Int("missing", s.Missing),
zap.Int("failed", s.Failed), zap.Int("skipped", s.Skipped),
}
if err != nil {
logger.Warn("library scan finished with errors", append(fields, zap.Error(err))...)
return
}
logger.Info("library scan finished", fields...)
})
}
// warnAboutLibraries logs libraries that look wrong. Rows created by the
// upgrade from the roots-in-config versions skip validation, so a stale root
// can show up as a missing directory or as a library inside another one.
func warnAboutLibraries(ctx context.Context, logger *zap.Logger, libs *library.Libraries) {
problems, err := libs.Problems(ctx)
if err != nil {
logger.Warn("could not check the libraries", zap.Error(err))
return
}
for _, p := range problems {
logger.Warn("library problem: review GET /api/v1/libraries", zap.String("problem", p))
}
}