incognitolm commited on
Commit ·
c239331
1
Parent(s): fd152e6
Create searchWorker.js
Browse files- server/searchWorker.js +31 -0
server/searchWorker.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// searchWorker.js — runs inside a worker_thread, fully isolated from the
|
| 2 |
+
// main WS server. Receives a query via workerData, performs the Gradio
|
| 3 |
+
// search, posts the result back via parentPort, then exits.
|
| 4 |
+
//
|
| 5 |
+
// Because this runs in its own V8 context / event loop, the Gradio client's
|
| 6 |
+
// internal SSE fetch stream cannot interfere with the main thread's ws server.
|
| 7 |
+
|
| 8 |
+
import { workerData, parentPort } from 'worker_threads';
|
| 9 |
+
import { Client } from '@gradio/client';
|
| 10 |
+
|
| 11 |
+
async function run() {
|
| 12 |
+
const { query } = workerData;
|
| 13 |
+
let client = null;
|
| 14 |
+
try {
|
| 15 |
+
client = await Client.connect('incognitolm/Web-Search');
|
| 16 |
+
const result = await client.predict('/perform_search', { query });
|
| 17 |
+
const raw = Array.isArray(result.data) ? result.data[0] : result.data;
|
| 18 |
+
if (!raw) throw new Error('Empty response from search endpoint');
|
| 19 |
+
const text = typeof raw === 'string' ? raw : JSON.stringify(raw);
|
| 20 |
+
parentPort.postMessage({ ok: true, result: text });
|
| 21 |
+
} catch (err) {
|
| 22 |
+
parentPort.postMessage({ ok: false, error: String(err) });
|
| 23 |
+
} finally {
|
| 24 |
+
try { client?.close?.(); } catch (_) {}
|
| 25 |
+
// Force-exit so the worker doesn't hang on lingering async handles
|
| 26 |
+
// (the SSE response body reader, heartbeat timer, etc.)
|
| 27 |
+
setTimeout(() => process.exit(0), 0);
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
run();
|