* 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[] = [])
| 926 | * that are safe to run synchronously on the main thread. |
| 927 | */ |
| 928 | private async runPragmasOffThread(pragmas: string[], inlineFallback: string[] = []): Promise<void> { |
| 929 | try { |
| 930 | const { Worker } = await import('node:worker_threads'); |
| 931 | const workerSource = ` |
| 932 | const { workerData, parentPort } = require('node:worker_threads'); |
| 933 | try { |
| 934 | const { DatabaseSync } = require('node:sqlite'); |
| 935 | const db = new DatabaseSync(workerData.dbPath); |
| 936 | for (const p of workerData.pragmas) { try { db.exec(p); } catch {} } |
| 937 | try { db.close(); } catch {} |
| 938 | } catch {} |
| 939 | parentPort.postMessage('done'); |
| 940 | `; |
| 941 | await new Promise<void>((resolve) => { |
| 942 | let settled = false; |
| 943 | const finish = (): void => { |
| 944 | if (!settled) { settled = true; resolve(); } |
| 945 | }; |
| 946 | try { |
| 947 | const worker = new Worker(workerSource, { eval: true, workerData: { dbPath: this.dbPath, pragmas } }); |
| 948 | worker.once('message', () => { void worker.terminate(); finish(); }); |
| 949 | worker.once('error', () => { void worker.terminate(); finish(); }); |
| 950 | worker.once('exit', finish); |
| 951 | } catch { |
| 952 | finish(); |
| 953 | } |
| 954 | }); |
| 955 | } catch { |
| 956 | for (const p of inlineFallback) { |
| 957 | try { this.db.exec(p); } catch { /* ignore */ } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | /** |
| 963 | * Give back what this connection holds only to read faster: its page cache, |
no test coverage detected