fix: on_startup handler spawn 后台任务后立即返回(不阻塞 aiohttp 启动)

This commit is contained in:
yumoqing 2026-08-05 17:31:27 +08:00
parent 99cc19f9aa
commit ee9ab066fc

View File

@ -9,7 +9,6 @@
import json import json
import asyncio import asyncio
import logging
from ahserver.serverenv import ServerEnv from ahserver.serverenv import ServerEnv
from appPublic.uniqueID import getID from appPublic.uniqueID import getID
from appPublic.log import debug from appPublic.log import debug
@ -430,12 +429,13 @@ def load_pipeline_service():
env.question_qna = get_task_qna env.question_qna = get_task_qna
# Background poller: auto-dispatch submitted role_tasks to role agents # Background poller: auto-dispatch submitted role_tasks to role agents
_logger = logging.getLogger("pipeline.agent_poller")
async def _role_poller(app): async def _role_poller(app):
"""轮询 pipeline_tasks 中待执行的 role_task交给对应角色agent执行""" """启动后台轮询器spawn 后立即返回(不阻塞 aiohttp 启动)"""
from sqlor.dbpools import DBPools from sqlor.dbpools import DBPools
poll_db = DBPools() poll_db = DBPools()
async def _poll_loop():
while True: while True:
try: try:
async with poll_db.sqlorContext("pipeline") as sor: async with poll_db.sqlorContext("pipeline") as sor:
@ -449,13 +449,16 @@ def load_pipeline_service():
pid = getattr(rec, 'tenant_id', '') pid = getattr(rec, 'tenant_id', '')
r = getattr(rec, 'role', '') r = getattr(rec, 'role', '')
if tid and pid and r: if tid and pid and r:
_logger.info(f"dispatching task={tid} role={r}") debug(f"agent_poller dispatching task={tid} role={r}")
asyncio.ensure_future( asyncio.ensure_future(
role_agent_loop(pid, r, agent_id=f"poller-{r}")) role_agent_loop(pid, r, agent_id=f"poller-{r}"))
except Exception as e: except Exception as e:
_logger.error(f"error: {e}") debug(f"agent_poller error: {e}")
await asyncio.sleep(10) await asyncio.sleep(10)
asyncio.create_task(_poll_loop())
debug("agent poller started")
from ahserver.configuredServer import add_startup from ahserver.configuredServer import add_startup
add_startup(_role_poller) add_startup(_role_poller)