167 lines
7.3 KiB
Python
Executable File
167 lines
7.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""批量查看并重新处理失败的 Capture 记录。
|
||
|
||
用法(先看失败原因,确认后再触发):
|
||
|
||
# 1) 列出某工作流所有 failed / cancelled 记录及失败原因(不触发任何操作)
|
||
export DROBOTICFLOW_PASSWORD='...'
|
||
python3 scripts/capture_reprocess.py --base-url https://<线上地址> \
|
||
--phone <管理员手机号> --workflow workflow-xxxx
|
||
|
||
# 2) 确认失败原因与数据已修复后,真正触发批量重新处理
|
||
python3 scripts/capture_reprocess.py ... --go --reason "源数据已修复"
|
||
|
||
说明:
|
||
- 重新处理接口对每条记录单独调用 POST
|
||
/api/workflows/{id}/capture-automation/records/{recordId}/reprocess,
|
||
服务端要求记录状态为 failed 或 cancelled、工作流为持续 Capture 类型。
|
||
- 默认每条之间间隔 2 秒,避免瞬间压入过多 Run;可用 --sleep 调整。
|
||
- 账号需要组织管理员权限(与页面上「重新处理」按钮一致)。
|
||
- 列表接口一次最多返回 200 条记录;更多记录需分批处理。
|
||
- 密码只从环境变量 DROBOTICFLOW_PASSWORD 读取,不进命令行历史。
|
||
"""
|
||
|
||
import argparse
|
||
import json
|
||
import os
|
||
import sys
|
||
import time
|
||
import urllib.error
|
||
import urllib.request
|
||
import http.cookiejar
|
||
from typing import Optional
|
||
|
||
|
||
def build_client(base_url: str) -> tuple:
|
||
jar = http.cookiejar.CookieJar()
|
||
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))
|
||
return opener, jar
|
||
|
||
|
||
def request_json(opener, method: str, url: str, payload: Optional[dict], csrf: Optional[str]) -> dict:
|
||
data = None
|
||
headers = {"Accept": "application/json"}
|
||
if payload is not None:
|
||
data = json.dumps(payload).encode("utf-8")
|
||
headers["Content-Type"] = "application/json"
|
||
if csrf:
|
||
headers["X-Droboticflow-CSRF-Token"] = csrf
|
||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||
try:
|
||
with opener.open(req, timeout=120) as response:
|
||
body = response.read().decode("utf-8")
|
||
return json.loads(body) if body.strip() else {}
|
||
except urllib.error.HTTPError as error:
|
||
body = error.read().decode("utf-8", errors="replace")
|
||
try:
|
||
message = json.loads(body).get("error", body)
|
||
except json.JSONDecodeError:
|
||
message = body
|
||
raise RuntimeError(f"HTTP {error.code}: {message}") from error
|
||
|
||
|
||
def csrf_from_jar(jar) -> Optional[str]:
|
||
for cookie in jar:
|
||
if cookie.name == "droboticflow_csrf":
|
||
return cookie.value
|
||
return None
|
||
|
||
|
||
def login(opener, jar, base_url: str, phone: str, password: str) -> str:
|
||
request_json(opener, "POST", f"{base_url}/api/auth/login", {"phone": phone, "password": password}, None)
|
||
csrf = csrf_from_jar(jar)
|
||
if not csrf:
|
||
raise RuntimeError("登录成功但未获得 CSRF cookie,无法执行写操作")
|
||
return csrf
|
||
|
||
|
||
def truncate(text: str, width: int) -> str:
|
||
text = " ".join((text or "").split())
|
||
return text if len(text) <= width else text[: width - 1] + "…"
|
||
|
||
|
||
def main() -> int:
|
||
parser = argparse.ArgumentParser(description="批量查看 / 重新处理失败的 Capture 记录")
|
||
parser.add_argument("--base-url", required=True, help="平台地址,如 https://example.com(不带末尾斜杠)")
|
||
parser.add_argument("--workflow", required=True, help="工作流 ID(workflow-…)")
|
||
parser.add_argument("--phone", help="管理员手机号")
|
||
parser.add_argument("--password", help="密码(建议改用环境变量 DROBOTICFLOW_PASSWORD)")
|
||
parser.add_argument("--status", default="failed,cancelled", help="要处理的记录状态,逗号分隔(默认 failed,cancelled)")
|
||
parser.add_argument("--limit", type=int, default=0, help="最多处理多少条(0 = 全部匹配)")
|
||
parser.add_argument("--go", action="store_true", help="真正触发重新处理;缺省只列出记录与失败原因")
|
||
parser.add_argument("--reason", default="数据已修复,批量重新处理", help="重新处理原因(记录在操作历史里)")
|
||
parser.add_argument("--sleep", type=float, default=2.0, help="每条请求之间的间隔秒数(默认 2)")
|
||
args = parser.parse_args()
|
||
|
||
password = args.password or os.environ.get("DROBOTICFLOW_PASSWORD")
|
||
if not password:
|
||
print("缺少密码:设置环境变量 DROBOTICFLOW_PASSWORD,或用 --password", file=sys.stderr)
|
||
return 2
|
||
if not args.phone:
|
||
print("缺少手机号:用 --phone", file=sys.stderr)
|
||
return 2
|
||
|
||
base_url = args.base_url.rstrip("/")
|
||
wanted = {item.strip() for item in args.status.split(",") if item.strip()}
|
||
|
||
opener, jar = build_client(base_url)
|
||
try:
|
||
csrf = login(opener, jar, base_url, args.phone, password)
|
||
except RuntimeError as error:
|
||
print(f"登录失败:{error}", file=sys.stderr)
|
||
return 1
|
||
print(f"已登录,开始查询工作流 {args.workflow} …\n")
|
||
|
||
try:
|
||
automation = request_json(opener, "GET", f"{base_url}/api/workflows/{args.workflow}/capture-automation", None, None)
|
||
except RuntimeError as error:
|
||
print(f"读取记录失败:{error}", file=sys.stderr)
|
||
return 1
|
||
|
||
counts = automation.get("counts") or {}
|
||
print("状态计数:", ",".join(f"{key}={value}" for key, value in sorted(counts.items()) if value))
|
||
records = [r for r in (automation.get("records") or []) if r.get("status") in wanted]
|
||
if args.limit > 0:
|
||
records = records[: args.limit]
|
||
print(f"匹配 {args.status} 的记录共 {len(records)} 条(接口单次最多返回 200 条)\n")
|
||
|
||
if not records:
|
||
print("没有匹配的记录。列表按更新时间倒序,失败记录可能被新记录挤出前 200 条。")
|
||
return 0
|
||
|
||
for record in records:
|
||
run_id = record.get("runId") or "—"
|
||
error = record.get("error") or record.get("failedNodeID") or "(无错误信息)"
|
||
print(f"{record['id']}")
|
||
print(f" Capture: {record.get('captureId', '—')} 状态: {record.get('status')} Run: {run_id}")
|
||
print(f" 失败信息: {truncate(error, 120)}")
|
||
|
||
if not args.go:
|
||
print(f"\n以上为只读预览。确认数据已修复后加 --go 触发重新处理(原因可用 --reason 指定)。")
|
||
return 0
|
||
|
||
print(f"\n开始批量重新处理(原因:{args.reason}),间隔 {args.sleep}s …")
|
||
ok, failed = 0, 0
|
||
for index, record in enumerate(records, 1):
|
||
record_id = record["id"]
|
||
url = f"{base_url}/api/workflows/{args.workflow}/capture-automation/records/{record_id}/reprocess"
|
||
try:
|
||
result = request_json(opener, "POST", url, {"reason": args.reason}, csrf)
|
||
except RuntimeError as error:
|
||
failed += 1
|
||
print(f"[{index}/{len(records)}] {record_id} ✗ {error}")
|
||
else:
|
||
after = next((r for r in (result.get("records") or []) if r.get("id") == record_id), {})
|
||
ok += 1
|
||
print(f"[{index}/{len(records)}] {record_id} ✓ 现在状态: {after.get('status', 'requeueing/queued')} Run: {after.get('runId', '—')}")
|
||
if index < len(records):
|
||
time.sleep(args.sleep)
|
||
|
||
print(f"\n完成:成功 {ok} 条,失败 {failed} 条。")
|
||
print("注意:排队成功不代表处理成功;失败原因相同的数据请先修复,否则会再次失败。")
|
||
return 1 if failed else 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|