* Run pragmas on a worker thread against its own connection to this DB * (shared machinery for runMaintenance and * checkpointWalPassive). Each pragma is individually best-effort; * the whole call is best-effort. `inlineFallback` (if any) runs on THIS * connection only wh
(pragmas: string[], inlineFallback: string[] = [])
| 699 | * that are safe to run synchronously on the main thread. |
| 700 | */ |
| 701 | private async runPragmasOffThread(pragmas: string[], inlineFallback: string[] = []): Promise<void> { |
| 702 | try { |
| 703 | const { Worker } = await import('node:worker_threads'); |
| 704 | const workerSource = ` |
| 705 | const { workerData, parentPort } = require('node:worker_threads'); |
| 706 | try { |
| 707 | const { DatabaseSync } = require('node:sqlite'); |
| 708 | const db = new DatabaseSync(workerData.dbPath); |
| 709 | for (const p of workerData.pragmas) { try { db.exec(p); } catch {} } |
| 710 | try { db.close(); } catch {} |
| 711 | } catch {} |
| 712 | parentPort.postMessage('done'); |
| 713 | `; |
| 714 | await new Promise<void>((resolve) => { |
| 715 | let settled = false; |
| 716 | const finish = (): void => { |
| 717 | if (!settled) { settled = true; resolve(); } |
| 718 | }; |
| 719 | try { |
| 720 | const worker = new Worker(workerSource, { eval: true, workerData: { dbPath: this.dbPath, pragmas } }); |
| 721 | worker.once('message', () => { void worker.terminate(); finish(); }); |
| 722 | worker.once('error', () => { void worker.terminate(); finish(); }); |
| 723 | worker.once('exit', finish); |
| 724 | } catch { |
| 725 | finish(); |
| 726 | } |
| 727 | }); |
| 728 | } catch { |
| 729 | for (const p of inlineFallback) { |
| 730 | try { this.db.exec(p); } catch { /* ignore */ } |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Close the database connection |
no test coverage detected