Documentation
¶
Overview ¶
Package queue tracks releases moving through grab -> download -> import (the download_queue table) and the activity feed ("one live view of everything currently downloading/importing/post-processing").
Index ¶
- func LineOrder(a, b Item) bool
- type ActivityEntry
- type CompletedItem
- type DispatchConfig
- type Dispatcher
- type Event
- type Item
- type ItemEvent
- type Level
- type Priority
- type Protocol
- type Repo
- func (r *Repo) AlbumEvents(albumID int64, limit int) ([]Event, error)
- func (r *Repo) Claim(id int64) (bool, error)
- func (r *Repo) ClearFailedForEpisodes(seriesID int64, season int, episodes []int, keep int64) (int64, error)
- func (r *Repo) ClearFailedForMovie(movieID, keep int64) (int64, error)
- func (r *Repo) ClearFinished() (int64, error)
- func (r *Repo) ClearSettledFailures() (int64, error)
- func (r *Repo) CountAutomaticAddedSince(since time.Time) (int, error)
- func (r *Repo) CountAutomaticOpen() (int, error)
- func (r *Repo) CountRunning() (int, error)
- func (r *Repo) CountWaiting() (int, error)
- func (r *Repo) Delete(id int64) error
- func (r *Repo) EncryptStoredURLs() (int, error)
- func (r *Repo) Enqueue(item Item) (int64, error)
- func (r *Repo) Get(id int64) (Item, error)
- func (r *Repo) HasActiveForAlbum(albumID int64) (bool, error)
- func (r *Repo) HasActiveForMovie(movieID int64) (bool, error)
- func (r *Repo) List() ([]Item, error)
- func (r *Repo) ListActivity(limit int) ([]ActivityEntry, error)
- func (r *Repo) ListActivityFor(limit int, query string) ([]ActivityEntry, error)
- func (r *Repo) LogActivity(movieID int64, eventType, message string) error
- func (r *Repo) LogItemActivity(movieID, seriesID int64, eventType, message string) error
- func (r *Repo) LogItemEvent(e ItemEvent) error
- func (r *Repo) LogSeriesActivity(seriesID int64, eventType, message string) error
- func (r *Repo) MovieEvents(movieID int64, limit int) ([]Event, error)
- func (r *Repo) NextWaiting(manualOnly bool) (Item, bool, error)
- func (r *Repo) Pause(id int64, interrupted bool) error
- func (r *Repo) PauseWaiting() (int64, error)
- func (r *Repo) PruneActivity(cutoff time.Time) (int64, error)
- func (r *Repo) PruneFinished(cutoff time.Time) (int64, error)
- func (r *Repo) RecentCompleted(limit int) ([]CompletedItem, error)
- func (r *Repo) RecentFailed(since time.Time) (int, error)
- func (r *Repo) RecoverInterrupted() (int64, error)
- func (r *Repo) ReleaseEvent(queueID int64, kind string, level Level, prefix string) error
- func (r *Repo) Requeue(id int64) (bool, error)
- func (r *Repo) SeriesEvents(seriesID int64, limit int) ([]Event, error)
- func (r *Repo) SetBox(b *crypto.Box)
- func (r *Repo) SetConflict(id int64, sourcePath, destPath string) error
- func (r *Repo) SetProgress(id int64, pct float64) error
- func (r *Repo) SetStatus(id int64, status Status, errMsg string) error
- type Status
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ActivityEntry ¶
type CompletedItem ¶
CompletedItem is a finished download with the time it finished.
type DispatchConfig ¶
type DispatchConfig struct {
// Limit is how many downloads may run at once. It is read on every
// decision, so a change applies at once: running downloads finish, and
// nothing new starts until fewer than the new limit are running.
Limit func() int
// ManualOnly, when it returns true, holds back everything the automatic
// searches added (safe mode). Downloads a person asked for still start.
ManualOnly func() bool
// Start begins the pipeline of item. An error means it could not begin.
Start func(item Item, done func()) error
// OnStartError is told about an item Start refused; the item is not
// waiting any more, so it should be put right (marked failed).
OnStartError func(item Item, err error)
// Hold, when it returns true, starts nothing new for now (the disk is
// nearly full, say). Running downloads carry on, and the line is looked
// at again on the next Kick.
Hold func() bool
}
DispatchConfig is what a Dispatcher needs from the app around it.
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher runs the download line: it starts the next waiting item whenever fewer than Limit are running, and again each time one ends. A download keeps its place for its whole pipeline (download, repair, unpack, import), so a limit of one really means one at a time.
It knows nothing about how a download is run. Start begins one and returns straight away; the function it is handed (done) must be called exactly once when that download has completely ended, whatever the outcome.
func NewDispatcher ¶
func NewDispatcher(repo *Repo, cfg DispatchConfig) *Dispatcher
NewDispatcher returns a dispatcher over repo. Nothing runs until Kick.
func (*Dispatcher) FreePlace ¶
func (d *Dispatcher) FreePlace() bool
FreePlace reports whether a download added now would start straight away.
func (*Dispatcher) Hold ¶
func (d *Dispatcher) Hold() (release func())
Hold stops new downloads from starting until the returned function is called, which also starts whatever can now start. Pausing everything uses it so the line does not move on while the running downloads wind down.
func (*Dispatcher) Holding ¶
func (d *Dispatcher) Holding(id int64) bool
Holding reports whether id is one of the downloads holding a place.
func (*Dispatcher) Kick ¶
func (d *Dispatcher) Kick()
Kick starts waiting downloads while there are free places. Call it whenever something may have changed: a download was added, resumed or ended, or the limit was raised. It never blocks on a download.
func (*Dispatcher) Limit ¶
func (d *Dispatcher) Limit() int
Limit is the number of downloads allowed to run at once: at least one.
func (*Dispatcher) Running ¶
func (d *Dispatcher) Running() int
Running is how many downloads hold a place right now.
type Item ¶
type Item struct {
ID int64
MovieID int64 // 0 for a TV grab
IndexerID sql.NullInt64
// TV grabs: SeriesID > 0 and Season > 0; Episode == 0 means a whole
// season pack. Movie grabs leave all three zero.
SeriesID int64
Season int
Episode int
AlbumID int64 // music grabs: the album; 0 otherwise
ReleaseTitle string
NZBURL string
SizeBytes int64
Protocol Protocol
Status Status
ProgressPct float64
Error string
// Interrupted marks a paused item that nobody paused: Mediarium was
// stopped or restarted while it was downloading (see RecoverInterrupted).
Interrupted bool
// Priority and LineSeq say where a waiting item stands in the download
// line: a higher Priority goes first, and within a priority the lower
// LineSeq. Enqueue sets both; List and Get fill them in.
Priority Priority
LineSeq int64
// SourcePath/DestPath are only set for StatusConflict items (see
// SetConflict) — the file still sitting under /downloads, and where it
// would go once resolved.
SourcePath string
DestPath string
// AddedAt/CompletedAt are set by List (RFC 3339 text; CompletedAt is
// empty until the item finishes).
AddedAt string
CompletedAt string
}
type ItemEvent ¶
type ItemEvent struct {
MovieID int64 // one of MovieID/SeriesID/AlbumID is set
SeriesID int64
AlbumID int64
Kind string // searched, grabbed, download, postprocess, imported, failed, blocklisted, retried, removed...
Message string
Level Level
// ItemOnly events are detail for the item's own log (a search, a
// download step) and are left out of the global activity feed.
ItemOnly bool
}
ItemEvent is one event in a movie's or show's own activity log.
type Priority ¶
type Priority int
Priority orders the download line. Something a person asked for goes before anything the automatic searches added.
type Protocol ¶
type Protocol string
Protocol mirrors internal/indexers.Protocol — duplicated as its own type rather than imported so this package (which owns persistence only) doesn't need to depend on the indexers package for a two-value enum.
type Repo ¶
type Repo struct {
// contains filtered or unexported fields
}
func (*Repo) AlbumEvents ¶
AlbumEvents returns an album's events, newest first, at most limit.
func (*Repo) Claim ¶
Claim takes a waiting item out of the line and marks it as downloading, in one step: it reports false when the item is no longer waiting (a person paused, stopped or removed it a moment ago). The "download started" note is left to the pipeline, which sets the same status when it begins.
func (*Repo) ClearFailedForEpisodes ¶ added in v1.4.1
func (r *Repo) ClearFailedForEpisodes(seriesID int64, season int, episodes []int, keep int64) (int64, error)
ClearFailedForEpisodes removes the failed entries of a show, other than keep, that were for one of the given episodes of season (a single-episode download), once those episodes have been downloaded another way.
func (*Repo) ClearFailedForMovie ¶ added in v1.4.1
ClearFailedForMovie removes the failed entries of a movie, other than keep, once the movie has been downloaded another way: they no longer need anyone's attention. It returns how many.
func (*Repo) ClearFinished ¶
ClearFinished removes every completed, failed and stopped entry and returns how many.
func (*Repo) ClearSettledFailures ¶ added in v1.4.1
ClearSettledFailures removes failed entries of a movie that a later download of the same movie completed, so old failures that no longer need attention leave the queue. It returns how many.
func (*Repo) CountAutomaticAddedSince ¶
CountAutomaticAddedSince counts the downloads the automatic searches added to the line at or after since, however they finished. Downloads a person asked for are not counted.
func (*Repo) CountAutomaticOpen ¶
CountAutomaticOpen counts the downloads the automatic searches added that are still waiting or running.
func (*Repo) CountRunning ¶
CountRunning counts the downloads that are being worked on: downloading or importing. Ones waiting in line are counted by CountWaiting.
func (*Repo) CountWaiting ¶
CountWaiting counts the downloads waiting in line.
func (*Repo) Delete ¶
Delete removes one queue entry (its history), not any file. Callers must not delete an item whose pipeline is still running.
func (*Repo) EncryptStoredURLs ¶
EncryptStoredURLs encrypts the download addresses that older versions kept in plain text. It returns how many it changed and does nothing once they are all encrypted.
func (*Repo) Get ¶
Get returns one queue item by id, including SourcePath — List includes DestPath (the UI needs it to show a conflict item's proposed destination) but omits SourcePath, which only resolveConflict's actual file move needs.
func (*Repo) HasActiveForAlbum ¶
HasActiveForAlbum is HasActiveForMovie for an album.
func (*Repo) HasActiveForMovie ¶
HasActiveForMovie reports whether movieID has a download that has not finished yet (queued, downloading, importing, paused, or parked on a conflict).
func (*Repo) ListActivity ¶
func (r *Repo) ListActivity(limit int) ([]ActivityEntry, error)
func (*Repo) ListActivityFor ¶ added in v1.4.0
func (r *Repo) ListActivityFor(limit int, query string) ([]ActivityEntry, error)
ListActivityFor is ListActivity narrowed to lines whose text contains query (case-insensitive; "" = all), with what is needed to link each line to its title.
func (*Repo) LogActivity ¶
LogActivity records one event on the activity feed.
movieID <= 0 is stored as NULL (activity.movie_id is nullable and SET NULL on delete) — used by TV events, which aren't tied to a movie.
func (*Repo) LogItemActivity ¶
LogItemActivity is LogActivity for a movie (movieID) or a show (seriesID) on the global feed.
func (*Repo) LogItemEvent ¶
LogItemEvent records an event for a movie or show. An item-only event that repeats one of the item's recent events word for word (the same search outcome every half hour) only moves that event's time forward, and each item keeps at most maxItemOnlyEvents item-only events.
func (*Repo) LogSeriesActivity ¶
LogSeriesActivity is LogActivity for a show's event on the global feed.
func (*Repo) MovieEvents ¶
MovieEvents returns a movie's events, newest first, at most limit.
func (*Repo) NextWaiting ¶
NextWaiting returns the item that should start next, or false when nothing is waiting. With manualOnly it only looks at what a person asked for.
func (*Repo) Pause ¶
Pause parks item id as paused. interrupted says nobody paused it: Mediarium was stopped or restarted while it was downloading. Its progress is left as it is and its error text is cleared.
func (*Repo) PauseWaiting ¶
PauseWaiting parks every download waiting in line as paused and marked interrupted, so a person has to press Resume. Safe mode uses it at start-up. It returns how many it paused.
func (*Repo) PruneActivity ¶
PruneActivity deletes activity entries older than cutoff and returns how many.
func (*Repo) PruneFinished ¶
PruneFinished deletes completed, failed and stopped entries that finished before cutoff and returns how many. Entries still running, paused or parked on a conflict are never pruned.
func (*Repo) RecentCompleted ¶
func (r *Repo) RecentCompleted(limit int) ([]CompletedItem, error)
RecentCompleted returns the most recently completed downloads, newest first.
func (*Repo) RecentFailed ¶
RecentFailed counts downloads that failed since the given time.
func (*Repo) RecoverInterrupted ¶
RecoverInterrupted pauses every item that was downloading or importing when Mediarium last stopped: nothing is running it any more, so it would sit there for ever. They come back paused and marked interrupted, and stay that way until a person resumes them. Items that were only waiting in line stay waiting: nothing was started for them, and the line carries on by itself. Call it once at startup, before anything can start a download. It returns how many it paused.
func (*Repo) ReleaseEvent ¶
ReleaseEvent records an item-only event for the movie or show a queue item belongs to, naming its release: "<prefix>: <release title>".
func (*Repo) Requeue ¶
Requeue puts a paused item back in the line, at the front of its priority (it was already started once, so it goes before the ones that never were). It reports false when the item is not paused.
func (*Repo) SeriesEvents ¶
SeriesEvents returns a show's events, newest first, at most limit.
func (*Repo) SetBox ¶
SetBox makes the repo keep download addresses encrypted. A download address from an indexer usually carries the source's API key or a tracker passkey (`...&apikey=KEY`), so the database file alone must not reveal it.
func (*Repo) SetConflict ¶
SetConflict parks item id in StatusConflict with the source file's current location and its proposed (but not-yet-taken) destination, for a person to resolve later via Get/List + a caller-driven overwrite-or- skip decision (this package only persists the state, same separation as the rest of the pipeline — internal/api owns the actual file move).
type Status ¶
type Status string
const ( StatusQueued Status = "queued" StatusDownloading Status = "downloading" StatusImporting Status = "importing" StatusCompleted Status = "completed" StatusFailed Status = "failed" // StatusConflict means the download finished but landed on a naming // collision under the "always ask" import conflict policy — // parked here with SourcePath/DestPath populated so a person can // resolve it (skip or overwrite) rather than it being silently skipped. StatusConflict Status = "conflict" // StatusPaused is a download whose transfer is stopped but whose partly // downloaded files and claim on the title are kept. It survives a // restart and is never resumed by itself: a person presses Resume. StatusPaused Status = "paused" // StatusStopped is a download a person cancelled. The title is free to // be searched again; the item stays in the list until it is retried or // removed. StatusStopped Status = "stopped" )