Spaces:
Sleeping
Sleeping
Download frontend/src/utils/taskPolling.ts from shaun07/grid-backtest: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/spaces/shaun07/grid-backtest/resolve/main/frontend/src/utils/taskPolling.ts
- Command line
-
hf download hf://spaces/shaun07/grid-backtest/frontend/src/utils/taskPolling.ts
-
curl -L -o taskPolling.ts https://huggingface.co/spaces/shaun07/grid-backtest/resolve/main/frontend/src/utils/taskPolling.ts
3.13 kB
| import axios from 'axios' | |
| export type TaskStatus = { | |
| task_id: string | |
| user_id?: string | null | |
| type?: string | |
| status?: string | |
| progress?: number | |
| stage?: string | |
| message?: string | null | |
| error?: string | null | |
| created_at?: string | null | |
| started_at?: string | null | |
| finished_at?: string | null | |
| file_path?: string | null | |
| params?: any | |
| result?: any | |
| } | |
| export type PollOptions = { | |
| intervalMs?: number | |
| timeoutMs?: number | |
| backoffMinMs?: number | |
| backoffMaxMs?: number | |
| onUpdate?: (st: TaskStatus) => void | |
| signal?: AbortSignal | |
| } | |
| const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)) | |
| const clamp = (v: number, min: number, max: number) => Math.max(min, Math.min(max, v)) | |
| const parseRetryAfterMs = (raw: any) => { | |
| const s = Number(raw) | |
| if (!Number.isFinite(s) || s <= 0) return null | |
| return Math.max(0, Math.floor(s * 1000)) | |
| } | |
| export const startTaskPolling = (taskId: string, options: PollOptions = {}) => { | |
| const controller = new AbortController() | |
| if (options.signal) { | |
| if (options.signal.aborted) { | |
| controller.abort() | |
| } else { | |
| options.signal.addEventListener('abort', () => controller.abort(), { once: true }) | |
| } | |
| } | |
| const intervalMs = Math.max(200, Number(options.intervalMs || 1000)) | |
| const timeoutMs = Math.max(1000, Number(options.timeoutMs || 10 * 60 * 1000)) | |
| const backoffMinMs = Math.max(200, Number(options.backoffMinMs || 3000)) | |
| const backoffMaxMs = Math.max(backoffMinMs, Number(options.backoffMaxMs || 10000)) | |
| const cancel = () => controller.abort() | |
| const promise = (async () => { | |
| const t0 = Date.now() | |
| let backoffPow = 0 | |
| while (true) { | |
| if (controller.signal.aborted) { | |
| throw new Error('已取消') | |
| } | |
| if (Date.now() - t0 > timeoutMs) { | |
| throw new Error('轮询超时') | |
| } | |
| try { | |
| const res = await axios.get<TaskStatus>(`/api/v2/tasks/${taskId}`, { signal: controller.signal as any }) | |
| const st = res.data | |
| options.onUpdate?.(st) | |
| backoffPow = 0 | |
| const status = String(st?.status || '').toLowerCase() | |
| if (status === 'completed' || status === 'failed' || status === 'canceled' || status === 'cancelled') { | |
| return st | |
| } | |
| await sleep(intervalMs) | |
| continue | |
| } catch (err: any) { | |
| if (controller.signal.aborted) { | |
| throw new Error('已取消') | |
| } | |
| const statusCode = err?.response?.status | |
| const retryAfter = parseRetryAfterMs(err?.response?.headers?.['retry-after']) | |
| if (statusCode === 429) { | |
| const wait = retryAfter ?? clamp(backoffMinMs * Math.pow(2, backoffPow), backoffMinMs, backoffMaxMs) | |
| backoffPow = Math.min(backoffPow + 1, 10) | |
| await sleep(wait) | |
| continue | |
| } | |
| const wait = clamp(backoffMinMs * Math.pow(2, backoffPow), backoffMinMs, backoffMaxMs) | |
| backoffPow = Math.min(backoffPow + 1, 10) | |
| await sleep(wait) | |
| } | |
| } | |
| })() | |
| return { promise, cancel } | |
| } | |