Files
Jellymove/docs/ARCHITECTURE.md
T
IxianPixel 25a743b350
CI / test (push) Successful in 1m12s
Show folder sizes for movies, shows and seasons. Closes #5
The library lists now show how much space each movie and show takes on disk, counting everything a move copies, with a per-season breakdown for shows, a running total and a "Largest first" sort.

- sizes.rs: a single background worker measures one folder at a time. Listing returns cached sizes straight away and queues folders; the UI
  polls until every size has arrived.
- Cached in a new folder_sizes table (store migration v2). A size is kept while the folder's signature (mtimes of the folder and its direct
  subfolders) is unchanged, checked at most hourly and re-measured after 7 days.
- Each agent measures its own folders, so peer listings carry the peer's sizes.
- Tests for measuring, signatures, natural season order, the migration, and an e2e test covering both sides and a moved item.
2026-11-06 16:43:52 +00:00

111 lines
5.5 KiB
Markdown

# Architecture
## Why this exists
As of November 2026 nothing published moves media between two Jellyfin
servers while understanding their libraries. Adjacent tools each cover one part:
| Need | Closest existing tool | Gap |
|---|---|---|
| Show two servers as one | Jellyswarrm (v0.3.1 deduplicates across servers) | Presentation only; media management not implemented |
| Move bytes hot ↔ cold | mergerfs cache movers, rclone union, rsync, Syncthing | Path- and size-based, single host or blind to Jellyfin; no rescans, no watch state |
| Carry watch state | JellySync, JellyPlex-Watched | No file movement |
| Policy-driven cleanup | Janitorr / Maintainerr | Deletes rather than demotes |
jellymove joins these into one flow that knows about libraries.
## Topology
One binary, run as an agent next to each Jellyfin. Each agent has:
- local filesystem access at the same paths Jellyfin uses
- an admin API key for its own Jellyfin
- the other agent's URL and a shared token
Jobs always run on the **destination**, which pulls. Whichever UI the user
starts from, the job ends up there: `POST /api/moves {from: "local"}` asks the
peer to create the job, and `{from: "peer"}` creates it locally. This keeps the
code that copies, verifies, promotes and deletes next to the disk it touches,
and lets a NATed home server pull from a public cloud server with no port
forwarding.
## Peer protocol (`/peer/v1`, Bearer token)
| Route | Purpose |
|---|---|
| `GET /info` | Node name, version, Jellyfin version |
| `GET /libraries`, `GET /items?library=&search=` | Browse the peer's Jellyfin |
| `POST /exports {item_id}` | Source: start checking, hashing and capturing user data. Idempotent per item. |
| `GET /exports/{id}` | Poll: `Preparing` → `Ready` (with `ExportInfo`) or `Failed` |
| `GET /exports/{id}/files/{idx}` | Range-capable download of manifest entry `idx` |
| `POST /exports/{id}/complete {job_id?, moved_to?}` | Source: move folder to a new trash entry and rescan. Idempotent. |
| `GET /trash`, `POST /trash/empty {ids}` | List the source's trash entries; delete entries by ID (never by path) |
| `POST /jobs`, `GET /jobs` | Create or list jobs on the peer (destination) |
## Job stages
`Job.stage` is the last *completed* milestone. The runner performs the next step:
```
Created ──plan──▶ ExportReady ──copy──▶ Copied ──promote──▶ Promoted
──wait for scan──▶ TargetScanned ──apply user data──▶ UserDataApplied
──clean source──▶ SourceCleaned ──hook──▶ Done
```
`JobStatus` is orthogonal: Queued / Running / Planned (dry run) / Failed / Done.
Retrying a failed job re-runs from its stage. Interrupted jobs resume on start.
## Transfer
- HTTP `Range` pull into `<dest library>/.jellymove-partial/<job>/<idx>.part`.
The `.part` length is the resume offset, so no extra state is needed.
- BLAKE3 hash computed by the source (memory-mapped, multithreaded) and checked
by the destination after the last byte. A mismatch discards the part.
- Exponential backoff, 20 attempts by default; 4xx errors are permanent.
- Token-bucket bandwidth cap (`governor`) shared across transfers.
- Delta sync is deliberately absent: media is written once, so whole-file
transfer with resume beats rolling checksums.
- Alternatives kept open behind the pull/serve split: iroh (QUIC, NAT
traversal) once iroh-blobs reaches 1.0; rclone as a pluggable transport.
## Folder sizes
Library items carry the size of their folder on disk, counting the same files a
move copies. Each agent measures its own folders (`sizes.rs`): listing a library
returns cached sizes at once and queues every folder for a single background
worker, so a NAS is never asked to walk several folders at once. A cached size
is kept while the folder's signature (modified times of the folder and its
direct subfolders) is unchanged, re-checked at most hourly, and measured again
after seven days regardless. The UI polls the list until every size is in.
## Jellyfin integration
- Browse: `GET /Items?parentId=<library>&includeItemTypes=Movie,Series&recursive=true&fields=Path,…`
- Folder: a movie's parent folder, or a series' own folder. It must sit strictly
inside a library location. A movie folder must contain no other main videos.
- Rescan: `POST /Library/Media/Updated` for the exact folder, then poll. The
first update can be lost, so it is re-sent halfway through the timeout.
- Finding the new item: by **path**, since we know exactly where we put it.
This is more reliable than provider IDs, which may be missing or differ.
Episodes are matched by (season, episode).
- User data: captured per user from `GET /Items?userId=…`, written with
`POST /UserItems/{id}/UserData?userId=…`, users matched by name.
## Why Svelte rather than Slint
Slint is a native GUI toolkit. A headless daemon needs a browser UI, so the UI
is a Svelte 5 SPA compiled into the binary with `rust-embed`, the same
single-binary model Jellyswarrm uses. Live progress uses Server-Sent Events
for jobs on this node, and polling for jobs on the peer.
## Known risks
1. New item IDs after a move break anything keyed by ID: playlists,
collections, Jellyseerr links, Jellyswarrm's cache (#216).
2. Jellyswarrm's merge key for movies and series isn't documented upstream,
and its pipeline is being rewritten.
3. PUID/PGID and mount mismatches. The Status tab probes every location for
existence and write access.
4. Large scans: some Jellyfin versions re-save thousands of items on a full
scan, so jellymove only ever requests path-scoped updates.