UI: job badges, skeletons, cancel support + API route to cancel jobs\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+32
-1
@@ -12,6 +12,7 @@ let worker: any = undefined;
|
||||
let enqueueAnalyze: (ticker: string, input: any) => Promise<string> | string;
|
||||
let getJob: (jobId: string) => Promise<any | null>;
|
||||
let listRecentJobs: (ticker?: string, limit?: number) => Promise<any[]>;
|
||||
let cancelJob: (jobId: string) => Promise<boolean>;
|
||||
|
||||
if (REDIS_URL) {
|
||||
const redis = new IORedis(REDIS_URL as string);
|
||||
@@ -86,6 +87,22 @@ if (REDIS_URL) {
|
||||
if (ticker) return mapped.filter((j: any) => j.data?.ticker === ticker);
|
||||
return mapped;
|
||||
};
|
||||
|
||||
cancelJob = async (jobId: string) => {
|
||||
try {
|
||||
const job = await analyzeQueue.getJob(jobId);
|
||||
if (!job) return false;
|
||||
const state = await job.getState();
|
||||
if (state === "waiting" || state === "delayed") {
|
||||
await job.remove();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (err) {
|
||||
console.error("cancelJob error:", err);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
} 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 };
|
||||
@@ -112,6 +129,20 @@ if (REDIS_URL) {
|
||||
return items;
|
||||
};
|
||||
|
||||
cancelJob = async (jobId: string) => {
|
||||
const job = jobsById[jobId];
|
||||
if (!job) return false;
|
||||
// If queued but not yet processing, remove from queue
|
||||
if (job.state === "queued") {
|
||||
const idx = queue.findIndex((q) => q.id === jobId);
|
||||
if (idx !== -1) queue.splice(idx, 1);
|
||||
job.state = "failed";
|
||||
job.failedReason = "cancelled";
|
||||
return true;
|
||||
}
|
||||
// Can't cancel if already processing/completed/failed
|
||||
return false;
|
||||
};
|
||||
|
||||
async function processQueue() {
|
||||
processing = true;
|
||||
@@ -168,5 +199,5 @@ if (REDIS_URL) {
|
||||
};
|
||||
}
|
||||
|
||||
export { enqueueAnalyze, getJob, listRecentJobs, analyzeQueue, worker };
|
||||
export { enqueueAnalyze, getJob, listRecentJobs, cancelJob, analyzeQueue, worker };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user