diff --git a/src/bot/bot.py b/src/bot/bot.py index 7ce7535..a91a4dd 100644 --- a/src/bot/bot.py +++ b/src/bot/bot.py @@ -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) diff --git a/src/db/database.py b/src/db/database.py index 09d4102..7af66b9 100644 --- a/src/db/database.py +++ b/src/db/database.py @@ -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):