feat: TTL purge — purge_old_sessions + /purge command + startup hook
Build & Deploy ResearchOwl / build-and-push (push) Successful in 5s
Build & Deploy ResearchOwl / build-and-push (push) Successful in 5s
This commit is contained in:
+64
-1
@@ -503,8 +503,70 @@ async def cmd_help(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
||||
|
||||
# ─── Bot setup ────────────────────────────────────────────────────────────────
|
||||
|
||||
async def _purge_on_startup(app: Application) -> None:
|
||||
db_conn = await get_db()
|
||||
try:
|
||||
db = ResearchDB(db_conn)
|
||||
result = await db.purge_old_sessions(30)
|
||||
if result["sessions"] > 0:
|
||||
logger.info("Startup purge done", **result)
|
||||
except Exception as e:
|
||||
logger.warning("Startup purge failed — bot continues", error=str(e))
|
||||
finally:
|
||||
await db_conn.close()
|
||||
|
||||
|
||||
async def cmd_purge(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
||||
if not is_authorized(update.effective_user.id):
|
||||
return
|
||||
|
||||
args = ctx.args or []
|
||||
|
||||
if not args:
|
||||
days = 30
|
||||
else:
|
||||
try:
|
||||
days = int(args[0])
|
||||
except ValueError:
|
||||
await update.message.reply_text(
|
||||
"❌ Uso: `/purge [días]`", parse_mode=ParseMode.MARKDOWN
|
||||
)
|
||||
return
|
||||
if days < 0:
|
||||
await update.message.reply_text("❌ El número de días debe ser ≥ 0.")
|
||||
return
|
||||
if days == 0 and not (len(args) >= 2 and args[1] == "confirm"):
|
||||
await update.message.reply_text(
|
||||
"⚠️ Esto borrará *todas* las sesiones completadas.\n"
|
||||
"Envía `/purge 0 confirm` para confirmar.",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
)
|
||||
return
|
||||
|
||||
db_conn = await get_db()
|
||||
try:
|
||||
db = ResearchDB(db_conn)
|
||||
result = await db.purge_old_sessions(days)
|
||||
await update.message.reply_text(
|
||||
f"🗑️ Purged: {result['sessions']} sessions, "
|
||||
f"{result['sources']} sources, "
|
||||
f"{result['chunks']} chunks, "
|
||||
f"{result['outputs']} outputs"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("Purge command failed", error=str(e))
|
||||
await update.message.reply_text(f"❌ Purge failed: {str(e)[:200]}")
|
||||
finally:
|
||||
await db_conn.close()
|
||||
|
||||
|
||||
def create_bot() -> Application:
|
||||
app = Application.builder().token(settings.telegram_bot_token).build()
|
||||
app = (
|
||||
Application.builder()
|
||||
.token(settings.telegram_bot_token)
|
||||
.post_init(_purge_on_startup)
|
||||
.build()
|
||||
)
|
||||
|
||||
app.add_handler(CommandHandler("start", cmd_start))
|
||||
app.add_handler(CommandHandler("help", cmd_help))
|
||||
@@ -516,6 +578,7 @@ def create_bot() -> Application:
|
||||
app.add_handler(CommandHandler("outputs", cmd_outputs))
|
||||
app.add_handler(CommandHandler("process", cmd_process))
|
||||
app.add_handler(CommandHandler("cancel", cmd_cancel))
|
||||
app.add_handler(CommandHandler("purge", cmd_purge))
|
||||
|
||||
return app
|
||||
|
||||
|
||||
Reference in New Issue
Block a user