fix: MH eligibility selenium integration and patient data extraction
This commit is contained in:
@@ -18,50 +18,30 @@ const SELENIUM_BASE_URL =
|
||||
// Python service helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Start an async job on the Python service and return the session ID. */
|
||||
export async function startPythonJob(
|
||||
/**
|
||||
* Call a synchronous Python service endpoint and return its result.
|
||||
* The Python service runs the selenium job inline and returns the result directly.
|
||||
* Timeout is long (8 min) to accommodate slow selenium flows.
|
||||
*/
|
||||
export async function callPythonSync(
|
||||
endpoint: string,
|
||||
payload: any
|
||||
): Promise<string> {
|
||||
payload: any,
|
||||
timeoutMs = 8 * 60 * 1000
|
||||
): Promise<any> {
|
||||
const resp = await axios.post(
|
||||
`${SELENIUM_BASE_URL}${endpoint}`,
|
||||
payload,
|
||||
{ timeout: 10_000 }
|
||||
{ timeout: timeoutMs }
|
||||
);
|
||||
const sid: string = resp.data?.session_id;
|
||||
if (!sid) throw new Error(`Python service did not return a session_id from ${endpoint}`);
|
||||
return sid;
|
||||
}
|
||||
|
||||
/** Poll /job/<sid>/status until completed/failed or timeout. */
|
||||
export async function pollPythonJob(
|
||||
sid: string,
|
||||
timeoutMs = 5 * 60 * 1000,
|
||||
intervalMs = 2_000
|
||||
): Promise<any> {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
const resp = await axios.get(
|
||||
`${SELENIUM_BASE_URL}/job/${sid}/status`,
|
||||
{ timeout: 5_000 }
|
||||
);
|
||||
const s = resp.data;
|
||||
if (s.status === "completed") {
|
||||
if (s.result?.status === "error") {
|
||||
const msg =
|
||||
typeof s.result.message === "string"
|
||||
? s.result.message
|
||||
: s.result.message?.msg ?? "Selenium returned error status";
|
||||
throw new Error(msg);
|
||||
}
|
||||
return s.result;
|
||||
}
|
||||
if (s.status === "failed") {
|
||||
throw new Error(s.error || "Python job failed");
|
||||
}
|
||||
await sleep(intervalMs);
|
||||
const data = resp.data;
|
||||
if (data?.status === "error") {
|
||||
const msg =
|
||||
typeof data.message === "string"
|
||||
? data.message
|
||||
: data.message?.msg ?? "Selenium returned error status";
|
||||
throw new Error(msg);
|
||||
}
|
||||
throw new Error("Selenium job timed out after polling");
|
||||
return data;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user