Files
qlib/frontend/web/lib/jobs.ts
T
Simon 1cf7c84897 fix(web): 因子研究/回测改走异步 Job(修复全市场研究长请求被代理中断 500)
- 根因:全市场因子测试同步执行约 47s,经 Next 同源代理转发时长请求被 socket 中断(前端日志:Failed to proxy .../api/factor-tests [socket hang up])
- factors/backtest 页改为 POST /api/jobs 提交 → 轮询 GET /api/jobs/{id} → 取内嵌结果(AGENT §19 异步化)
- 新增 lib/jobs.ts(submitJob / waitJob,超时 10 分钟),运行中显示 Job 号
- 端到端验证:全市场 momentum_60 测试 JOB-93479087 → success(IC -0.0598,样本 323 日)+ Experiment 归档
- tsc + next build 通过;同步接口保留(适合小样本/脚本调用)
2026-09-07 20:38:56 +08:00

44 lines
1.5 KiB
TypeScript

/** 异步 Job 研究执行(Phase 4):POST /api/jobs 提交 → 轮询 GET /api/jobs/{id}。
*
* 大样本研究(全市场)可能耗时数十秒到分钟级,经异步 Job 后台执行,
* 避免 HTTP 长阻塞(AGENT §19)。页面提交后即时返回 job_id,再轮询到终态。
*/
import { apiGet, apiPost } from "./api";
import type { ResearchSpec } from "./types";
export interface JobSubmit {
job_id: string;
status: string;
}
export interface JobStatusResp {
job_id: string;
status: string;
result?: unknown;
error?: string | null;
spec?: Record<string, unknown>;
}
export function submitJob(spec: ResearchSpec): Promise<JobSubmit> {
return apiPost<JobSubmit>("/jobs", spec);
}
/** 轮询直到 success / failed / cancelled,或超时(默认 10 分钟)。 */
export async function waitJob<T>(
jobId: string,
timeoutMs = 600_000,
): Promise<{ status: string; result: T | null; error?: string }> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const job = await apiGet<JobStatusResp>(`/jobs/${jobId}`);
if (job.status === "success") {
return { status: job.status, result: (job.result as T) ?? null };
}
if (job.status === "failed" || job.status === "cancelled") {
return { status: job.status, result: null, error: job.error ?? undefined };
}
await new Promise((resolve) => setTimeout(resolve, 1500));
}
return { status: "timeout", result: null, error: "等待结果超时,请稍后在「实验」页查看归档" };
}