diff --git a/docs/architecture.md b/docs/architecture.md index 06457c4..58a0653 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -62,8 +62,9 @@ stateDiagram-v2 ### Transition & Failure Invariants 1. **Linear Progression**: `QUEUED` ➔ `DOWNLOADING` ➔ `SCANNING` ➔ `VERIFYING_METADATA` ➔ `ORGANIZING` ➔ `COMPLETED`. 2. **Terminal Failure**: Any failure at any phase transitions the job directly to `FAILED` with a populated `error_message`. -3. **Crash & Restart Recovery**: Upon worker startup, any orphaned non-terminal jobs (`QUEUED`, `DOWNLOADING`, `SCANNING`, `VERIFYING_METADATA`, `ORGANIZING`) in the SQLite database are automatically transitioned to `FAILED` with reason `"Worker restarted during execution"`. Retrying requires a new submission. -4. **Duplicate Rejection (Idempotency)**: +3. **Stalled Download Timeout**: If a torrent achieves zero progress (0 bytes downloaded or unresolved magnet metadata) for 15 consecutive minutes, the job transitions to `FAILED` with reason `"Download stalled: no seeds or metadata timeout"`. +4. **Crash & Restart Recovery**: Upon worker startup, any orphaned non-terminal jobs (`QUEUED`, `DOWNLOADING`, `SCANNING`, `VERIFYING_METADATA`, `ORGANIZING`) in the SQLite database are automatically transitioned to `FAILED` with reason `"Worker restarted during execution"`. Retrying requires a new submission. +5. **Duplicate Rejection (Idempotency)**: - A submission is rejected if a non-terminal job for the same `imdb_id` is currently active in the database. - A submission is rejected if `/media/movies/*[imdbid-{imdb_id}]` already exists in the destination library. @@ -95,7 +96,7 @@ The sanitizer enforces a strict file-system boundary before files are touched or --- -## 📂 Storage Paths & Naming Convention +## 📂 Storage Paths & Two-Stage Atomic Relocation - **Canonical Staging Path**: `/data/staging//` - **Canonical Library Destination**: @@ -103,7 +104,12 @@ The sanitizer enforces a strict file-system boundary before files are touched or /media/movies/ (<Year>) [imdbid-<IMDbID>]/<Title> (<Year>).<ext> /media/movies/<Title> (<Year>) [imdbid-<IMDbID>]/<Title> (<Year>).en.srt ``` -- **Cleanup**: The `/data/staging/<job_id>/` folder is purged immediately upon successful completion or terminal failure. +- **Two-Stage Atomic Relocation (Cross-Volume Safe)**: + 1. *Stage 1 (Transfer)*: Validated files are copied from `/data/staging/<job_id>/` to a temporary directory on the library mount: `/media/movies/.staging_<job_id>/` using `shutil.copy2`. + 2. *Stage 2 (Atomic Swap)*: Once copying completes and file integrity is verified, `os.replace` atomically renames `/media/movies/.staging_<job_id>/` to the final `/media/movies/<Title> (<Year>) [imdbid-<IMDbID>]/` directory. This prevents Jellyfin/Plex from indexing partially-copied media. +- **Cleanup**: + - The scratch directory `/data/staging/<job_id>/` is purged immediately upon successful completion or terminal failure. + - The torrent session is removed from Transmission daemon via RPC `remove_torrent(id, delete_data=True)` to prevent lingering memory/network leaks. --- diff --git a/docs/deployment.md b/docs/deployment.md index 1b98cf1..9264d66 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -24,7 +24,7 @@ services: - magnetflix_internal labels: - "traefik.enable=true" - - "traefik.http.routers.magnetflix.rule=Host(`addmovie.arch-services.mywire.org`)" + - "traefik.http.routers.magnetflix.rule=Host(`addmovie.roadtotech.me`)" - "traefik.http.routers.magnetflix.entrypoints=websecure" - "traefik.http.routers.magnetflix.tls.certresolver=letsencrypt" - "traefik.http.routers.magnetflix.middlewares=authelia@docker" @@ -42,6 +42,7 @@ services: - DATABASE_URL=sqlite:////data/magnetflix.sqlite3 - STAGING_DIR=/data/staging - MEDIA_DIR=/media/movies + - TMDB_API_KEY=${TMDB_API_KEY} volumes: - ./data:/data - /mnt/storage/media/movies:/media/movies @@ -58,7 +59,7 @@ services: - TZ=America/Chicago volumes: - ./config/transmission:/config - - ./data/staging:/downloads + - ./data/staging:/data/staging networks: - magnetflix_internal @@ -73,7 +74,7 @@ networks: ## 🔐 Authelia SSO Integration -When a user visits `addmovie.arch-services.mywire.org`: +When a user visits `addmovie.roadtotech.me`: 1. Traefik triggers the `authelia@docker` forward-auth middleware. 2. If unauthenticated, Authelia redirects the user to the login portal. 3. Upon authentication, Authelia injects headers into the forwarded request: diff --git a/docs/overview.md b/docs/overview.md index f3f861e..ddc4d61 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -31,14 +31,14 @@ sequenceDiagram participant Engine as Transmission Daemon participant Media as Jellyfin / Plex Library - User->>Traefik: GET addmovie.arch-services.mywire.org + User->>Traefik: GET addmovie.roadtotech.me Traefik->>User: Renders MagNetFlix Web Form User->>Traefik: Submit form (magnet URI + IMDb ID: tt1375666) Traefik->>BFF: POST /api/v1/downloads (includes Remote-User header) BFF->>Worker: gRPC SubmitDownload(magnet, imdb_id, user) Worker-->>BFF: Acknowledged (Job ID: job-42) BFF-->>User: Enqueued! Showing live progress - Worker->>Engine: Start torrent download in /tmp/staging/job-42 + Worker->>Engine: Start torrent download in /data/staging/job-42 Engine-->>Worker: Download Completed Worker->>Worker: Run Security Whitelist Scan (Purge .exe/.bat) Worker->>Worker: Verify IMDb metadata & rename files