- 工参从 2024-01-01 起每周一一份,排队任务一次补一份,新的周先补;一个地市同时只取一份 - 大数据平台账号登录成功后排补取,排队进程每 10 分钟检查缺的周;排队进程重启时放开没取完的快照 - 拆除前工参用拆除日上一周的周一,拆除后用当周的周一;待拆小区不参与周边站和指标匹配 - 一周的指标等周日过后第 3 天才查,没到时间在结果说明里写明哪天之后有数据 - AI 工作区没就绪时开通接口返回 200,改为等待重试,最多 150 秒 - 排任务加锁,避免多个线程同时排时用到已关闭的连接池 - 运行面板显示各地市工参缓存进度
83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
"""凭证刷新和采集周期。只在排队进程中运行。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import threading
|
|
import time
|
|
|
|
from app.collect.accounts import reset_active_tasks
|
|
from app.collect.gongcan import release_loading
|
|
from app.collect.gongcan_sync import request_missing
|
|
from app.config import installed
|
|
from app.db import engine, session_scope
|
|
from app.tasks.runner import refresh_accounts, scan_sources
|
|
|
|
logger = logging.getLogger("decom")
|
|
_started = False
|
|
|
|
|
|
def start_background() -> None:
|
|
global _started
|
|
if _started:
|
|
return
|
|
_started = True
|
|
try:
|
|
reset_active_tasks()
|
|
except Exception as exc:
|
|
logger.info("重置账号占用未完成: %s", exc)
|
|
try:
|
|
released = release_loading(engine())
|
|
if released:
|
|
logger.info("上次没取完的工参快照 %s 份,已放开重取", released)
|
|
except Exception as exc:
|
|
logger.info("放开没取完的工参快照未完成: %s", exc)
|
|
threading.Thread(target=_refresh_loop, name="decom-refresh", daemon=True).start()
|
|
threading.Thread(target=_collect_loop, name="decom-collect", daemon=True).start()
|
|
threading.Thread(target=_gongcan_loop, name="decom-gongcan", daemon=True).start()
|
|
|
|
|
|
def _refresh_loop() -> None:
|
|
while True:
|
|
if not installed():
|
|
time.sleep(2)
|
|
continue
|
|
seconds = 300
|
|
try:
|
|
with session_scope() as session:
|
|
from app.models import AppSetting
|
|
|
|
setting = session.get(AppSetting, 1)
|
|
if setting is not None:
|
|
seconds = setting.credential_refresh_seconds
|
|
asyncio.run(refresh_accounts())
|
|
except Exception as exc:
|
|
logger.info("凭证刷新未完成: %s", exc)
|
|
time.sleep(max(60, seconds))
|
|
|
|
|
|
def _collect_loop() -> None:
|
|
while True:
|
|
if not installed():
|
|
time.sleep(2)
|
|
continue
|
|
try:
|
|
scan_sources()
|
|
except Exception as exc:
|
|
logger.info("采集调度未完成: %s", exc)
|
|
time.sleep(60)
|
|
|
|
|
|
def _gongcan_loop() -> None:
|
|
"""工参每周一一份。新的一周到了、或者之前没补完,就排补取任务。"""
|
|
while True:
|
|
if not installed():
|
|
time.sleep(2)
|
|
continue
|
|
try:
|
|
request_missing()
|
|
except Exception as exc:
|
|
logger.info("工参缓存检查未完成: %s", exc)
|
|
time.sleep(600)
|