# janisadhi/music-sync architecture

> Architecture diagram of the GitHub repository janisadhi/music-sync, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/janisadhi/music-sync
- Repository: https://github.com/janisadhi/music-sync

## Overview

#### Purpose and Entry Points
Music Sync is a self-hosted system that mirrors configured YouTube Music playlists into a local audio library, enriches tracks with tags/artwork and synchronized lyrics, and exposes management APIs plus a browser dashboard. Backend startup and worker lifecycle are centralized in `app/main.py`; the SPA starts at `dashboard/src/main.jsx` and API access is routed through `dashboard/src/services/api.js`.

#### Backend Application Boundary
FastAPI provides authenticated REST operations for playlists, songs, synchronization, metadata, settings, and dashboard status. API handlers should remain orchestration-facing; persistent state and domain behavior live behind service modules. Core runtime/configuration, authentication, filesystem paths, and shared yt-dlp setup form the application boundary.
Paths: `app/api/sync.py`, `app/api/playlists.py`, `app/core/runtime.py`, `app/core/ytdlp.py`.

#### Persistent State and Library Storage
PostgreSQL is the source of truth for users, app settings, playlists, songs, download/retry state, metadata history, and cookie-backed YouTube access. SQLAlchemy models/session management support all workers; Alembic evolves this schema. Audio, artwork, and `.lrc` files are persisted in the mounted local library rather than the database.
Paths: `app/database/models.py`, `app/database/session.py`, `alembic/env.py`, `app/core/paths.py`.

#### Discovery, Reconciliation, and Sync
A synchronization run reads configured playlists, uses yt-dlp flat extraction to discover remote video/track state, and reconciles it with stored playlist/song records. Discovery creates or updates work candidates; it does not directly couple playlist scanning to media extraction. This is the first stage of the stated worker contract.
Paths: `app/sync/service.py`, `app/watcher/youtube.py`, `app/reconciler/service.py`.

#### Download Pipeline
The downloader asynchronously drains eligible database-backed work, invokes yt-dlp to retrieve native/high-quality Opus audio, and records outcomes, failure classification, and exponential-backoff retry state. The backend image supplies yt-dlp’s runtime dependencies, including Deno for YouTube challenge solving, FFmpeg for media processing, and tagging tools.
Paths: `app/downloader/service.py`, `app/downloader/worker.py`, `Dockerfile`.

#### Lyrics and Metadata Enrichment
After download, a separate lyrics worker fetches timed lyrics from LRCLIB and writes `.lrc` output; tracks without lyrics are organized separately. A distinct metadata service can scan/process the local library, match music metadata through MusicBrainz and Beets, write tags/artwork, and organize files. This is an inter-service/media-library boundary rather than frontend logic.
Paths: `app/lyrics/service.py`, `app/lyrics/worker.py`, `metadata_service/main.py`, `metadata_service/processor.py`, `metadata_service/musicbrainz.py`.

#### Scheduling and Operations
APScheduler triggers periodic synchronization while download and lyric workers operate independently, preventing a slow external integration from blocking discovery or API serving. Persistent settings control scheduling, playlist-watch behavior, directories, retries, and cookies.
Paths: `app/scheduler/service.py`, `app/settings/service.py`, `app/core/events.py`.

#### Frontend and Deployment
React 19/Vite provides the management and playback SPA: protected routes, playlist/song/metadata/settings views, and synchronized lyric display. In production Nginx serves the static dashboard and proxies API requests. Docker Compose runs PostgreSQL, backend, and dashboard; GitHub Actions supports container deployment to GHCR.
Paths: `dashboard/src/App.jsx`, `dashboard/src/layouts/DashboardLayout.jsx`, `dashboard/src/components/AudioPlayer.jsx`, `dashboard/nginx.conf`, `docker-compose.yml`, `.github/workflows/deploy.yaml`.

