refactor: unify data preparations and research input snapshots
Deploy production / deploy (push) Successful in 53s
Deploy production / deploy (push) Successful in 53s
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import argparse
|
||||
import asyncio
|
||||
import getpass
|
||||
import math
|
||||
|
||||
from sqlalchemy import delete, update
|
||||
|
||||
@@ -59,6 +60,63 @@ async def token_command(args):
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
async def catalog_sync_command(args):
|
||||
"""Enqueue on the existing runner, then observe without owning the upstream session."""
|
||||
import json
|
||||
import time
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
from .business import Business
|
||||
from .catalog.contracts import CatalogJobInput, Scope
|
||||
from .catalog.service import Catalog
|
||||
from .models import Job
|
||||
|
||||
engine, sessions = create_database(Settings().database_url)
|
||||
try:
|
||||
async with sessions.begin() as db:
|
||||
if args.resume_job:
|
||||
if args.region or args.universe or args.delay is not None:
|
||||
raise ValueError("--resume-job 不能同时指定新范围")
|
||||
job = await db.get(Job, args.resume_job)
|
||||
if not job or job.kind != "catalog_full_sync":
|
||||
raise ValueError("只能恢复已有全量目录任务")
|
||||
result = await Business(db).retry_job(job.id)
|
||||
job_id = result["id"]
|
||||
else:
|
||||
if not args.region or not args.universe or args.delay is None:
|
||||
raise ValueError("需要 --region、--universe 和 --delay")
|
||||
scope = Scope(instrument_type=args.instrument_type, region=args.region,
|
||||
universe=args.universe, delay=args.delay)
|
||||
job = await Catalog(db).create_job(CatalogJobInput(scope=scope), full=True)
|
||||
job_id = job.id
|
||||
deadline, previous = time.monotonic() + args.wait_timeout, None
|
||||
while True:
|
||||
async with sessions() as db:
|
||||
job = await db.get(Job, job_id)
|
||||
data = dict(job_id=job.id, status=job.status, processed=job.processed,
|
||||
total=job.total, failed=job.failed, checkpoint=job.checkpoint, error=job.error)
|
||||
current = json.dumps(data, ensure_ascii=False, sort_keys=True)
|
||||
if current != previous:
|
||||
print(current, flush=True)
|
||||
previous = current
|
||||
if job.status == "completed":
|
||||
return 0
|
||||
if job.status in ("failed", "completed_with_errors", "cancelled"):
|
||||
return 2 if job.checkpoint.get("error_code") == "invalid_scope" else 1
|
||||
if job.status in ("waiting_auth", "waiting_connection"):
|
||||
return 3
|
||||
if time.monotonic() >= deadline:
|
||||
print(f"等待超时;后台任务 {job_id} 继续执行", flush=True)
|
||||
return 4
|
||||
await asyncio.sleep(min(5, max(0, deadline - time.monotonic())))
|
||||
except HTTPException as exc:
|
||||
print(str(exc.detail), flush=True)
|
||||
return 3 if exc.status_code == 409 else 1
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
commands = parser.add_subparsers(dest="command", required=True)
|
||||
@@ -70,8 +128,19 @@ if __name__ == "__main__":
|
||||
commands.add_parser("mcp-token-list")
|
||||
revoke = commands.add_parser("mcp-token-revoke")
|
||||
revoke.add_argument("token_id")
|
||||
sync = commands.add_parser("catalog-sync", help="全量同步一个范围的数据集及全部字段")
|
||||
sync.add_argument("--region")
|
||||
sync.add_argument("--universe")
|
||||
sync.add_argument("--delay", type=int, choices=range(0, 10))
|
||||
sync.add_argument("--instrument-type", default="EQUITY")
|
||||
sync.add_argument("--resume-job")
|
||||
sync.add_argument("--wait-timeout", type=float, default=21600)
|
||||
args = parser.parse_args()
|
||||
if args.command == "catalog-sync" and (not math.isfinite(args.wait_timeout) or args.wait_timeout <= 0):
|
||||
parser.error("--wait-timeout 必须大于 0")
|
||||
try:
|
||||
if args.command == "catalog-sync":
|
||||
raise SystemExit(asyncio.run(catalog_sync_command(args)))
|
||||
asyncio.run(reset_password() if args.command == "reset-password" else token_command(args))
|
||||
except ValueError as exc:
|
||||
parser.error(str(exc))
|
||||
|
||||
Reference in New Issue
Block a user