Skip to content

Background workers

Horde runs several daemon threads alongside the API. None require a separate process manager inside the container.

Lifecycle

Every worker is a BackgroundWorker (or implements the same ManagedWorker protocol: start(), signal_stop(), stop(timeout), is_alive, healthy) from services/workers.py. lifespan creates one WorkerRegistry, keeps it on app.state.workers and starts every worker through it.

On shutdown (SIGTERM from docker stop, Ctrl+C):

  1. Uvicorn stops accepting connections and drains open ones for up to --timeout-graceful-shutdown (10 s). The SSE streams (/api/downloads/events, /api/downloads/{id}/events) watch app.state.shutdown_event, which is set the moment the signal arrives, so they end immediately instead of using up the grace period.
  2. lifespan shutdown logs stopping N background workers, signals every worker first, then joins them one by one, newest first, each with its own timeout (5 s by default, 10 s for the download and ffmpeg queues) and an overall budget of 20 s. Each result is logged at INFO; a worker that does not stop is logged by name at ERROR. Because everything is signalled before anything is joined, independent workers wind down in parallel.
  3. In-flight downloads and postprocess encodes are cancelled and requeued (status queued, not cancelled), so the next boot resumes them. ffmpeg children get SIGTERM, then SIGKILL after 3 s; each runs in its own session so helper processes die with it. A registry entry (ffmpeg-children) ends anything still running last. Every long ffmpeg goes through run_ffmpeg (download postprocess, H.264 copies, transcodes, sprite sheets, thumbnail frames), so all of them are cancellable this way; only short ffprobe-style probes (30 s or less) use plain subprocess.run.

Worst-case stop time is the 10 s drain plus the 20 s join budget, so 30 s; a normal stop takes 1-3 s. The Compose file allows 40 s (stop_grace_period). With plain docker run the default is only 10 s, so pass --stop-timeout 40 (or more), or Docker kills the process before the workers finish.

If startup fails part way, the finally still stops the workers that already started.

The registry also feeds GET /api/ready and the horde_worker_up gauge on GET /api/metrics.

