ADR-0022: Per-instance priority for searches and downloads¶
- Status: accepted
- Date: 2026-10-21
Context¶
SnatchArr (pronounced "snatch-arr": every search is a snatch, -arr is the *arr family suffix) treats every *arr instance alike today:
- The worker leases queued runs oldest first (
LeaseRuninapi/internal/store/queries/runs.sql,ORDER BY queued_at). When workers are busy or the shared stamina (global_hourly_cap,api/internal/domain/settings.go) runs short, whichever instance queued first takes the searches. - The queue sweep walks instances in database order (
SweepAllinapi/internal/queue/service.go), so the force imports and removals of a Whisparr queue can run before those of Sonarr.
The operator asked (2026-10-20, node SNX-INSTANCE-PRIO, paraphrased) for a priority per instance: a sensible default puts movies and TV first, music next and adult content last, and each user sets the order for searches and the order for downloads separately. The default is a shipped preset the user edits, not a fixed constant.
Decision¶
1. Two orders, one rank each¶
Every instance carries two ranks: a search rank and a download rank. A rank is an integer from 1 to 100; a lower rank goes first. Equal ranks share their place.
- Search order decides which queued run a worker leases next, and so which instance spends the indexer budget and the shared stamina first.
- Download order decides the order in which the queue sweep visits instances, so imports, removals and re-searches of a higher instance run first in each sweep. Ordering inside download clients follows in node
SNX-DLCLIENT-PRIO, which reads the same rank; this record does not change any download client.
2. Shipped preset and editable active defaults¶
The ranks come from a preset keyed by instance kind. SnatchArr ships one preset, media_first:
| Kind | Search rank | Download rank |
|---|---|---|
sonarr, radarr (TV and movies) |
10 | 10 |
lidarr (music) |
20 | 20 |
readarr (books) |
30 | 30 |
whisparr_v2, whisparr_v3 (adult) |
40 | 40 |
Books are not named in the request; they sit after music and before adult content, and the user can move them. The steps of ten leave room to slot a kind in between without renumbering.
- The active preset is stored in the global settings row and edited through
PUT /api/v1/settingslike every other setting (search_priority_preset,download_priority_preset). A stored preset names every instance kind; a preset that leaves one out is refused, so the lease query never needs a fallback. GET /api/v1/priority-presetsreturns the shipped presets, so the UI restoresmedia_firstfrom the server instead of a copy in the web code.- An instance may override either rank through
GET/PUT /api/v1/instances/{instanceId}/priority(search_priority,download_priority, null = follow the preset). The response carries the effective rank and its source (presetorinstance) for each order. Clearing an override returns the instance to the preset. - The priority endpoint is separate from the instance definition, like the snatch policy, so Configarr-managed instances accept it while their definition stays read-only.
3. Named snapshots and accessible reordering¶
Accepted on 2026-10-31; decision ledger entries snatcharr-prio-named-presets-model and snatcharr-prio-reorder-semantics.
A named preset stores both the search and download maps in priority_presets (migration 000053_priority_presets). Up to 20 user presets may exist. Names contain 1–40 characters after trimming and are unique ignoring case. The shipped media_first preset remains read-only, is listed with builtin: true, and does not occupy a user slot. User presets have UUIDs; the builtin has no UUID.
POST /api/v1/priority-presets/{presetId}/apply copies both maps through the settings service's existing update path. A user snapshot is addressed by UUID; POST /api/v1/priority-presets/media_first/apply applies the shipped snapshot by name. Editing, renaming or deleting a saved snapshot does not change active settings. Applying it again explicitly replaces both active maps. Mutations require the same administrator authorization as settings updates. The existing list response retains name, search and download and adds builtin and user id fields.
The caps editor shows separate search and download lists. Moving a kind assigns ranks of 10 × position (one-based). An explicit tie uses the rank of the immediately preceding row; the first row cannot tie. Moving a tied row clears that row's tie. Up and down buttons with localized accessible names provide keyboard operation, and a live region announces the new position and rank. The Save action persists the edited maps. Instance overrides keep their existing numeric behavior; instances without overrides follow the active maps. See PriorityOrderFields.svelte and priority-order.test.ts.
4. No starvation¶
Rank ordering alone could hold a low-ranked run back for as long as higher runs keep arriving. A queued run older than priority_max_wait (one hour, a constant in api/internal/snatch) sorts before every younger run regardless of rank. Inside each group the order is rank, then queued_at. The bound keeps the wait of the lowest instance finite (HISS-02) while the preset still decides every ordinary contention.
5. Tests¶
Each consumer ships positive, negative and boundary tests:
- Positive: with
media_first, a Sonarr run queued after a Whisparr run is leased first; the sweep visits Sonarr before Whisparr. - Negative: ranks outside 1–100, unknown kinds in a preset and malformed overrides are refused with RFC 9457 problem details; a cleared override falls back to the preset.
- Boundary: equal ranks keep
queued_atorder; a run pastpriority_max_waitovertakes a higher rank; ranks 1 and 100 are accepted. - Named preset regressions: names at 0/1/40/41 characters, case-insensitive duplicates, the 20th and 21st saves, complete maps, read-only builtin and administrator authorization. Applying copies both maps; later edits and deletion leave the active settings unchanged (
handlers_priority_presets_integration_test.go). - Keyboard reordering and saving are checked through the served settings API; the named
nightsnapshot is saved, applied and read back. Both lists and the named panel retain zero axe violations (web/e2e/features/559-instance-priority.test.ts). - Planted case: skipping the apply copy or the reorder map write makes the corresponding regression fail.
- Planted case: a lease query that ignores the rank (back to
ORDER BY queued_at) turns the positive test red.
Consequences¶
- Positive: the operator's ordering applies out of the box; scarce stamina goes to the media the user cares about first; search and download orders can differ (for example, music searched before books but imported after them).
- Neutral: existing installs get
media_firston upgrade; no instance carries an override until the user sets one. With idle workers and spare stamina the order changes nothing, because every due run is leased at once. - Negative: a lowest-ranked instance can wait up to
priority_max_waitlonger than today under contention.
Alternatives rejected¶
- Weights (share of the budget per instance) instead of ranks: rejected for the first version. Ranks answer "who goes first" directly and are easy to read in the UI; weights need a fair-share scheduler over the stamina buckets and can follow if measured contention calls for them.
- A fixed order in code: rejected; the request asks for a preset the user edits.
- One shared order for searches and downloads: rejected; the request asks for separate orders.
References¶
- Node
SNX-INSTANCE-PRIO(2026-10-20) (paraphrased): instances get priorities; the default puts movies and TV first, music next and adult content last; users set search and download orders separately; the default is an editable shipped preset. - Node
SNX-DLCLIENT-PRIO(2026-10-20) (paraphrased): applies the instance priorities inside download clients per category or tag. - ADR-0006, ADR-0012, ADR-0018, ADR-0020.