fix: add HTTP polling fallback for selenium job results through Cloudflare
Socket.IO reconnects through Cloudflare proxy get a new socket.id, causing the backend to emit job:update to a stale socket that no longer exists. The PDF viewer modal never opened even though PDFs were saved successfully. Adds a GET /api/insurance-status/job-status/:jobId endpoint backed by InProcessQueue.getJob(), and a waitForSeleniumJob() helper on the frontend that races socket events against HTTP polling every 3s. Whichever resolves first wins, so local (socket) and external (Cloudflare) both work reliably. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -42,6 +42,56 @@ import { TuftsSCOEligibilityButton } from "@/components/insurance-status/tufts-s
|
||||
import { UnitedSCOEligibilityButton } from "@/components/insurance-status/united-sco-button-modal";
|
||||
import { CCAEligibilityButton } from "@/components/insurance-status/cca-button-modal";
|
||||
|
||||
/**
|
||||
* Waits for a Selenium job to complete by racing socket.io events against
|
||||
* HTTP polling every 3 seconds. This ensures the result is received even
|
||||
* when the socket reconnects through Cloudflare (changing socket.id).
|
||||
*/
|
||||
function waitForSeleniumJob(
|
||||
jobId: string,
|
||||
onActive?: (message: string) => void,
|
||||
): Promise<any> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
let settled = false;
|
||||
|
||||
const settle = (fn: () => void) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearInterval(pollInterval);
|
||||
socket.off("job:update", socketHandler);
|
||||
fn();
|
||||
};
|
||||
|
||||
const socketHandler = (payload: any) => {
|
||||
if (String(payload.jobId) !== String(jobId)) return;
|
||||
if (payload.status === "active") {
|
||||
onActive?.(payload.message ?? "Selenium running...");
|
||||
} else if (payload.status === "completed") {
|
||||
settle(() => resolve(payload.result ?? {}));
|
||||
} else if (payload.status === "failed") {
|
||||
settle(() => reject(new Error(payload.error ?? "Selenium job failed")));
|
||||
}
|
||||
};
|
||||
socket.on("job:update", socketHandler);
|
||||
|
||||
const pollInterval = setInterval(async () => {
|
||||
if (settled) return;
|
||||
try {
|
||||
const res = await apiRequest("GET", `/api/insurance-status/job-status/${jobId}`);
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
if (data.status === "completed") {
|
||||
settle(() => resolve(data.result ?? {}));
|
||||
} else if (data.status === "failed") {
|
||||
settle(() => reject(new Error(data.error ?? "Selenium job failed")));
|
||||
}
|
||||
} catch {
|
||||
// ignore transient poll errors
|
||||
}
|
||||
}, 3000);
|
||||
});
|
||||
}
|
||||
|
||||
export default function InsuranceStatusPage() {
|
||||
const { user } = useAuth();
|
||||
const { toast } = useToast();
|
||||
@@ -254,27 +304,9 @@ export default function InsuranceStatusPage() {
|
||||
}),
|
||||
);
|
||||
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
const handler = (payload: any) => {
|
||||
if (String(payload.jobId) !== String(jobId)) return;
|
||||
if (payload.status === "active") {
|
||||
dispatch(
|
||||
setTaskStatus({
|
||||
key: "eligibilityCheck",
|
||||
status: "pending",
|
||||
message: payload.message ?? "Selenium running...",
|
||||
}),
|
||||
);
|
||||
} else if (payload.status === "completed") {
|
||||
socket.off("job:update", handler);
|
||||
resolve(payload.result ?? {});
|
||||
} else if (payload.status === "failed") {
|
||||
socket.off("job:update", handler);
|
||||
reject(new Error(payload.error ?? "Selenium job failed"));
|
||||
}
|
||||
};
|
||||
socket.on("job:update", handler);
|
||||
});
|
||||
return waitForSeleniumJob(jobId, (msg) =>
|
||||
dispatch(setTaskStatus({ key: "eligibilityCheck", status: "pending", message: msg }))
|
||||
);
|
||||
};
|
||||
|
||||
const handleAddPatient = async () => {
|
||||
@@ -468,21 +500,9 @@ export default function InsuranceStatusPage() {
|
||||
|
||||
dispatch(setTaskStatus({ key: "eligibilityCheck", status: "pending", message: "Selenium browser starting..." }));
|
||||
|
||||
const jobResult = await new Promise<any>((resolve, reject) => {
|
||||
const handler = (payload: any) => {
|
||||
if (String(payload.jobId) !== String(jobId)) return;
|
||||
if (payload.status === "active") {
|
||||
dispatch(setTaskStatus({ key: "eligibilityCheck", status: "pending", message: payload.message ?? "Selenium running..." }));
|
||||
} else if (payload.status === "completed") {
|
||||
socket.off("job:update", handler);
|
||||
resolve(payload.result ?? {});
|
||||
} else if (payload.status === "failed") {
|
||||
socket.off("job:update", handler);
|
||||
reject(new Error(payload.error ?? "Selenium job failed"));
|
||||
}
|
||||
};
|
||||
socket.on("job:update", handler);
|
||||
});
|
||||
const jobResult = await waitForSeleniumJob(jobId, (msg) =>
|
||||
dispatch(setTaskStatus({ key: "eligibilityCheck", status: "pending", message: msg }))
|
||||
);
|
||||
|
||||
dispatch(setTaskStatus({ key: "eligibilityCheck", status: "success", message: "Eligibility and service history PDFs saved to Documents." }));
|
||||
toast({
|
||||
@@ -549,21 +569,9 @@ export default function InsuranceStatusPage() {
|
||||
|
||||
dispatch(setTaskStatus({ key: "eligibilityCheck", status: "pending", message: "Selenium browser starting..." }));
|
||||
|
||||
const jobResult = await new Promise<any>((resolve, reject) => {
|
||||
const handler = (payload: any) => {
|
||||
if (String(payload.jobId) !== String(jobId)) return;
|
||||
if (payload.status === "active") {
|
||||
dispatch(setTaskStatus({ key: "eligibilityCheck", status: "pending", message: payload.message ?? "Selenium running..." }));
|
||||
} else if (payload.status === "completed") {
|
||||
socket.off("job:update", handler);
|
||||
resolve(payload.result ?? {});
|
||||
} else if (payload.status === "failed") {
|
||||
socket.off("job:update", handler);
|
||||
reject(new Error(payload.error ?? "Selenium job failed"));
|
||||
}
|
||||
};
|
||||
socket.on("job:update", handler);
|
||||
});
|
||||
const jobResult = await waitForSeleniumJob(jobId, (msg) =>
|
||||
dispatch(setTaskStatus({ key: "eligibilityCheck", status: "pending", message: msg }))
|
||||
);
|
||||
|
||||
dispatch(setTaskStatus({ key: "eligibilityCheck", status: "success", message: "CMSP PDFs saved to Documents." }));
|
||||
toast({
|
||||
|
||||
Reference in New Issue
Block a user