Files
Jellymove/docs/ARCHITECTURE.md
2026-11-06 17:34:22 +00:00

122 lines
6.4 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 |
| `GET /seasons?series=` | A show's seasons, with each one's folder if it has its own |
| `GET /users`, `GET /watchers?item=` | User names; users with watch state for an item. Used to warn before a move whose watch history wouldn't carry over |
| `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) |
| `POST /jobs/{id}/reapply` | Apply the watch state of users who had no account when the move ran (`Job.missing_users`), once they do |
## 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.
- Seasons: `includeItemTypes=Season` under the series. A season export's folder
is the show's; its manifest holds only `<season folder>/…` plus the show's
loose non-video files (`SeasonInfo.show_files`). The destination finds the
show by provider ID, then name, and skips those show files if it already has
the show. A season's own user data isn't carried (it would land on the
whole show); its episodes' is. After the last season leaves, the source
moves what remains of the show folder into the same trash entry, but only if
no videos are left in it.
- 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.