feat: 实现已确认的登录、采集和任务导出
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
"""凭证刷新和采集周期。用户任务由队列或本进程线程执行。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db import session_scope
|
||||
from app.models import AppSetting
|
||||
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
|
||||
threading.Thread(target=_refresh_loop, name="decom-refresh", daemon=True).start()
|
||||
threading.Thread(target=_collect_loop, name="decom-collect", daemon=True).start()
|
||||
|
||||
|
||||
def _refresh_loop() -> None:
|
||||
while True:
|
||||
seconds = 300
|
||||
try:
|
||||
with session_scope() as session:
|
||||
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:
|
||||
try:
|
||||
scan_sources()
|
||||
except Exception as exc:
|
||||
logger.info("采集调度未完成: %s", exc)
|
||||
time.sleep(60)
|
||||
Reference in New Issue
Block a user