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) }