wrolpi/scripts
lrnselfreliance 97a6566e3d
Fix API stalls: non-blocking switch worker, threaded searches, indexed Videos page (#678)
* Fix API stalls: non-blocking switch worker, threaded searches, indexed Videos page

Three response-time fixes found by benchmarking the read-only API endpoints
hot and cold on real libraries:

* `switch_worker` runs on the Sanic worker's event loop every 0.1s and called
  the blocking `multiprocessing.Event.wait(timeout=1)`.  With no switch pending
  (the normal state) this froze the loop for a second at a time; about a third
  of all requests, including trivial ones, took ~1,000 ms.  It now polls
  `is_set()` and returns immediately.

* The search handlers (videos, files, archives, docs, zims) and `/api/statistics`
  ran synchronous SQLite queries on the event loop, so one slow search stalled
  every other request on that worker.  They now run via `asyncio.to_thread`.

* The unfiltered Videos page (`POST /api/videos/search` with only an order)
  used `mimetype LIKE 'video/%' OR ...` plus `COUNT(*) OVER()`, which visits
  every video's index entry before LIMIT applies: seconds cold on a large
  library on a spinning disk, and ~10 s hot for a deep page.  It now seeks each
  exact mimetype on the (mimetype, effective_datetime) index for `limit + offset`
  rows and merges them, with the total from a separate count cached for 60 s.
  Results are identical to the generic query.

Also adds `scripts/api_perf.py` (times every read-only endpoint against a host,
optionally restarting the API over SSH for cold numbers) and
`scripts/api_perf_compare.py` (merges per-host results).  Host profiles live in a
gitignored JSON file; an example is included.

Measured on an 8-core docker host with a 10 GB library: median hot request
1,007 ms -> 22 ms; Videos page 1 hot 287 -> 108 ms, page 50 1,345 -> 120 ms.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Serve the video description from its own endpoint instead of Video.__json__

`Video.__json__` computed `description` as `c_text or get_video_description()`,
where the fallback opens and parses the video's info json from disk.  The list
endpoints serialize every result with `__json__`, so any page containing videos
without an indexed description (common: many videos have none) read their info
json files on every request, cold, for a field the list never displays.  The
description is not part of search either: search runs against the FTS copy of
`c_text`, so the fallback never fed the index.

Drop `description` from `__json__` and serve it from
`GET /api/videos/<id>/description`, like comments and captions.  The endpoint
returns the indexed `c_text`, falling back to the info json only there, for a
row indexed before descriptions were stored.  The player page fetches it through
`useVideoExtras` alongside the comments.

Tests assert the list and detail responses never touch the info json, and cover
the endpoint's indexed, fallback, and not-found cases.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Address review: thread-local zim sessions, fast-path equivalence test, comments

* Zim search, single-zim search, and search estimates run libzim off the event
  loop, each opening its own session inside the worker thread rather than
  passing the request's SQLAlchemy Session across threads.
* The Videos fast-path test now runs the same requests through the generic
  query and asserts identical rows and paging for both date orders.  The one
  intentional difference is documented: past the end, the fast path still
  reports the true total.
* switch_worker returns before its try block so the empty path does not clear
  the event; its comment states the invariant only.  The copied "synchronous
  SQLite" comments are reduced to one line in one place.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-29 16:17:53 -06:00
..
api_perf.py Fix API stalls: non-blocking switch worker, threaded searches, indexed Videos page (#678) 2026-08-29 16:17:53 -06:00
api_perf_compare.py Fix API stalls: non-blocking switch worker, threaded searches, indexed Videos page (#678) 2026-08-29 16:17:53 -06:00
api_perf_hosts.example.json Fix API stalls: non-blocking switch worker, threaded searches, indexed Videos page (#678) 2026-08-29 16:17:53 -06:00
build_app.sh Bake the app build into images; rebuild on-device only when stale (#536) 2026-07-15 15:19:25 -06:00
check_database_integrity.sh check_database_integrity.sh: address greptile review 2026-07-22 19:47:48 -06:00
cleanup_orphaned_tags.py Creating script to clean up orphaned tag files. 2026-02-28 17:05:02 -07:00
delete_downloads.py Many fixes split out from the feature/unified-query branch. Installing diagnostic tools in the built images. Displaying video comment statistics. Echoing error when install.sh fails. 2024-04-14 13:13:30 -06:00
delete_similarly_named_videos.py scripts: set exec bit on delete_similarly_named_videos.py 2026-07-22 19:47:35 -06:00
dev_build.sh Postgres -> SQLite hard cutover: the database moves into the media directory. 2026-07-12 14:09:37 -06:00
find_and_convert_srt_to_vtt.py Adding option to ignore errors in find_and_convert_srt_to_vtt.py. 2023-08-04 08:36:30 -06:00
find_and_rename_orphan_video_files.py Bumping version for new feature. 2022-12-07 18:15:28 -07:00
find_corrupt_videos.py Postgres -> SQLite hard cutover: the database moves into the media directory. 2026-07-12 14:09:37 -06:00
fix_media_permissions.sh File permissions fix script checks for exfat and other file systems before trying to repair, displays a warning and gives advice on how to fix the issue. 2026-03-05 18:18:53 -07:00
generate_certificates.sh Certificates: only include SANs permitted by the Root CA name constraints. 2026-07-05 12:38:08 -06:00
generate_subtitles.py Creating script which finds all SRT files and converts them to VTT. 2023-08-03 22:36:11 -06:00
install_debian_12.sh install: always install serve; resolve it via PATH in wrolpi-app 2026-07-23 18:21:58 -06:00
install_help_service.sh Replacing Tag names with escaped tag names to avoid errors in Tags Directory. 2024-10-16 18:56:10 -06:00
install_protomaps.sh Remove stale Postgres references after SQLite cutover. 2026-07-18 13:28:54 -06:00
install_raspberrypios.sh install: always install serve; resolve it via PATH in wrolpi-app 2026-07-23 18:21:58 -06:00
renew_certificates.sh Certificates: help.sh validates the CA and leaf certs; fix renew exec bit. 2026-07-09 16:00:43 -06:00
reset_api_db.sh Postgres -> SQLite hard cutover: the database moves into the media directory. 2026-07-12 14:09:37 -06:00
reset_tags.sh Postgres -> SQLite hard cutover: the database moves into the media directory. 2026-07-12 14:09:37 -06:00
reset_virtual_environments.sh upgrade: rebuild venvs after a Python major-version change 2026-07-20 09:08:30 -06:00
singlefile_browser_wrapper.sh Single file deno (#478) 2026-06-12 17:07:28 -06:00
singlefile_to_archive.py A Channel can be tagged with a single Tag. The Channel can be moved into a Tag Directory according to the config. 2024-10-16 18:56:19 -06:00
start_kiwix_serve.sh zim: address PR review — config race, YAML null, path traversal, pinned build 2026-07-20 19:10:09 -06:00
upgrade.sh upgrade: rebuild venvs after a Python major-version change 2026-07-20 09:08:30 -06:00
upgrade_yt-dlp.sh Creating script which updates yt-dlp in the wrolpi venv to the latest available. 2025-01-23 17:46:37 -07:00
wrolpi-usb.sh Docs: recommend Raspberry Pi Imager instead of Etcher. 2026-07-03 18:58:54 -06:00