本页面为机器翻译,尚待人工审核,可能存在错误。
取消正在进行的导入
本方案展示如何在异步导入仍在处理时将其取消。工作进程会在每个批次边界检查是否存在取消请求;一旦发现,它会在当前批次结束时停止,并将状态切换为 cancelled。在取消之前已完成的行会保留下来——不存在需要事后清理的部分写入批次。
何时取消
当您在导入途中意识到导入有误时,取消端点就是合适的工具:文件错了、投资组合错了、快照已过时、日期错了。典型的触发情形:
- 监控仪表板发现导入指向了错误的文件。
- 您本想先对其进行试运行,却直接针对生产投资组合启动了数百万行的回填。
- 上游系统在上传完成后将该文件标记为已损坏。
取消不能替代 dryRun: true(参见试运行方案)。一旦正式导入开始运行,在取消点之前成功的行都会被提交。您可以依赖 Waafir 不会写入部分批次,但无法回滚已提交的行;正确的做法是随后执行一次纠正性导入,对错误落库的内容进行调整。
批次边界保证
工作进程按每批两百行处理数据。每处理完一个批次,它都会检查导入状态;如果发现状态为 cancelled,就会在此停止,状态保持为 cancelled。具体保证如下:
- 工作进程在取消信号之前已开始的每个批次都会完整执行。这些行会被提交。
- 取消信号到达时工作进程尚未开始的每个批次都会被跳过。这些行不会落库。
- 绝不会出现半个批次落库的中间状态。批次一旦开始,就会完成。
终态 cancelled 状态带有取消那一刻的行计数——即实际已提交的数量。
方案
# curl — cancel an in-flight import
set -euo pipefail
PAT="${WAAFIR_PAT}"
BASE_URL="${WAAFIR_BASE_URL:-https://app.waafir.io}"
IMPORT_ID="${1:?supply the import id}"
# Step 1 — DELETE the import. The endpoint returns immediately with the
# (eventual) cancellation in motion. A second DELETE is idempotent —
# already-cancelled imports return the same final status.
curl -fsS -X DELETE "${BASE_URL}/api/v1/monitoring/loan/imports/${IMPORT_ID}" \
-H "Authorization: Bearer ${PAT}" \
| jq '{importId, status}'
# Step 2 — wait for the worker to reach the next batch boundary and
# flip to a terminal status.
TERMINAL_PATTERN='succeeded|partial|failed|cancelled'
while :; do
STATUS=$(curl -fsS "${BASE_URL}/api/v1/monitoring/loan/imports/${IMPORT_ID}" \
-H "Authorization: Bearer ${PAT}" | jq -r .status)
echo "import ${IMPORT_ID}: ${STATUS}"
if echo "$STATUS" | grep -Eq "^(${TERMINAL_PATTERN})$"; then break; fi
sleep 2
done
# Step 3 — read the final row counts to know how many rows actually committed.
curl -fsS "${BASE_URL}/api/v1/monitoring/loan/imports/${IMPORT_ID}" \
-H "Authorization: Bearer ${PAT}" \
| jq '{status, rowCountProcessed, rowCountSucceeded, rowCountFailed}'// TypeScript — cancel an in-flight import
const PAT = process.env.WAAFIR_PAT!;
const BASE_URL = process.env.WAAFIR_BASE_URL ?? "https://app.waafir.io";
const importId = process.argv[2];
if (!importId) throw new Error("usage: ts-node cancel.ts <importId>");
const auth = { Authorization: `Bearer ${PAT}` };
// Step 1 — DELETE the import; the response confirms the signal was accepted.
const cancelRes = await fetch(`${BASE_URL}/api/v1/monitoring/loan/imports/${importId}`, {
method: "DELETE",
headers: auth,
});
if (!cancelRes.ok) throw new Error(`cancel failed: ${cancelRes.status} ${await cancelRes.text()}`);
console.log("cancel accepted:", await cancelRes.json());
// Step 2 — poll for terminal status.
const TERMINAL = new Set(["succeeded", "partial", "failed", "cancelled"]);
type Status = {
status: string;
rowCountProcessed: number;
rowCountSucceeded: number;
rowCountFailed: number;
};
let final: Status;
while (true) {
final = (await (await fetch(
`${BASE_URL}/api/v1/monitoring/loan/imports/${importId}`,
{ headers: auth },
)).json()) as Status;
console.log(`import ${importId}: ${final.status}`);
if (TERMINAL.has(final.status)) break;
await new Promise((r) => setTimeout(r, 2_000));
}
// Step 3 — what actually landed.
console.log({
status: final.status,
rowCountProcessed: final.rowCountProcessed,
rowCountSucceeded: final.rowCountSucceeded,
rowCountFailed: final.rowCountFailed,
});# python — cancel an in-flight import
import os
import sys
import time
import requests
PAT = os.environ["WAAFIR_PAT"]
BASE_URL = os.environ.get("WAAFIR_BASE_URL", "https://app.waafir.io")
import_id = sys.argv[1] if len(sys.argv) > 1 else None
if not import_id:
raise SystemExit("usage: python cancel.py <importId>")
auth = {"Authorization": f"Bearer {PAT}"}
# Step 1 — DELETE the import.
cancel = requests.delete(
f"{BASE_URL}/api/v1/monitoring/loan/imports/{import_id}",
headers=auth,
timeout=30,
)
cancel.raise_for_status()
print("cancel accepted:", cancel.json())
# Step 2 — poll for terminal status.
terminal = {"succeeded", "partial", "failed", "cancelled"}
while True:
final = requests.get(
f"{BASE_URL}/api/v1/monitoring/loan/imports/{import_id}",
headers=auth,
timeout=30,
).json()
print(f"import {import_id}: {final['status']}")
if final["status"] in terminal:
break
time.sleep(2)
# Step 3 — what actually landed.
print({
"status": final["status"],
"rowCountProcessed": final["rowCountProcessed"],
"rowCountSucceeded": final["rowCountSucceeded"],
"rowCountFailed": final["rowCountFailed"],
})验证要点
轮询循环结束后,如果取消信号在工作进程完成之前送达,status 为 cancelled;如果工作进程在下一次批次边界检查之前就处理完了整个文件(这种情况很少见,但对于小文件是可能的),则为 succeeded / partial / failed 之一。
rowCountSucceeded 字段是取消之前已提交的行数。如果您需要一份确切的落库清单,请按外部 ID 对这些行进行对账(参见对账方案)。
对同一导入再次发送 DELETE 会返回现有的终态状态,而不会重新发出取消;该端点是幂等的。取消一个已处于终态的导入(已经达到 succeeded、partial 或 failed 的导入)不会产生任何操作,只会返回现有状态。