MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / runPragmasOffThread

Method runPragmasOffThread

src/db/index.ts:622–654  ·  view source on GitHub ↗

* 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[] = [])

Source from the content-addressed store, hash-verified

620 * that are safe to run synchronously on the main thread.
621 */
622 private async runPragmasOffThread(pragmas: string[], inlineFallback: string[] = []): Promise<void> {
623 try {
624 const { Worker } = await import('node:worker_threads');
625 const workerSource = `
626 const { workerData, parentPort } = require('node:worker_threads');
627 try {
628 const { DatabaseSync } = require('node:sqlite');
629 const db = new DatabaseSync(workerData.dbPath);
630 for (const p of workerData.pragmas) { try { db.exec(p); } catch {} }
631 try { db.close(); } catch {}
632 } catch {}
633 parentPort.postMessage('done');
634 `;
635 await new Promise<void>((resolve) => {
636 let settled = false;
637 const finish = (): void => {
638 if (!settled) { settled = true; resolve(); }
639 };
640 try {
641 const worker = new Worker(workerSource, { eval: true, workerData: { dbPath: this.dbPath, pragmas } });
642 worker.once('message', () => { void worker.terminate(); finish(); });
643 worker.once('error', () => { void worker.terminate(); finish(); });
644 worker.once('exit', finish);
645 } catch {
646 finish();
647 }
648 });
649 } catch {
650 for (const p of inlineFallback) {
651 try { this.db.exec(p); } catch { /* ignore */ }
652 }
653 }
654 }
655
656 /**
657 * Close the database connection

Callers 1

runMaintenanceMethod · 0.95

Calls 3

terminateMethod · 0.65
execMethod · 0.65
finishFunction · 0.50

Tested by

no test coverage detected