65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
"""RIIN 增量同步:从数据库最近下单时间增量拉取(无参,全自动)。
|
|
|
|
对应 riin-incremental-sync。from = floorToHour(MAX(orderedAt)),to = now。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from prefect import flow, task
|
|
|
|
from flows.riin._pipeline import sync_riin_records
|
|
from flows.shared import (
|
|
SyncDB, build_stats_rows, create_riin_client, emit_sync_summary, fetch_riin_all_pages,
|
|
floor_to_hour, format_date,
|
|
)
|
|
|
|
|
|
@task(retries=3, retry_delay_seconds=[3, 6, 9])
|
|
def extract_orders(params: dict) -> list:
|
|
client = create_riin_client()
|
|
try:
|
|
return fetch_riin_all_pages(client, params)
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
@task
|
|
def upsert_records(raw_orders: list) -> dict:
|
|
client = create_riin_client()
|
|
try:
|
|
return sync_riin_records(raw_orders, client, SyncDB())
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
@flow(name="riin-incremental-sync", log_prints=True)
|
|
def riin_incremental_sync() -> dict:
|
|
db = SyncDB()
|
|
latest = db.get_latest_ordered_at("RIIN")
|
|
if latest is not None and latest.tzinfo is not None:
|
|
latest = latest.replace(tzinfo=None)
|
|
now = datetime.now()
|
|
from_dt = floor_to_hour(latest) if latest else floor_to_hour(now)
|
|
to_dt = now
|
|
if from_dt >= to_dt:
|
|
from_dt = floor_to_hour(now)
|
|
|
|
params = {
|
|
"time_type": 3,
|
|
"start_time": format_date(from_dt),
|
|
"end_time": format_date(to_dt),
|
|
"page_size": 200,
|
|
}
|
|
print(f"RIIN 增量同步 {params['start_time']} ~ {params['end_time']}")
|
|
raw = extract_orders(params)
|
|
stats = upsert_records(raw)
|
|
print(f"完成: 订单 {stats['orderCount']} / 商品 {stats['goodsCount']}")
|
|
emit_sync_summary("RIIN 增量同步", build_stats_rows("RIIN", stats, [("时间范围", f"{params['start_time']} ~ {params['end_time']}")]))
|
|
return {"syncRange": {"from": params["start_time"], "to": params["end_time"]}, **stats}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
riin_incremental_sync.serve(name="riin-incremental-sync", cron="0 * * * *")
|