feat(automation): 持久执行队列与重复充值确认

This commit is contained in:
yml
2026-11-23 15:47:56 +08:00
parent 2549511598
commit aaacbea10a
29 changed files with 2147 additions and 178 deletions
+40 -13
View File
@@ -2,6 +2,8 @@
from __future__ import annotations
import os
from loguru import logger
from .database import SessionLocal
@@ -9,8 +11,18 @@ from .database import SessionLocal
_started = False
def _control_plane_enabled() -> bool:
return os.getenv("BACKGROUND_CONTROL_PLANE_ENABLED", "true").strip().lower() not in {
"0",
"false",
"no",
"off",
}
def recover_orphaned_work() -> None:
"""进程启动时收敛上次中断的任务和运行记录。"""
from .models import ExecutionJob
from .services.account_supply_service import ensure_default_supply_targets
from .services.account_work_queue import recover_running_account_work
from .services.automation.scheduler import cleanup_orphan_runs
@@ -25,6 +37,14 @@ def recover_orphaned_work() -> None:
db = SessionLocal()
try:
durable_batch_ids = {
str(batch_id)
for (batch_id,) in (
db.query(ExecutionJob.batch_id)
.filter(ExecutionJob.status.in_(("queued", "running")))
.all()
)
}
cleaned_huya = cleanup_orphan_huya_tasks(
db,
message="任务已中断(服务重启)",
@@ -33,6 +53,7 @@ def recover_orphaned_work() -> None:
logger.info("启动清理虎牙残留任务: {} 条", cleaned_huya)
cleaned_douyu = cleanup_orphan_douyu_tasks(
db,
active_batch_ids=durable_batch_ids,
message="任务已中断(服务重启)",
)
if cleaned_douyu:
@@ -71,6 +92,7 @@ def start_background_services() -> bool:
return False
from .services.automation import register_default_jobs, start_scheduler
from .services.binding_poll_scheduler import start_binding_poll_scheduler
from .services.execution_worker import start_execution_job_worker
from .services.integration import start_integration_outbox_worker
from .services.payment_poll_scheduler import start_payment_poll_scheduler
from .services.platform_fulfillment.auto_allocator import (
@@ -80,13 +102,15 @@ def start_background_services() -> bool:
start_platform_fulfillment_reconciler,
)
register_default_jobs()
start_scheduler()
start_integration_outbox_worker()
start_platform_fulfillment_reconciler()
start_platform_fulfillment_auto_allocator()
start_binding_poll_scheduler()
start_payment_poll_scheduler()
start_execution_job_worker()
if _control_plane_enabled():
register_default_jobs()
start_scheduler()
start_integration_outbox_worker()
start_platform_fulfillment_reconciler()
start_platform_fulfillment_auto_allocator()
start_binding_poll_scheduler()
start_payment_poll_scheduler()
_started = True
logger.info("后台服务已启动")
return True
@@ -98,6 +122,7 @@ def stop_background_services() -> None:
return
from .services.automation import stop_scheduler
from .services.binding_poll_scheduler import stop_binding_poll_scheduler
from .services.execution_worker import stop_execution_job_worker
from .services.integration import stop_integration_outbox_worker
from .services.payment_poll_scheduler import stop_payment_poll_scheduler
from .services.platform_fulfillment.auto_allocator import (
@@ -107,12 +132,14 @@ def stop_background_services() -> None:
stop_platform_fulfillment_reconciler,
)
stop_payment_poll_scheduler()
stop_binding_poll_scheduler()
stop_platform_fulfillment_auto_allocator()
stop_platform_fulfillment_reconciler()
stop_integration_outbox_worker()
stop_scheduler()
if _control_plane_enabled():
stop_payment_poll_scheduler()
stop_binding_poll_scheduler()
stop_platform_fulfillment_auto_allocator()
stop_platform_fulfillment_reconciler()
stop_integration_outbox_worker()
stop_scheduler()
stop_execution_job_worker()
_started = False
logger.info("后台服务已停止")