feat: always enqueue analyze jobs as background, save jobId to DB, reuse active jobs, cleanup stale jobs

This commit is contained in:
2026-05-16 22:31:19 +02:00
parent ff798abf04
commit b5b5207756
3 changed files with 97 additions and 90 deletions
+42 -7
View File
@@ -386,13 +386,6 @@ export default function Analyze() {
const quote = quoteRes.ok ? await quoteRes.json() : null;
const indicatorsData = indicatorsRes.ok ? await indicatorsRes.json() : null;
const analysisRes = await fetch("/api/analyze", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ticker }),
});
const analysis = analysisRes.ok ? await analysisRes.json() : null;
const indicators: Indicators = {
rsi: indicatorsData?.indicators?.rsi ?? null,
sma20: indicatorsData?.indicators?.sma ?? null,
@@ -407,6 +400,48 @@ export default function Analyze() {
avgVolume: indicatorsData?.indicators?.avgVolume ?? null,
};
const analysisRes = await fetch("/api/analyze", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ticker }),
});
if (analysisRes.status === 202) {
// Background job queued - poll for completion
const data = await analysisRes.json();
const jobId = data.jobId;
let cancelled = false;
let timer: ReturnType<typeof setTimeout> | null = null;
const poll = async () => {
try {
const jr = await fetch(`/api/jobs/${jobId}`);
if (!jr.ok) { if (!cancelled) timer = setTimeout(poll, 2000); return; }
const j = await jr.json();
if (cancelled) return;
if (j.state === "completed" && j.returnValue) {
setStocks((s) => s.map((st) =>
st.id === id ? { ...st, loading: false, currentPrice: quote?.price ?? null, indicators, analysis: j.returnValue } : st
));
cancelled = true;
return;
}
if (j.state === "failed") {
setStocks((s) => s.map((st) => st.id === id ? { ...st, loading: false } : st));
cancelled = true;
return;
}
} catch (e) { /* ignore */ }
if (!cancelled) timer = setTimeout(poll, 2000);
};
poll();
return () => { cancelled = true; if (timer) clearTimeout(timer); };
}
// Fallback: synchronous response
const analysis = analysisRes.ok ? await analysisRes.json() : null;
setStocks((s) => s.map((st) =>
st.id === id
? { ...st, loading: false, currentPrice: quote?.price ?? null, indicators, analysis }