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,31 +429,35 @@ 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()
while True:
try: async def _poll_loop():
async with poll_db.sqlorContext("pipeline") as sor: while True:
recs = await sor.sqlExe( try:
"SELECT id, tenant_id, role FROM pipeline_tasks " async with poll_db.sqlorContext("pipeline") as sor:
"WHERE state='submitted' AND pipeline_id='role_task' " recs = await sor.sqlExe(
"AND claimed_by IS NULL ORDER BY created_at ASC LIMIT 5", "SELECT id, tenant_id, role FROM pipeline_tasks "
{}) "WHERE state='submitted' AND pipeline_id='role_task' "
for rec in (recs or []): "AND claimed_by IS NULL ORDER BY created_at ASC LIMIT 5",
tid = getattr(rec, 'id', '') {})
pid = getattr(rec, 'tenant_id', '') for rec in (recs or []):
r = getattr(rec, 'role', '') tid = getattr(rec, 'id', '')
if tid and pid and r: pid = getattr(rec, 'tenant_id', '')
_logger.info(f"dispatching task={tid} role={r}") r = getattr(rec, 'role', '')
asyncio.ensure_future( if tid and pid and r:
role_agent_loop(pid, r, agent_id=f"poller-{r}")) debug(f"agent_poller dispatching task={tid} role={r}")
except Exception as e: asyncio.ensure_future(
_logger.error(f"error: {e}") role_agent_loop(pid, r, agent_id=f"poller-{r}"))
await asyncio.sleep(10) except Exception as e:
debug(f"agent_poller error: {e}")
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)