Worker (registry name) Notes
ffmpeg-children Terminates ffmpeg processes still running at shutdown.
download-queue, ffmpeg-queue, download-reaper Download threads (bounded by MAX_DOWNLOAD_CONCURRENCY), the postprocess pool and the stuck-job reaper (which also re-checks held downloads every 30 s).
sprites One bounded pool of SPRITE_CONCURRENCY threads fed from a queue; a bulk import queues entries, not threads. Stopping the pool cancels the running ffmpeg. Sprite ffmpeg (and the scanner's thumbnail grab) runs under nice -n 15 where nice exists, so it yields CPU to playback and streaming on small servers.
scanner-watchdog, scanner-poller File watcher and the polling walk.
metadata-sync, horde-ai-worker, horde-ai-timer, horde-channel-catalog, autodownload-poller, subtitle-retry, ask, comments, stream-ai, discovery, discovery-rank, job-metadata, trash-purge, channel-retention, notifications, media-sidecars, codec-probe-backfill, pot-probe, nightly-backup The loops described below.

Short-lived threads started by a request (bulk metadata sync, playlist sync, H.264 copies) are not registered; they are daemon threads and every database write they make is transactional.

Scanner

Start: create_scanner_workers() after DB init. The first full walk runs on the poller thread, not during startup, so the API answers while a large library is scanned.

Mode Behavior
Watchdog Recursive observer on DOWNLOADS_DIR for create/move events
Poll Full tree walk every SCAN_INTERVAL_SEC (default 60) as a fallback

Discovers importable video extensions, ignores intermediate .part / .fNNN / .norm files, and coordinates with orphan cleanup. New files may enter the library or the review queue depending on path and settings.

Download queue

Recover: download_queue.recover() on startup — jobs left downloading become queued. Jobs interrupted by a clean shutdown are requeued the same way before the process exits. Global pause is restored from app settings key download_queue_paused so Pause survives process restart.

  • FIFO queue with MAX_DOWNLOAD_CONCURRENCY download workers (default 2) plus a separate ffmpeg pool (MAX_FFMPEG_CONCURRENCY, default 1 or 2 with GPU).
  • Disk limits: before dispatching a library job the queue asks SpaceGate (services/downloads/space.py) whether the library is under max_library_bytes (the number from services/library_usage.py, cached for 5 s) and the volume has at least HORDE_MIN_FREE_GB free. A job that does not fit stays queued and is remembered in memory with a reason (library_full, would_exceed, low_disk, disk_full); later jobs that fit may still start. GET /api/downloads/queue/status, the job list and the progress snapshots carry held_reason. The reaper tick re-checks held jobs every 30 s, and deletes and settings changes call library_changed(). After the metadata fetch the runner compares the preset's estimated size with the limit and returns the job to the queue (DownloadHeld) when it would not fit. An out-of-space write is classified as disk_full and makes the queue hold for five minutes.
  • One multiplex SSE stream (GET /api/downloads/events); per-job streams remain for Watch.
  • Per-job cancel/pause events; pause does not kill an in-flight encode. Progress snapshots for SSE (includes typed error_kind on failure and stage during post-download work).
  • Metadata extracts share the yt-dlp extract gate with preview/feed (1 + 1.25s spacing; interactive preview jumps background fills).
  • See Download pipeline and Troubleshooting.

H.264 copies

compat_rendition.py owns a small thread pool for compat (sidecar) and convert (in-place) jobs. It shares the ffmpeg cap (MAX_FFMPEG_CONCURRENCY, else 2 with a GPU H.264 encoder, otherwise 1). Jobs are idempotent per video, queued in memory, and only their outcome is persisted (compat_path, compat_size, compat_source, compat_error on videos). A viewer-requested copy jumps ahead of a bulk backfill. Queued jobs are lost on restart; Create N copies in Settings → Library re-queues whatever is still missing. Cancel drops queued jobs and signals running ones. Details: H.264 sidecar.

Codec probe backfill

Start: create_probe_backfill_worker() in the app lifespan. After a 10 s delay it ffprobes ready videos whose media_probed_at is empty, 25 at a time with a short pause, and stores the codec columns. It exits when nothing is left; unreadable files (NAS offline, mid-move) are skipped until the next boot.

Job metadata

Start: job_metadata.create_worker() after subtitle retry.

Cheap URL/bulk enqueue does not wait on yt-dlp. This worker fills title, channel, thumbnail, and available_presets (and resolves best to a concrete height). YouTube Shorts discovered after enqueue are dropped (skipped / shorts), not shown as cancelled.

Metadata sync

Start: create_sync_worker(interval_hours=…) from settings (default often 24h).

Each cycle:

  1. Select videos due for resync (metadata_synced_at older than interval).
  2. Process a batch of 20 stale videos.
  3. Refresh stale channel catalogs when enabled.
  4. Rescan subscribed YouTube playlists (flat extract; download missing ids; attach existing library matches; keep YouTube order).
  5. Sleep until the next interval (interval re-read from settings so UI changes apply without restart).

The loop itself ticks about hourly; playlist membership is cheap compared to per-video metadata refresh. Use Sync now on a subscribed playlist if a new part just dropped.

AI worker

Recover: recover_ai_jobs() on startup — any AiJob left running → queued (then wake).

Start: ai.worker.create_workers() (the job runner and its scheduler) — single-flight (one job at a time).

Behavior Detail
Kinds embed_video, enrich_tags, refresh_categories, embed_catalog_video, summarize, chapters
Retries Up to 3 attempts; backoff run_after ≈ 2 × attempts minutes
Pause Settings ai.paused or the AI weekly budget hard stop
Schedule on_download enqueue (plus idle catch-up of missing search indexes) vs timed sweeps
Cleanup The timer deletes completed jobs older than 7 days, hourly; failed and cancelled ones stay for retry and review

Design note: Single-flight AI.

Catalog worker

Recover: recover_catalog_jobs() on startup — catalogs left indexing → queued (then wake).

Start: create_catalog_worker() — one catalog at a time (implementation: services/channel_catalog/ package — runtime, index, query, skips).

Phases per catalog:

flat  →  descriptions  →  embed  →  ready
  1. flat — paginated channel upload list
  2. descriptions — fill descriptions for newest window
  3. embed — enqueue embed_catalog_video jobs for the AI worker, only for videos whose title or description changed since they were embedded

Members-only, age-restricted, and private IDs go to channel_catalog_skips. Runtime status (current channel, phase, counts, last skip/error) is exposed for the Settings/Channels UI.

Autodownload poller

Start: channel_autodownload.create_worker() after the catalog worker.

Every 15 minutes (first pass ~60s after startup), for each channel with autodownload enabled, run a feed-head sync of the newest uploads and enqueue matching videos into the download queue. Enabling autodownload from a channel page also syncs immediately. Jobs are created from catalog metadata (no per-video preview extract). See Channels.

Subtitle retry

Start: subtitle_retry.create_worker() after autodownload.

English captions are fetched after the file is watchable. A timedtext HTTP 429 (or other retryable miss) leaves subtitles_pending set and stores subtitles_retry_after / subtitles_fetch_attempts instead of treating empty VTT as final.

Control Value Purpose
Backoff 5m → 10m → 20m → 40m → 1h Per-video wait after each retryable miss
Global cooldown same delay (max wins) Do not immediately hit timedtext for the next pending video
Poll 15 s Pick one due video (newest download first)
Spacing 15 s between fetches Avoid a caption stampede after the cooldown lifts

Videos added in the last 48 hours that never got a metadata sync and still have empty captions are re-queued on worker start. Watch keeps showing “Subtitles loading” while subtitles_pending is true. Library Resync uses the same helper and skips timedtext while backoff is active.

Stream AI

Start: stream_ai.create_worker() after subtitle retry. Once an hour it moves the saved AI results of streamed videos (stream_ai_state) onto library videos with the same YouTube id (download finalize does this immediately; the sweep covers imports) and deletes results nobody opened for HORDE_STREAM_AI_RETENTION_DAYS (default 30). See ADR 0010.

Start: discovery.gather.create_worker(ctx.discovery) after stream AI. After 90 seconds, and then whenever woken (a person's first visit or a refresh) or every 10 minutes, it runs for each person who opened Recommended in the last 3 days and is due (6 hours since their last run, or asked): up to 4 YouTube searches and 3 up next lists through the extract gate at background priority, at most 30 calls an hour in all, 20 seconds apart, pausing 30 minutes (doubling to 12 hours) after a bot check or rate limit. It then embeds what it found and prunes old hits. Off when discovery_enabled or youtube_video_search is off. See ADR 0019.

Start: discovery.warm.create_worker() after the gatherer (discovery-rank). Ranking a feed scores every candidate, a second or more on a large library, and a first page needs a ranking that is current. This worker makes it ahead of the request: 15 seconds after startup and then every 10 seconds it looks at the 8 people who opened Recommended most recently (in the last 3 days) and ranks today's feed, with its AI categories, for whoever has no current ranking in memory or one that lapses within 30 seconds (rankings are kept 15 minutes). A pass that ranks nothing is a few small queries per person.

It holds back in three cases, and a request then ranks for itself as it always did:

  • Something just changed. Progress is reported every 5 seconds while a library video plays and every 30 while a stream does, and a bulk download adds a video every so often. Each puts the ranking out of date, so a feed is ranked again only after 45 seconds without a change: nothing is ranked while a video plays.
  • It ranked for this person less than 3 minutes ago (or failed to: one person's failure is logged and the pass goes on to the next).
  • The person is paging. A further page was read from the kept ranking in the last 2 minutes, and the next page's offset counts on it staying.

So the request ranks when it comes first: right after a watch, on a new account's first visit, and for any other order (Refresh). A pass that fails as a whole is logged and the next one waits 5 minutes.

Comments

Start: comments.worker.create_worker() after the Ask worker. One minute after startup, then one video per pass: a YouTube video in the library without saved comments (newest added first, and a finished download wakes it), else one whose retry or refresh is due. Each fetch is one yt-dlp page extract in the extract gate at background priority, then the comment pages outside the gate, one request a second, at most one fetch every 30 s: the top 100 comments and up to 10 replies each for the highest-ranked threads (100 replies in all). A rate limit or bot check pauses it for an hour; YouTube out of reach or a busy gate for 10 minutes (not counted as a failure). A failure is retried after 1 h and 4 h, then waits for the next refresh. Comments are fetched again when the video is 7, 30, 182 and 365 days old (from its upload date, else when it was added), skipping ages already past or less than three days away, then every 182 days from the last fetch. Off when comments_enabled (Settings → Library → Comments) is off. See Comments and ADR 0023.

Trash purge

Start: trash.create_worker() after job metadata (trash-purge). Two minutes after start and then every hour it erases trash entries older than HORDE_TRASH_DAYS (default 7; with 0 entries still expire after 7 days), skipping entries whose trash folder cannot be seen (an unmounted share). It also looks at trash folders no entry owns that are over an hour old: it moves their files back when their video is still in the library and otherwise leaves them, never deleting them. It wakes the download queue when disk space was freed. See ADR 0013.

Channel retention

Start: channel_retention.create_worker() after trash purge (channel-retention). Two minutes after start, then every hour (and at once when a rule is saved), it applies every enabled channel_retention rule: the videos a rule selects (and that no exception keeps) go through trash.trash_video with no user and a reason such as "Retention: keep latest 20". A video that cannot be moved is logged and skipped; the rest still go. It does nothing while the trash is off, and it does not wake the download queue: trashed bytes still count against the disk cap. See Cleaning up old videos.

Notifications

Start: ctx.notifier.create_worker() after channel retention (notifications). Emitters (notifications.emit, called from the download pipeline, the queue's disk hold and the live poller) only append to a bounded queue on ctx.notifier; the worker pops it, folds bursts per target and sends with a 10 s timeout and three attempts. Every five minutes it also looks at things nobody emits: the outcome of "Update now" and a new release (the latter only when a target wants it, through the cached update check). See Notifications.

Media-server files

Start: ctx.sidecars.create_worker() after the notifier (media-sidecars). It runs the library-wide jobs of the optional Jellyfin / Kodi / Plex files, one at a time, from a small queue on ctx.sidecars (SidecarJobs in services/media_sidecars.py): the backfill when the setting is turned on, a rewrite of a renamed channel's videos, and the removal of every file. It writes only when the bytes differ, pauses briefly after each video it wrote, stops when the setting is switched off mid-run, and reports done/total under System → Background activity (kind sidecars). Single-video changes (a download finishing, an edit, a thumbnail change, a metadata resync, AI tagging) do not use the queue: they call media_sidecars.refresh(video_id) in the thread that made the change, which logs and swallows its own failures.

Nightly backup

Start: backup.create_backup_worker() unless HORDE_BACKUP_ENABLED=0. Checks every minute; once the configured local hour has passed and today's horde-nightly-YYYYMMDD.db does not exist, it writes it with VACUUM INTO and prunes older nightly files. A failed attempt (disk full, volume gone) is logged and retried an hour later. See Backup & restore.