feat(bot): marcar sesiones 'running' huérfanas como 'interrupted' al arrancar
Las tareas de research viven solo en memoria (_active_tasks): un reinicio del pod las mata sin tocar la DB y sus sesiones quedan en 'running' para siempre — parecen activas en /status y get_active_session. Nuevo estado ResearchStatus.INTERRUPTED y barrido en _on_startup antes de la purga. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
ae56227c03
commit
187d29a372
@@ -851,6 +851,27 @@ async def cmd_help(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
||||
|
||||
# ─── Bot setup ────────────────────────────────────────────────────────────────
|
||||
|
||||
async def _mark_interrupted_on_startup(app: Application) -> None:
|
||||
"""Las tareas de research viven solo en memoria (_active_tasks): un
|
||||
reinicio del pod las mata sin tocar la DB, y sus sesiones quedan en
|
||||
'running' para siempre — parecen activas en /status y get_active_session.
|
||||
"""
|
||||
db_conn = await get_db()
|
||||
try:
|
||||
cursor = await db_conn.execute(
|
||||
"UPDATE research_sessions SET status = ?, updated_at = ? WHERE status = ?",
|
||||
(ResearchStatus.INTERRUPTED, time.time(), ResearchStatus.RUNNING),
|
||||
)
|
||||
await db_conn.commit()
|
||||
if cursor.rowcount:
|
||||
logger.info("Orphaned running sessions marked interrupted",
|
||||
count=cursor.rowcount)
|
||||
except Exception as e:
|
||||
logger.warning("Interrupted-mark failed — bot continues", error=str(e))
|
||||
finally:
|
||||
await db_conn.close()
|
||||
|
||||
|
||||
async def _purge_on_startup(app: Application) -> None:
|
||||
db_conn = await get_db()
|
||||
try:
|
||||
@@ -994,6 +1015,7 @@ async def _start_scheduler(app: Application) -> None:
|
||||
|
||||
|
||||
async def _on_startup(app: Application) -> None:
|
||||
await _mark_interrupted_on_startup(app)
|
||||
await _purge_on_startup(app)
|
||||
await _start_scheduler(app)
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ class ResearchStatus(str, Enum):
|
||||
SATURATED = "saturated"
|
||||
FINISHED = "finished"
|
||||
ERROR = "error"
|
||||
INTERRUPTED = "interrupted" # el pod se reinició con el research en marcha
|
||||
|
||||
|
||||
class OutputType(str, Enum):
|
||||
|
||||
Reference in New Issue
Block a user