- feat: DELETE /api/v1/providers/{name}/data (admin) removes everything a
provider gave: its lists of similar artists, its tags and the records of
when it was asked, then the artists that only that data named and that
nobody has files for. Last.fm's terms ask for this when an agreement ends.
The user's own artists, music, listens and settings stay, the provider is not
switched off, and a second call removes nothing
- feat: a discovered artist that no list names any more is removed when the
list that named it is replaced, together with its tags, so they do not pile
up. Real artists, and any artist that another list still names, are kept
- feat: the scan reads the GENRE tag of each file, offline (several genres
separated by ";", lower case, at most five per file), and
GET /artists/{id}/tags gets a third entry, files: the genres of the artist's
own playable tracks, each weighted by how many tracks have it. It asks nobody
and is always there
- feat: POST /library/scan and POST /libraries/{id}/scan take full=true to read
every file again, not only the new or changed ones, so MusicBrainz ids and
genres reach music that was indexed before. Files keep their identity, so
listening history is kept. Anything but true or false is a 400
- test: the plan of a full scan, the scanner and the manager; the purge on both
databases, checked by removing the rule that spares real artists; the
cleanup of replaced lists; genres per file and per artist
- docs: README, OpenAPI and the todo list updated
122 lines
3.2 KiB
Go
122 lines
3.2 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Errors returned when a scan cannot start.
|
|
var (
|
|
ErrScanRunning = errors.New("a scan is already running")
|
|
ErrNoLibraries = errors.New("there are no libraries to scan")
|
|
)
|
|
|
|
// LibrarySource lists the libraries a scan can cover. LibraryStore satisfies it.
|
|
type LibrarySource interface {
|
|
ListLibraries(ctx context.Context) ([]Library, error)
|
|
GetLibrary(ctx context.Context, id int64) (Library, error)
|
|
}
|
|
|
|
// Status describes the scan state for the API.
|
|
type Status struct {
|
|
Running bool
|
|
StartedAt time.Time
|
|
LastFinishedAt time.Time
|
|
Last *Summary // result of the last finished scan
|
|
LastError string
|
|
}
|
|
|
|
// Manager runs at most one background scan at a time.
|
|
type Manager struct {
|
|
scanner *Scanner
|
|
libraries LibrarySource
|
|
|
|
mu sync.Mutex
|
|
status Status
|
|
onFinish func(Summary, error)
|
|
}
|
|
|
|
// NewManager builds a Manager that scans libraries from the given source.
|
|
func NewManager(scanner *Scanner, libraries LibrarySource) *Manager {
|
|
return &Manager{scanner: scanner, libraries: libraries}
|
|
}
|
|
|
|
// OnFinish registers a function called once after every scan, with the result
|
|
// of that scan. Unlike hub events it is never dropped. Set it before Start.
|
|
// A new scan may start while a callback is still running, so callbacks of
|
|
// consecutive scans can overlap: keep the function safe for concurrent use.
|
|
func (m *Manager) OnFinish(fn func(Summary, error)) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.onFinish = fn
|
|
}
|
|
|
|
// Start scans every library in the background. ctx bounds the scan, so pass a
|
|
// context that lives as long as the application, not an HTTP request.
|
|
// It returns ErrNoLibraries when none are defined.
|
|
func (m *Manager) Start(ctx context.Context, opts ...ScanOption) error {
|
|
libs, err := m.libraries.ListLibraries(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("list libraries: %w", err)
|
|
}
|
|
if len(libs) == 0 {
|
|
return ErrNoLibraries
|
|
}
|
|
return m.start(ctx, libs, opts)
|
|
}
|
|
|
|
// StartLibrary scans a single library in the background.
|
|
// It returns ErrLibraryNotFound for an unknown id.
|
|
func (m *Manager) StartLibrary(ctx context.Context, id int64, opts ...ScanOption) error {
|
|
lib, err := m.libraries.GetLibrary(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return m.start(ctx, []Library{lib}, opts)
|
|
}
|
|
|
|
func (m *Manager) start(ctx context.Context, libs []Library, opts []ScanOption) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.status.Running {
|
|
return ErrScanRunning
|
|
}
|
|
m.status.Running = true
|
|
m.status.StartedAt = time.Now()
|
|
go m.run(ctx, libs, opts)
|
|
return nil
|
|
}
|
|
|
|
func (m *Manager) run(ctx context.Context, libs []Library, opts []ScanOption) {
|
|
sum, err := m.scanner.Scan(ctx, libs, opts...)
|
|
|
|
m.mu.Lock()
|
|
m.status.Running = false
|
|
m.status.LastFinishedAt = time.Now()
|
|
m.status.Last = &sum
|
|
m.status.LastError = ""
|
|
if err != nil {
|
|
m.status.LastError = err.Error()
|
|
}
|
|
fn := m.onFinish
|
|
m.mu.Unlock()
|
|
|
|
if fn != nil {
|
|
fn(sum, err)
|
|
}
|
|
}
|
|
|
|
// Status returns a snapshot of the scan state.
|
|
func (m *Manager) Status() Status {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.status
|
|
}
|
|
|
|
// Subscribe returns a channel of scan events (see Hub.Subscribe).
|
|
func (m *Manager) Subscribe(buffer int) (<-chan Event, func()) {
|
|
return m.scanner.hub.Subscribe(buffer)
|
|
}
|