queue

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func LineOrder

func LineOrder(a, b Item) bool

LineOrder sorts waiting items into the order they will start in.

Types

type ActivityEntry

type ActivityEntry struct {
	ID        int64
	MovieID   sql.NullInt64
	EventType string
	Message   string
	CreatedAt string
}

type CompletedItem

type CompletedItem struct {
	Item
	CompletedAt string
}

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

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 Event

type Event struct {
	At      string
	Kind    string
	Message string
	Level   Level
}

Event is a stored item event.

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 Level

type Level string

Level is how an item event is shown: info, warn or error.

const (
	LevelInfo  Level = "info"
	LevelWarn  Level = "warn"
	LevelError Level = "error"
)

type Priority

type Priority int

Priority orders the download line. Something a person asked for goes before anything the automatic searches added.

const (
	PriorityAutomatic Priority = 0
	PriorityManual    Priority = 1
)

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.

const (
	ProtocolUsenet  Protocol = "usenet"
	ProtocolTorrent Protocol = "torrent"
)

type Repo

type Repo struct {
	// contains filtered or unexported fields
}

func NewRepo

func NewRepo(db *sql.DB) *Repo

func (*Repo) AlbumEvents

func (r *Repo) AlbumEvents(albumID int64, limit int) ([]Event, error)

AlbumEvents returns an album's events, newest first, at most limit.

func (*Repo) Claim

func (r *Repo) Claim(id int64) (bool, error)

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

func (r *Repo) ClearFinished() (int64, error)

ClearFinished removes every completed, failed and stopped entry and returns how many.

func (*Repo) CountAutomaticAddedSince

func (r *Repo) CountAutomaticAddedSince(since time.Time) (int, error)

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

func (r *Repo) CountAutomaticOpen() (int, error)

CountAutomaticOpen counts the downloads the automatic searches added that are still waiting or running.

func (*Repo) CountRunning

func (r *Repo) CountRunning() (int, error)

CountRunning counts the downloads that are being worked on: downloading or importing. Ones waiting in line are counted by CountWaiting.

func (*Repo) CountWaiting

func (r *Repo) CountWaiting() (int, error)

CountWaiting counts the downloads waiting in line.

func (*Repo) Delete

func (r *Repo) Delete(id int64) error

Delete removes one queue entry (its history), not any file. Callers must not delete an item whose pipeline is still running.

func (*Repo) EncryptStoredURLs

func (r *Repo) EncryptStoredURLs() (int, error)

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

func (r *Repo) Enqueue(item Item) (int64, error)

func (*Repo) Get

func (r *Repo) Get(id int64) (Item, error)

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

func (r *Repo) HasActiveForAlbum(albumID int64) (bool, error)

HasActiveForAlbum is HasActiveForMovie for an album.

func (*Repo) HasActiveForMovie

func (r *Repo) HasActiveForMovie(movieID int64) (bool, error)

HasActiveForMovie reports whether movieID has a download that has not finished yet (queued, downloading, importing, paused, or parked on a conflict).

func (*Repo) List

func (r *Repo) List() ([]Item, error)

func (*Repo) ListActivity

func (r *Repo) ListActivity(limit int) ([]ActivityEntry, error)

func (*Repo) LogActivity

func (r *Repo) LogActivity(movieID int64, eventType, message string) error

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

func (r *Repo) LogItemActivity(movieID, seriesID int64, eventType, message string) error

LogItemActivity is LogActivity for a movie (movieID) or a show (seriesID) on the global feed.

func (*Repo) LogItemEvent

func (r *Repo) LogItemEvent(e ItemEvent) error

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

func (r *Repo) LogSeriesActivity(seriesID int64, eventType, message string) error

LogSeriesActivity is LogActivity for a show's event on the global feed.

func (*Repo) MovieEvents

func (r *Repo) MovieEvents(movieID int64, limit int) ([]Event, error)

MovieEvents returns a movie's events, newest first, at most limit.

func (*Repo) NextWaiting

func (r *Repo) NextWaiting(manualOnly bool) (Item, bool, error)

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

func (r *Repo) Pause(id int64, interrupted bool) error

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

func (r *Repo) PauseWaiting() (int64, error)

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

func (r *Repo) PruneActivity(cutoff time.Time) (int64, error)

PruneActivity deletes activity entries older than cutoff and returns how many.

func (*Repo) PruneFinished

func (r *Repo) PruneFinished(cutoff time.Time) (int64, error)

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

func (r *Repo) RecentFailed(since time.Time) (int, error)

RecentFailed counts downloads that failed since the given time.

func (*Repo) RecoverInterrupted

func (r *Repo) RecoverInterrupted() (int64, error)

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

func (r *Repo) ReleaseEvent(queueID int64, kind string, level Level, prefix string) error

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

func (r *Repo) Requeue(id int64) (bool, error)

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

func (r *Repo) SeriesEvents(seriesID int64, limit int) ([]Event, error)

SeriesEvents returns a show's events, newest first, at most limit.

func (*Repo) SetBox

func (r *Repo) SetBox(b *crypto.Box)

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

func (r *Repo) SetConflict(id int64, sourcePath, destPath string) error

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

func (*Repo) SetProgress

func (r *Repo) SetProgress(id int64, pct float64) error

func (*Repo) SetStatus

func (r *Repo) SetStatus(id int64, status Status, errMsg string) error

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL