perf(automation): 持久队列驱动并优化履约热路径
This commit is contained in:
+12
-90
@@ -54,102 +54,24 @@ async def lifespan(app: FastAPI):
|
||||
setup_logger(level=_log_level, log_dir=str(_log_dir))
|
||||
|
||||
init_db()
|
||||
# 进程重启后内存批次丢失,清理历史 pending/running,避免前端被假活跃批次锁死。
|
||||
from loguru import logger
|
||||
|
||||
from .database import SessionLocal, dispose_engines
|
||||
from .services.account_work_queue import recover_running_account_work
|
||||
from .services.douyu_service import cleanup_orphan_douyu_tasks
|
||||
from .services.huya_service import cleanup_orphan_huya_tasks
|
||||
from .services.login_quota_service import cleanup_orphan_login_attempts
|
||||
from .services.login_service import (
|
||||
cleanup_orphan_login_tasks,
|
||||
cleanup_orphan_relogin_tasks,
|
||||
)
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
cleaned = cleanup_orphan_huya_tasks(db, message="任务已中断(服务重启)")
|
||||
if cleaned:
|
||||
logger.info(f"启动清理虎牙残留任务: {cleaned} 条")
|
||||
cleaned_douyu = cleanup_orphan_douyu_tasks(db, message="任务已中断(服务重启)")
|
||||
if cleaned_douyu:
|
||||
logger.info(f"启动清理斗鱼残留任务: {cleaned_douyu} 条")
|
||||
cleaned_relogin = cleanup_orphan_relogin_tasks(db)
|
||||
if cleaned_relogin:
|
||||
logger.info(f"启动清理 CK 重登残留任务: {cleaned_relogin} 条")
|
||||
from .services.automation.scheduler import cleanup_orphan_runs
|
||||
|
||||
cleaned_runs = cleanup_orphan_runs(db)
|
||||
if cleaned_runs:
|
||||
logger.info(f"启动收尾中断的自动化运行记录: {cleaned_runs} 条")
|
||||
cleaned_login = cleanup_orphan_login_tasks(db)
|
||||
if cleaned_login:
|
||||
logger.info(f"启动清理登录残留任务: {cleaned_login} 条")
|
||||
cleaned_attempts = cleanup_orphan_login_attempts(db)
|
||||
if cleaned_attempts:
|
||||
logger.info(f"启动收敛中断的登录尝试流水: {cleaned_attempts} 条")
|
||||
recovered_work = recover_running_account_work(db)
|
||||
if recovered_work:
|
||||
logger.info(f"启动恢复中断的账号内部工作项: {recovered_work} 条")
|
||||
from .services.account_supply_service import ensure_default_supply_targets
|
||||
|
||||
seeded_targets = ensure_default_supply_targets(db)
|
||||
if seeded_targets:
|
||||
logger.info(f"已建立默认账号供给目标: {seeded_targets} 条")
|
||||
from .services.yyb_service import cleanup_orphan_yyb_tasks
|
||||
|
||||
cleaned_yyb = cleanup_orphan_yyb_tasks(db, message="任务已中断(服务重启)")
|
||||
if cleaned_yyb:
|
||||
logger.info(f"启动清理应用宝残留任务: {cleaned_yyb} 条")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
# 自动化调度器:AUTOMATION_ENABLED 未开启时不会起线程(默认关闭)。
|
||||
from .services.automation import (
|
||||
register_default_jobs,
|
||||
start_scheduler,
|
||||
stop_scheduler,
|
||||
)
|
||||
|
||||
register_default_jobs()
|
||||
start_scheduler()
|
||||
from .services.binding_poll_scheduler import (
|
||||
start_binding_poll_scheduler,
|
||||
stop_binding_poll_scheduler,
|
||||
)
|
||||
from .services.integration import (
|
||||
start_integration_outbox_worker,
|
||||
stop_integration_outbox_worker,
|
||||
)
|
||||
from .services.payment_poll_scheduler import (
|
||||
start_payment_poll_scheduler,
|
||||
stop_payment_poll_scheduler,
|
||||
)
|
||||
from .services.platform_fulfillment.auto_allocator import (
|
||||
start_platform_fulfillment_auto_allocator,
|
||||
stop_platform_fulfillment_auto_allocator,
|
||||
)
|
||||
from .services.platform_fulfillment.reconciler import (
|
||||
start_platform_fulfillment_reconciler,
|
||||
stop_platform_fulfillment_reconciler,
|
||||
from .background_runtime import (
|
||||
recover_orphaned_work,
|
||||
start_background_services,
|
||||
stop_background_services,
|
||||
)
|
||||
from .database import dispose_engines
|
||||
from .services.task_runtime import shutdown as shutdown_task_runtime
|
||||
|
||||
start_integration_outbox_worker()
|
||||
start_platform_fulfillment_reconciler()
|
||||
start_platform_fulfillment_auto_allocator()
|
||||
start_binding_poll_scheduler()
|
||||
start_payment_poll_scheduler()
|
||||
role = os.getenv("RUNTIME_ROLE", "all").strip().lower()
|
||||
background_in_process = role in {"all", "background"}
|
||||
if background_in_process:
|
||||
recover_orphaned_work()
|
||||
start_background_services()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
stop_payment_poll_scheduler()
|
||||
stop_binding_poll_scheduler()
|
||||
stop_platform_fulfillment_auto_allocator()
|
||||
stop_platform_fulfillment_reconciler()
|
||||
stop_integration_outbox_worker()
|
||||
stop_scheduler()
|
||||
if background_in_process:
|
||||
stop_background_services()
|
||||
shutdown_task_runtime(wait=False)
|
||||
dispose_engines()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user