18 KiB
SQLite DB Worker
This document records the current non-EPG SQLite worker implementation in the Electron app.
Related:
Summary
- Heavy non-EPG SQLite work no longer runs on Electron's main thread.
- A dedicated long-lived database worker now handles the slow Xtream and playlist database operations that were freezing the UI.
- Renderer APIs stay stable. The main change is that progress and long-running
state now flow through a request-scoped
DB_OPERATION_EVENTcontract instead of a single global progress event.
Goals
The worker cutover addresses three concrete problems:
- Main-process UI stalls during large SQLite operations.
- Xtream import progress events were global and unsafe for concurrent jobs.
- EPG and non-EPG writers needed shared SQLite concurrency settings so they
can coexist without
SQLITE_BUSYregressions.
Current Ownership
Main-process runtime wiring
These files own worker lifecycle and IPC bridging:
apps/electron-backend/src/app/services/database-worker-client.tsapps/electron-backend/src/app/events/database/category.events.tsapps/electron-backend/src/app/events/database/content.events.tsapps/electron-backend/src/app/events/database/playlist.events.tsapps/electron-backend/src/app/events/database/xtream.events.tsapps/electron-backend/src/main.ts
Worker runtime
These files own the worker protocol and the SQLite work itself:
apps/electron-backend/src/app/workers/database-worker.types.tsapps/electron-backend/src/app/workers/database.worker.tsapps/electron-backend/src/app/workers/database.worker-connection.tsapps/electron-backend/src/app/workers/worker-runtime-paths.ts
Pure database operation modules
Keep SQL-heavy logic here so the worker entry remains a thin dispatcher:
apps/electron-backend/src/app/database/operations/category.operations.tsapps/electron-backend/src/app/database/operations/content.operations.tsapps/electron-backend/src/app/database/operations/playlist.operations.tsapps/electron-backend/src/app/database/operations/xtream.operations.ts
Worker Architecture
Request flow
- Renderer calls the existing preload API such as
window.electron.dbSaveContent. ipcMain.handle(...)in the Electron backend builds a payload and delegates toDatabaseWorkerClient.DatabaseWorkerClientlazily starts one long-livedworker_threadsworker and correlates requests with a generatedrequestId.- The worker executes SQLite work and sends back either:
readyeventresponse
- The main process resolves the IPC request and forwards worker events back to the originating renderer process.
Why one long-lived worker
- It avoids worker startup cost on every search/delete/import.
- It centralizes failure handling and restart behavior.
- It mirrors the existing EPG worker approach without multiplying writable SQLite owners.
Packaged worker bootstrap
Packaged Electron builds do not load worker scripts and native modules from the same place:
- worker scripts live under
Resources/dist/apps/electron-backend/workers - unpacked native modules live under one of the approved
app.asar.unpacked/.../node_moduleslocations
Both the EPG worker and the DB worker now share the same runtime helper:
resolveWorkerRuntimeBootstrap(...)for main-process worker launchloadNativeModuleFromSearchPaths(...)for worker-side native module loading
The helper uses process.resourcesPath as the primary packaged base and keeps
path.dirname(app.getAppPath()) only as a fallback.
Worker Message Contract
The worker contract lives in
apps/electron-backend/src/app/workers/database-worker.types.ts.
Core message types
DbWorkerRequestMessageDbWorkerResponseMessageDbWorkerEventMessageDbOperationEvent
Progress event contract
The worker now emits request-scoped events with:
operationIdoperationplaylistIdstatus- optional
phase - optional
current - optional
total - optional
increment
Current shipped operation names:
save-contentdelete-xtream-contentrestore-xtream-user-datadelete-playlistdelete-all-playlists
The event is forwarded to the renderer as DB_OPERATION_EVENT.
Cancellation contract
Long-running Xtream and playlist operations now support best-effort cancellation.
Renderer requests cancellation via:
DB_CANCEL_OPERATIONwindow.electron.dbCancelOperation(operationId)DatabaseService.cancelOperation(operationId)
If a worker operation is canceled:
- the worker emits a final
cancelledevent - the request rejects with an
AbortError - the UI clears its busy state without treating the operation as success
Cancellation is cooperative and chunk-based. Already committed SQLite batches stay committed.
Renderer Contract
The preload bridge keeps the existing database methods but adds scoped worker events.
Important preload APIs
onDbOperationEvent(callback)dbSaveContent(playlistId, streams, type, operationId?)dbDeleteXtreamContent(playlistId, operationId?)dbRestoreXtreamUserData(..., operationId?)dbDeletePlaylist(playlistId, operationId?)dbDeleteAllPlaylists(operationId?)dbCancelOperation(operationId)- legacy compatibility:
onDbSaveContentProgress(callback)removeDbSaveContentProgress()
DatabaseService.saveXtreamContent(...) now generates an operationId,
subscribes to onDbOperationEvent, filters by that operationId, and only
falls back to the legacy progress API if the newer event channel is missing.
DatabaseService also owns:
createOperationId(...)cancelOperation(operationId)isDbAbortError(error)
Migrated Operations
The worker now owns all heavy non-EPG SQLite paths plus the remaining portal state handlers that still used direct main-thread SQLite access.
Categories
DB_HAS_CATEGORIESDB_GET_CATEGORIESDB_SAVE_CATEGORIESDB_GET_ALL_CATEGORIESDB_UPDATE_CATEGORY_VISIBILITY
Content
DB_HAS_CONTENTDB_GET_CONTENTDB_SAVE_CONTENTDB_GET_CONTENT_BY_XTREAM_IDDB_SEARCH_CONTENTDB_GLOBAL_SEARCHDB_GET_GLOBAL_RECENTLY_ADDED
Playlist metadata
DB_CREATE_PLAYLISTDB_UPSERT_APP_PLAYLISTDB_UPSERT_APP_PLAYLISTSDB_GET_APP_PLAYLISTSDB_GET_APP_PLAYLISTDB_GET_PLAYLISTDB_UPDATE_PLAYLISTDB_DELETE_PLAYLISTDB_DELETE_ALL_PLAYLISTSDB_GET_APP_STATEDB_SET_APP_STATE
Xtream refresh helpers
DB_DELETE_XTREAM_CONTENTDB_RESTORE_XTREAM_USER_DATA
Favorites
DB_ADD_FAVORITEDB_REMOVE_FAVORITEDB_IS_FAVORITEDB_GET_FAVORITESDB_GET_GLOBAL_FAVORITESDB_GET_ALL_GLOBAL_FAVORITESDB_REORDER_GLOBAL_FAVORITES
Recently viewed
DB_GET_RECENTLY_VIEWEDDB_CLEAR_RECENTLY_VIEWEDDB_GET_RECENT_ITEMSDB_ADD_RECENT_ITEMDB_CLEAR_PLAYLIST_RECENT_ITEMSDB_REMOVE_RECENT_ITEM
Playback positions
DB_SAVE_PLAYBACK_POSITIONDB_GET_PLAYBACK_POSITIONDB_GET_SERIES_PLAYBACK_POSITIONSDB_GET_RECENT_PLAYBACK_POSITIONSDB_GET_ALL_PLAYBACK_POSITIONSDB_CLEAR_PLAYBACK_POSITION
SQLite Concurrency Rules
EPG remains on its own worker, so both workers must use compatible SQLite pragmas.
Applied now in both the shared connection path and worker-owned connections:
foreign_keys = ONjournal_mode = WALbusy_timeout = 5000
Current sources:
libs/shared/database/src/lib/connection.tsapps/electron-backend/src/app/workers/database.worker-connection.tsapps/electron-backend/src/app/workers/epg-parser.worker.ts
UI Behavior Changes
Search
Xtream search now guards against stale async responses:
- local playlist search uses a monotonically increasing request version
- global search uses a separate request version in the dialog component
- clearing search invalidates older pending results
This prevents an older worker response from repainting over a newer query or a cleared search state.
Xtream type-aware content lookup
Xtream UI flows must treat xtream_id as only partially unique.
Current contract:
xtream_idcan collide acrosslive,movie, andserieswithin the same playlist.- Any DB-backed lookup that starts from an Xtream result card, favorite button,
recent-item update, continue-watching flow, or detail route must resolve
content by:
playlist_idxtream_idcontent.type
- Mixed Xtream collection identity must key entries by
type + xtream_id, notxtream_idalone. This includes favorites maps, recent-item lists, dashboard collection payloads, and other UI state keyed off persisted Xtream content.
Why this matters:
- Search results are already type-filtered, so resolving favorites by only
playlist_id + xtream_idcan favorite the wrong persisted row when IDs collide. - Continue-watching / recently-viewed flows that resolve by only
playlist_id + xtream_idcan store the wrong persisted row when a series or movie ID collides with a live entry. - Mixed favorites maps keyed only by
xtream_idcan mark an unrelated live row as favorited when the actual favorite is a movie or series with the same numeric ID. - Mixed recent/favorites collection items keyed only by
xtream_idcan cause local UI state to remove, reorder, or reactivate the wrong Xtream entry when different content types collide.
Current implementation paths:
apps/electron-backend/src/app/database/operations/content.operations.tslibs/portal/xtream/data-access/src/lib/with-favorites.feature.tslibs/portal/xtream/data-access/src/lib/with-recent-items.tslibs/portal/xtream/feature/src/lib/portal-channels-list/portal-channels-list.component.tslibs/portal/shared/util/src/lib/collection/unified-recent-data.service.tslibs/portal/shared/util/src/lib/collection/unified-favorites-data.service.ts
Busy states
The UI now has explicit long-running state for destructive operations:
- recent playlist rows show row-level refresh/delete spinners
- Xtream import overlay shows phase text and a cancel action
- Xtream playlist rows show request-scoped progress and cancel actions
- busy rows block repeat clicks while an operation is in flight
- settings "remove all playlists" owns its own spinner/disabled state and consumes request-scoped DB operation events for progress text while the worker deletes playlist data
These changes matter because once SQLite work leaves the main thread, the renderer can actually paint the loading state instead of freezing.
Build And Packaging Notes
Worker bundling
apps/electron-backend/build-worker.js now bundles both:
epg-parser.worker.tsdatabase.worker.ts
The worker build also aliases:
database-schemadatabase-path-utils
These aliases avoid importing the shared database barrel from inside the worker, which would otherwise pull in runtime code that assumes the main Electron process environment.
Worker path resolution
DatabaseWorkerClient resolves:
- development path from
__dirname - packaged path from
process.resourcesPath/dist/apps/electron-backend/workers/... - fallback packaged path from
path.dirname(app.getAppPath())
Packaged artifact verification
tools/packaging/verify-electron-package-layout.mjs verifies packaged worker
artifacts for:
- Linux unpacked resources
- macOS app bundles
- Windows unpacked app resources
The script checks:
epg-parser.worker.jsdatabase.worker.jsbetter-sqlite3in one approved unpacked node_modules location- Snap packaging compatibility settings for
better-sqlite3:snap.base = core22- Snap launch args keep the X11 fallback
- Snap and the other non-Flatpak Linux artifacts build on Ubuntu 22.04, while Flatpak builds on a separate Ubuntu 24.04 CI runner
Development rebuild rule
Worker-backed database logic is executed from the compiled bundle at:
dist/apps/electron-backend/workers/database.worker.js
Do not assume a source edit is active in the live app. If a fix touches:
apps/electron-backend/src/app/database/operations/apps/electron-backend/src/app/workers/apps/electron-backend/src/app/events/database/- preload-backed DB methods consumed by the renderer
then the safe workflow is:
- rebuild the worker bundle, or the full
electron-backendtarget if preload, main-process, or web output also changed - confirm the new
dist/artifact exists or has a fresh timestamp - restart the Electron process
- only then rerun CDP/manual checks or Electron E2E
A running Electron app keeps using the worker bundle it already loaded at startup. This is a common reason a worker fix appears "not working" in manual verification even when the source patch is correct.
Testing
Unit coverage added
apps/electron-backend/src/app/services/database-worker-client.spec.ts
covers:
- worker ready -> request -> response flow
- event forwarding to a pending request
- serialized worker error propagation
AbortErrorpropagation for cancelled work- cancel message routing to the live worker
- worker exit recovery and fresh worker startup
apps/electron-backend/src/app/events/epg.events.spec.ts covers:
- shared worker bootstrap usage for the EPG worker
nativeModuleSearchPathsforwarding into workerData- actionable worker-path resolution failures
apps/electron-backend/src/app/workers/worker-runtime-paths.spec.ts covers:
- packaged and development worker path resolution
- packaged native-module search path ordering
- aggregated native module resolution errors
Electron responsiveness coverage
apps/electron-backend-e2e/src/xtream-responsiveness.e2e.ts covers:
- large Xtream import shows the overlay promptly
- DB worker progress events advance during import
- renderer animation frames continue while import/delete are in progress
- large Xtream playlist delete shows row-level busy UI and completes cleanly
apps/electron-backend-e2e/src/electron-test-fixtures.ts now also captures:
DB_OPERATION_EVENThistory in the renderer- a requestAnimationFrame counter for repaint assertions
For deterministic E2E timing, tests may set:
IPTVNATOR_DB_WORKER_BATCH_DELAY_MS=20
This delay is test-only and disabled by default.
Useful verification commands
pnpm exec jest --config apps/electron-backend/jest.config.ts --runInBand apps/electron-backend/src/app/services/database-worker-client.spec.ts apps/electron-backend/src/app/events/epg.events.spec.ts apps/electron-backend/src/app/workers/worker-runtime-paths.spec.ts
pnpm nx run electron-backend:build-worker
pnpm exec tsc -p apps/electron-backend/tsconfig.app.json --noEmit
pnpm exec tsc -p apps/web/tsconfig.app.json --noEmit
pnpm nx run electron-backend-e2e:e2e -- --project=electron --grep "Electron Xtream Responsiveness"
pnpm run verify:package-layout -- macos arm64
pnpm run verify:package-layout -- linux
pnpm run verify:package-layout -- windows
Electron runtime validation
pnpm nx serve electron-backend
agent-browser --cdp 9222 tab list
agent-browser --cdp 9222 tab 1
agent-browser --cdp 9222 snapshot -i -c -d 3
pnpm run smoke:packaged -- macos arm64
When worker-backed behavior changed, rebuild and restart before reconnecting:
pnpm nx run electron-backend:build-worker
CI=1 NX_TASKS_RUNNER_DYNAMIC_OUTPUT=false pnpm nx run electron-backend:build --skip-nx-cache
stat -f "%Sm %N" dist/apps/electron-backend/workers/database.worker.js
Then restart the Electron process and reconnect to 127.0.0.1:9222.
Electron freeze tracing
When a renderer route freezes before DevTools become usable, start Electron with one of these opt-in trace flags and inspect the terminal output:
IPTVNATOR_TRACE_STARTUP=1 pnpm run serve:backend
Available trace flags:
IPTVNATOR_TRACE_STARTUP=1Enables the broad startup trace set: BrowserWindow lifecycle, renderer bridge calls, DB worker requests/events, and SQL tracing.IPTVNATOR_TRACE_IPC=1Logswindow.electron.*method calls crossing the preload bridge so you can see whether the renderer is still reaching Electron main.IPTVNATOR_TRACE_DB=1LogsDatabaseWorkerClientrequest dispatch, completion timing, and emittedDB_OPERATION_EVENTpayloads.IPTVNATOR_TRACE_SQL=1Logs SQLite statements for the shared main-process connection and the DB worker connection usingbetter-sqlite3verbose hooks.IPTVNATOR_TRACE_WINDOW=1Logs BrowserWindow loading, navigation,unresponsive, andrender-process-gonetransitions.IPTVNATOR_TRACE_RENDERER_CONSOLE=1Mirrors renderer console messages into the Electron terminal output when the renderer itself is the thing getting wedged.
Electron E2E troubleshooting
If a production-mode Electron or Electron E2E launch shows ERR_FILE_NOT_FOUND
for hashed chunk-*.js, main-*.js, or styles-*.css assets:
- treat
dist/apps/webas stale first - rerun a deterministic production build, for example:
CI=1 NX_TASKS_RUNNER_DYNAMIC_OUTPUT=false pnpm nx run electron-backend:build --skip-nx-cache
- verify
dist/apps/web/index.htmluses<base href="./">before relaunching Electron in file-backed mode
Current Limitations
These are intentionally still out of scope for this first cut:
- request cancellation
- moving network-heavy Xtream fetches off the current path
- migrating every remaining small SQLite IPC handler to the worker
- richer delete progress reporting for bulk destructive operations
- repo-wide Angular/Jest cleanup for the currently failing web test baseline
Extending The Worker
When adding another heavy SQLite operation:
- Put SQL-heavy logic in
apps/electron-backend/src/app/database/operations/. - Add the channel name to
database-worker.types.ts. - Handle it in
database.worker.ts. - Proxy the IPC handler through
DatabaseWorkerClient. - If the renderer needs progress, emit a request-scoped
DbOperationEvent. - Reuse existing preload/service APIs where possible instead of creating a new renderer-facing contract.
- Re-run the worker unit test and at least one Electron runtime smoke.