From 1bd385e0540f7f7047f5480626ee08513c20f6c4 Mon Sep 17 00:00:00 2001 From: mr-forust Date: Sat, 27 Jun 2026 15:45:05 +0200 Subject: [PATCH] fix: add WAL mode for session SQLite to prevent 'database is locked', persist active account tab in localStorage --- telegram_scraper_with_forwarding.py | 14 ++++++++++++++ webui/app.js | 6 ++++-- webui_server.py | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/telegram_scraper_with_forwarding.py b/telegram_scraper_with_forwarding.py index c60f676..fe4cc1b 100644 --- a/telegram_scraper_with_forwarding.py +++ b/telegram_scraper_with_forwarding.py @@ -79,6 +79,19 @@ class ForwardingRule: enabled: bool = True +def _ensure_session_wal(session_path: str) -> None: + db_path = session_path if session_path.endswith(".session") else f"{session_path}.session" + if not Path(db_path).exists(): + return + try: + conn = sqlite3.connect(db_path, timeout=1) + conn.execute("PRAGMA journal_mode=WAL;") + conn.execute("PRAGMA synchronous=NORMAL;") + conn.close() + except Exception: + pass + + class OptimizedTelegramScraper: def __init__(self, account_id: Optional[str] = None): self.account_id = account_id @@ -1496,6 +1509,7 @@ class OptimizedTelegramScraper: return False session = account_session_path(self.SESSION_DIR, self.account_id or "session") + _ensure_session_wal(session) self.client = TelegramClient( session, self.state["api_id"], diff --git a/webui/app.js b/webui/app.js index e38c656..1fabdfb 100644 --- a/webui/app.js +++ b/webui/app.js @@ -177,6 +177,7 @@ function renderAccountPanel(accountId) { function switchAccount(accountId) { if (state.activeAccount === accountId) return; state.activeAccount = accountId; + localStorage.setItem("activeAccount", accountId); // Update tabs document.querySelectorAll(".account-tab").forEach((tab) => { @@ -488,8 +489,9 @@ async function loadAccounts() { // Render panels for each account accounts.forEach((acc) => renderAccountPanel(acc.id)); - // Determine active account - const activeId = state.activeAccount || accounts[0].id; + // Determine active account (persisted across reloads) + const savedId = localStorage.getItem("activeAccount"); + const activeId = (savedId && accounts.some(a => a.id === savedId)) ? savedId : (state.activeAccount || accounts[0].id); switchAccount(activeId); // Update settings diff --git a/webui_server.py b/webui_server.py index 5f9ce9f..031e78d 100644 --- a/webui_server.py +++ b/webui_server.py @@ -37,6 +37,7 @@ from health import health_payload from scraper_jobs import ScraperJobService from telethon import TelegramClient from telethon.errors import SessionPasswordNeededError +from telegram_scraper_with_forwarding import _ensure_session_wal BASE_DIR = Path(__file__).resolve().parent @@ -462,6 +463,7 @@ class TelegramAuthManager: if not api_id or not api_hash: raise RuntimeError("Save api_id and api_hash first for this account.") if account_id not in self.clients or self.clients[account_id] is None: + _ensure_session_wal(account_session_path(SESSION_DIR, account_id)) self.clients[account_id] = TelegramClient( account_session_path(SESSION_DIR, account_id), api_id,