sonda/internal/images/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

52 lines
1.8 KiB
Go

package images
import (
"context"
"errors"
"time"
"git.kmalloc.org/squid/sonda/internal/jobs"
)
// Outcomes of the covers job, as the summary counts them.
const (
OutcomeFromFiles = "from_files" // a cover found in the user's own files
OutcomeFromArchive = "from_archive" // a cover from the Cover Art Archive
OutcomeNone = jobs.None // no cover anywhere
)
// AlbumLister lists the albums that need a cover built, so the job does not read them all at once.
type AlbumLister interface {
// AlbumsToBuild returns up to limit ids greater than after, in order, of the albums that have
// something to play and no cover that is still good (or all of them with refresh).
AlbumsToBuild(ctx context.Context, after int64, limit int, refresh bool, now time.Time) ([]int64, error)
// CountAlbumsToBuild says how many there are.
CountAlbumsToBuild(ctx context.Context, refresh bool, now time.Time) (int, error)
}
// NewCoversJob builds the job that builds the cover of every album in the background. For each
// album it does what opening its cover does (the user's own files first, then the provider of
// covers if one is on), and skips the albums that already have a good one.
func NewCoversJob(svc *Service, albums AlbumLister) *jobs.Job {
return jobs.New(jobs.Task{
Noun: "albums",
Count: albums.CountAlbumsToBuild,
List: albums.AlbumsToBuild,
Do: func(ctx context.Context, id int64, refresh bool) (string, error) {
source, err := svc.EnsureAlbumCover(ctx, id, refresh)
switch {
case errors.Is(err, ErrAlbumNotFound):
return "", jobs.ErrSkip
case err != nil:
return "", err
}
switch source {
case "":
return OutcomeNone, nil
case SourceCoverArt:
return OutcomeFromArchive, nil
}
return OutcomeFromFiles, nil
},
}, svc.now)
}