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
+6 -10
View File
@@ -22,11 +22,9 @@ if (REDIS_URL) {
worker = new Worker(
"analyze",
async (job: any) => {
console.log("[queue] Processing analyze job", job.id, job.data.ticker);
const { ticker, input } = job.data as { ticker: string; input: any };
const apiKey = process.env.OPENROUTER_API_KEY;
if (!apiKey || apiKey === "your_openrouter_api_key_here") {
console.log("[queue] mock mode for analyze", ticker);
const mockDecision = {
action: "hold",
confidence: 0.6,
@@ -41,7 +39,6 @@ if (REDIS_URL) {
}
const { graph, config } = await buildTradingGraph(apiKey);
console.log("[queue] Trading config:", config);
// Fetch latest Alpaca account and prices; abort job if unavailable so work runs on fresh data
try {
const account = await fetchAccount();
@@ -89,7 +86,6 @@ if (REDIS_URL) {
},
});
console.log("[queue] Job complete and saved for", ticker);
return decision;
},
{ connection: redis }
@@ -106,12 +102,12 @@ if (REDIS_URL) {
const state = await job.getState();
const failedReason = job.failedReason || null;
const returnValue = job.returnvalue || null;
return { id: job.id, state, failedReason, returnValue };
return { id: job.id, state, failedReason, returnValue, timestamp: job.timestamp ?? job.data?.timestamp ?? null };
};
listRecentJobs = async (ticker?: string, limit = 50) => {
const jobs = await analyzeQueue.getJobs(["waiting", "active", "completed", "failed", "delayed"], 0, limit - 1);
const mapped = await Promise.all(jobs.map(async (j: any) => ({ id: j.id, name: j.name, data: j.data, state: await j.getState(), returnValue: j.returnvalue || null })));
const mapped = await Promise.all(jobs.map(async (j: any) => ({ id: j.id, name: j.name, data: j.data, state: await j.getState(), returnValue: j.returnvalue || null, timestamp: j.timestamp ?? j.data?.timestamp ?? null })));
if (ticker) return mapped.filter((j: any) => j.data?.ticker === ticker);
return mapped;
};
@@ -133,7 +129,7 @@ if (REDIS_URL) {
};
} else {
// In-process fallback queue for environments without Redis (dev/tests)
type Job = { id: string; ticker: string; input: any; state: "queued" | "processing" | "completed" | "failed"; result?: any; failedReason?: string };
type Job = { id: string; ticker: string; input: any; state: "queued" | "processing" | "completed" | "failed"; result?: any; failedReason?: string; timestamp: number };
const queue: Job[] = [];
const jobsById: Record<string, Job> = {};
let processing = false;
@@ -144,7 +140,7 @@ if (REDIS_URL) {
enqueueAnalyze = (ticker: string, input: any) => {
const id = makeId();
const job: Job = { id, ticker, input, state: "queued" };
const job: Job = { id, ticker, input, state: "queued", timestamp: input?.timestamp ?? Date.now() };
queue.push(job);
jobsById[id] = job;
if (!processing) processQueue().catch((e) => console.error("inproc queue error:", e));
@@ -152,7 +148,7 @@ if (REDIS_URL) {
};
listRecentJobs = async (ticker?: string, limit = 50) => {
const items = Object.values(jobsById).slice(-limit).reverse().map((j) => ({ id: j.id, data: { ticker: j.ticker }, state: j.state, returnValue: j.result || null }));
const items = Object.values(jobsById).slice(-limit).reverse().map((j) => ({ id: j.id, data: { ticker: j.ticker, timestamp: j.timestamp }, state: j.state, returnValue: j.result || null, timestamp: j.timestamp }));
if (ticker) return items.filter((it) => it.data?.ticker === ticker);
return items;
};
@@ -252,7 +248,7 @@ if (REDIS_URL) {
getJob = async (jobId: string) => {
const job = jobsById[jobId];
if (!job) return null;
return { id: job.id, state: job.state, failedReason: job.failedReason || null, returnValue: job.result || null };
return { id: job.id, state: job.state, failedReason: job.failedReason || null, returnValue: job.result || null, timestamp: job.timestamp };
};
}