You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.4 KiB
Python

# backend/app/worker/sync_jobs.py — phase-13 주기 sync 잡 + 수동 트리거 진입점
# 프로토타입은 수동 트리거(/api/connectors/sync-all)도 제공(데모 결정성).
from sqlmodel import Session
from ..config import get_settings
from ..connectors.registry import ConnectorRegistry
from ..db import engine
from ..models import ConnState
def run_periodic_sync(session: Session | None = None) -> list[dict]:
"""worker 스케줄러가 sync_interval_minutes 마다 호출. real/연결된 계정만 sync.
mock 계정은 데이터가 변하지 않으므로 주기 sync 대상이 아니다."""
def _do(s: Session) -> list[dict]:
out: list[dict] = []
for a in ConnectorRegistry.accounts(s):
mode = ConnectorRegistry.effective_mode(a)
if a.state in (ConnState.connected, ConnState.error) and mode != "mock":
res = ConnectorRegistry.get(s, a).sync(s)
out.append(res.__dict__)
return out
if session is not None:
return _do(session)
with Session(engine) as s:
return _do(s)
def register(scheduler) -> None:
"""phase-7 worker.main(APScheduler)에서 호출(있을 때). 없으면 수동 트리거로 동작."""
mins = get_settings().sync_interval_minutes
scheduler.add_job(
run_periodic_sync, "interval", minutes=mins, id="connector_sync", replace_existing=True
)