Main entry points: [`app/database/models.py`](https://github.com/janisadhi/music-sync/blob/main/app/database/models.py) (PostgreSQL state), [`app/api/sync.py`](https://github.com/janisadhi/music-sync/blob/main/app/api/sync.py) (FastAPI REST API), [`app/core/runtime.py`](https://github.com/janisadhi/music-sync/blob/main/app/core/runtime.py) (Runtime and yt-dlp setup), [`docker-compose.yml`](https://github.com/janisadhi/music-sync/blob/main/docker-compose.yml) (Docker Compose stack)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Dashboard"]
  node_spa["React management SPA<br/>React/Vite<br/>[App.jsx]"]
  node_audio_player["Audio player<br/>React component<br/>[AudioPlayer.jsx]"]
  node_nginx{{"Nginx dashboard server<br/>reverse proxy<br/>[nginx.conf]"}}
end

subgraph group_backend["Backend application"]
  node_api["FastAPI REST API<br/>[sync.py]"]
  node_runtime["Runtime and yt-dlp setup<br/>application core<br/>[runtime.py]"]
  node_settings["Settings service<br/>domain service<br/>[service.py]"]
  node_scheduler["Sync scheduler<br/>APScheduler service<br/>[service.py]"]
end

subgraph group_workers["Sync and enrichment workers"]
  node_sync["Sync orchestration<br/>sync service<br/>[service.py]"]
  node_youtube_discovery["YouTube playlist discovery<br/>yt-dlp discovery<br/>[youtube.py]"]
  node_reconciler["Playlist reconciler<br/>reconciliation service<br/>[service.py]"]
  node_download_worker["Download worker<br/>async worker<br/>[worker.py]"]
  node_downloader["Media downloader<br/>yt-dlp service<br/>[service.py]"]
  node_lyrics_worker["Lyrics worker<br/>async worker<br/>[worker.py]"]
  node_metadata_service["Metadata service<br/>library processor<br/>[processor.py]"]
end

subgraph group_storage["Persistent storage"]
  node_database[("PostgreSQL state<br/>SQLAlchemy models<br/>[models.py]")]
  node_library[("Mounted media library<br/>filesystem storage<br/>[paths.py]")]
  node_migration["Schema migrations<br/>Alembic<br/>[env.py]"]
end

subgraph group_deployment["Deployment"]
  node_compose{{"Docker Compose stack<br/>container orchestration<br/>[docker-compose.yml]"}}
  node_backend_image["Backend runtime image<br/>Docker image"]
  node_deployment_ci["Container deployment CI<br/>GitHub Actions<br/>[deploy.yaml]"]
end

node_spa -->|"renders"| node_audio_player
node_spa -->|"served by"| node_nginx
node_nginx -->|"proxies API"| node_api
node_api -->|"manages"| node_settings
node_api -->|"starts sync"| node_sync
node_api -->|"reads and writes"| node_database
node_runtime -->|"starts"| node_api
node_runtime -->|"runs"| node_download_worker
node_runtime -->|"runs"| node_lyrics_worker
node_settings -->|"persists settings"| node_database
node_scheduler -->|"triggers"| node_sync
node_settings -->|"controls"| node_scheduler
node_sync -->|"discovers playlists"| node_youtube_discovery
node_youtube_discovery -->|"remote track state"| node_reconciler
node_reconciler -->|"creates work candidates"| node_database
node_download_worker -->|"claims and updates work"| node_database
node_download_worker -->|"retrieves media"| node_downloader
node_downloader -->|"writes audio"| node_library
node_lyrics_worker -->|"finds downloaded tracks"| node_database
node_lyrics_worker -->|"writes .lrc files"| node_library
node_metadata_service -->|"tags, artworks, organizes"| node_library
node_migration -->|"evolves schema"| node_database
node_compose -->|"runs"| node_database
node_compose -->|"runs backend"| node_runtime
node_compose -->|"runs dashboard"| node_nginx
node_backend_image -->|"provides runtime dependencies"| node_runtime
node_deployment_ci -->|"builds and deploys"| node_backend_image

click node_spa "https://github.com/janisadhi/music-sync/blob/main/dashboard/src/App.jsx"
click node_audio_player "https://github.com/janisadhi/music-sync/blob/main/dashboard/src/components/AudioPlayer.jsx"
click node_nginx "https://github.com/janisadhi/music-sync/blob/main/dashboard/nginx.conf"
click node_api "https://github.com/janisadhi/music-sync/blob/main/app/api/sync.py"
click node_runtime "https://github.com/janisadhi/music-sync/blob/main/app/core/runtime.py"
click node_settings "https://github.com/janisadhi/music-sync/blob/main/app/settings/service.py"
click node_scheduler "https://github.com/janisadhi/music-sync/blob/main/app/scheduler/service.py"
click node_sync "https://github.com/janisadhi/music-sync/blob/main/app/sync/service.py"
click node_youtube_discovery "https://github.com/janisadhi/music-sync/blob/main/app/watcher/youtube.py"
click node_reconciler "https://github.com/janisadhi/music-sync/blob/main/app/reconciler/service.py"
click node_download_worker "https://github.com/janisadhi/music-sync/blob/main/app/downloader/worker.py"
click node_downloader "https://github.com/janisadhi/music-sync/blob/main/app/downloader/service.py"
click node_lyrics_worker "https://github.com/janisadhi/music-sync/blob/main/app/lyrics/worker.py"
click node_metadata_service "https://github.com/janisadhi/music-sync/blob/main/metadata_service/processor.py"
click node_database "https://github.com/janisadhi/music-sync/blob/main/app/database/models.py"
click node_library "https://github.com/janisadhi/music-sync/blob/main/app/core/paths.py"
click node_migration "https://github.com/janisadhi/music-sync/blob/main/alembic/env.py"
click node_compose "https://github.com/janisadhi/music-sync/blob/main/docker-compose.yml"
click node_backend_image "https://github.com/janisadhi/music-sync/blob/main/Dockerfile"
click node_deployment_ci "https://github.com/janisadhi/music-sync/blob/main/.github/workflows/deploy.yaml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_spa,node_audio_player,node_nginx toneBlue
class node_api,node_runtime,node_settings,node_scheduler toneAmber
class node_sync,node_youtube_discovery,node_reconciler,node_download_worker,node_downloader,node_lyrics_worker,node_metadata_service toneMint
class node_database,node_library,node_migration toneRose
class node_compose,node_backend_image,node_deployment_ci toneIndigo
```

## Components

### Dashboard

- [React management SPA](https://github.com/janisadhi/music-sync/blob/main/dashboard/src/App.jsx): React/Vite · Protected management, playback, and lyric views. · `dashboard/src/App.jsx`
- [Audio player](https://github.com/janisadhi/music-sync/blob/main/dashboard/src/components/AudioPlayer.jsx): React component · Plays library audio with synchronized lyrics. · `dashboard/src/components/AudioPlayer.jsx`
- [Nginx dashboard server](https://github.com/janisadhi/music-sync/blob/main/dashboard/nginx.conf): reverse proxy · Serves the SPA and proxies API requests. · `dashboard/nginx.conf`

### Backend application

- [FastAPI REST API](https://github.com/janisadhi/music-sync/blob/main/app/api/sync.py): FastAPI · Authenticated orchestration endpoints for library management. · `app/api/sync.py`
- [Runtime and yt-dlp setup](https://github.com/janisadhi/music-sync/blob/main/app/core/runtime.py): application core · Centralizes startup, worker lifecycle, and yt-dlp configuration. · `app/core/runtime.py`
- [Settings service](https://github.com/janisadhi/music-sync/blob/main/app/settings/service.py): domain service · Provides persistent scheduling, retry, directory, and cookie settings. · `app/settings/service.py`
- [Sync scheduler](https://github.com/janisadhi/music-sync/blob/main/app/scheduler/service.py): APScheduler service · Triggers periodic synchronization independently of workers. · `app/scheduler/service.py`

### Sync and enrichment workers

- [Sync orchestration](https://github.com/janisadhi/music-sync/blob/main/app/sync/service.py): sync service · Reads configured playlists and drives discovery reconciliation. · `app/sync/service.py`
- [YouTube playlist discovery](https://github.com/janisadhi/music-sync/blob/main/app/watcher/youtube.py): yt-dlp discovery · Uses flat extraction to obtain remote playlist and track state. · `app/watcher/youtube.py`
- [Playlist reconciler](https://github.com/janisadhi/music-sync/blob/main/app/reconciler/service.py): reconciliation service · Creates or updates database-backed work candidates. · `app/reconciler/service.py`
- [Download worker](https://github.com/janisadhi/music-sync/blob/main/app/downloader/worker.py): async worker · Drains eligible work and records outcomes and retry state. · `app/downloader/worker.py`
- [Media downloader](https://github.com/janisadhi/music-sync/blob/main/app/downloader/service.py): yt-dlp service · Retrieves native or high-quality Opus audio. · `app/downloader/service.py`
- [Lyrics worker](https://github.com/janisadhi/music-sync/blob/main/app/lyrics/worker.py): async worker · Fetches timed lyrics after download. · `app/lyrics/worker.py`
- [Metadata service](https://github.com/janisadhi/music-sync/blob/main/metadata_service/processor.py): library processor · Matches metadata, writes tags/artwork, and organizes local media. · `metadata_service/processor.py`

### Persistent storage

- [PostgreSQL state](https://github.com/janisadhi/music-sync/blob/main/app/database/models.py): SQLAlchemy models · Source of truth for users, playlists, songs, work state, and settings. · `app/database/models.py`
- [Mounted media library](https://github.com/janisadhi/music-sync/blob/main/app/core/paths.py): filesystem storage · Persists audio, artwork, and .lrc files. · `app/core/paths.py`
- [Schema migrations](https://github.com/janisadhi/music-sync/blob/main/alembic/env.py): Alembic · `alembic/env.py`

### Deployment

- [Docker Compose stack](https://github.com/janisadhi/music-sync/blob/main/docker-compose.yml): container orchestration · Runs PostgreSQL, backend, and dashboard services. · `docker-compose.yml`
- [Backend runtime image](https://github.com/janisadhi/music-sync/blob/main/Dockerfile): Docker image · Supplies yt-dlp, Deno, FFmpeg, and tagging tools. · `Dockerfile`
- [Container deployment CI](https://github.com/janisadhi/music-sync/blob/main/.github/workflows/deploy.yaml): GitHub Actions · Builds and deploys container images to GHCR. · `.github/workflows/deploy.yaml`

## Connections

- React management SPA → Audio player: renders
- React management SPA → Nginx dashboard server: served by
- Nginx dashboard server → FastAPI REST API: proxies API
- FastAPI REST API → Settings service: manages
- FastAPI REST API → Sync orchestration: starts sync
- FastAPI REST API → PostgreSQL state: reads and writes
- Runtime and yt-dlp setup → FastAPI REST API: starts
- Runtime and yt-dlp setup → Download worker: runs
- Runtime and yt-dlp setup → Lyrics worker: runs
- Settings service → PostgreSQL state: persists settings
- Sync scheduler → Sync orchestration: triggers
- Settings service → Sync scheduler: controls
- Sync orchestration → YouTube playlist discovery: discovers playlists
- YouTube playlist discovery → Playlist reconciler: remote track state
- Playlist reconciler → PostgreSQL state: creates work candidates
- Download worker → PostgreSQL state: claims and updates work
- Download worker → Media downloader: retrieves media
- Media downloader → Mounted media library: writes audio
- Lyrics worker → PostgreSQL state: finds downloaded tracks
- Lyrics worker → Mounted media library: writes .lrc files
- Metadata service → Mounted media library: tags, artworks, organizes
- Schema migrations → PostgreSQL state: evolves schema
- Docker Compose stack → PostgreSQL state: runs
- Docker Compose stack → Runtime and yt-dlp setup: runs backend
- Docker Compose stack → Nginx dashboard server: runs dashboard
- Backend runtime image → Runtime and yt-dlp setup: provides runtime dependencies
- Container deployment CI → Backend runtime image: builds and deploys
