fix: add WAL mode for session SQLite to prevent 'database is locked', persist active account tab in localStorage

This commit is contained in:
2026-06-27 15:45:05 +02:00
parent 93f2b94dd2
commit 1bd385e054
3 changed files with 20 additions and 2 deletions
+14
View File
@@ -79,6 +79,19 @@ class ForwardingRule:
enabled: bool = True 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: class OptimizedTelegramScraper:
def __init__(self, account_id: Optional[str] = None): def __init__(self, account_id: Optional[str] = None):
self.account_id = account_id self.account_id = account_id
@@ -1496,6 +1509,7 @@ class OptimizedTelegramScraper:
return False return False
session = account_session_path(self.SESSION_DIR, self.account_id or "session") session = account_session_path(self.SESSION_DIR, self.account_id or "session")
_ensure_session_wal(session)
self.client = TelegramClient( self.client = TelegramClient(
session, session,
self.state["api_id"], self.state["api_id"],
+4 -2
View File
@@ -177,6 +177,7 @@ function renderAccountPanel(accountId) {
function switchAccount(accountId) { function switchAccount(accountId) {
if (state.activeAccount === accountId) return; if (state.activeAccount === accountId) return;
state.activeAccount = accountId; state.activeAccount = accountId;
localStorage.setItem("activeAccount", accountId);
// Update tabs // Update tabs
document.querySelectorAll(".account-tab").forEach((tab) => { document.querySelectorAll(".account-tab").forEach((tab) => {
@@ -488,8 +489,9 @@ async function loadAccounts() {
// Render panels for each account // Render panels for each account
accounts.forEach((acc) => renderAccountPanel(acc.id)); accounts.forEach((acc) => renderAccountPanel(acc.id));
// Determine active account // Determine active account (persisted across reloads)
const activeId = state.activeAccount || accounts[0].id; const savedId = localStorage.getItem("activeAccount");
const activeId = (savedId && accounts.some(a => a.id === savedId)) ? savedId : (state.activeAccount || accounts[0].id);
switchAccount(activeId); switchAccount(activeId);
// Update settings // Update settings
+2
View File
@@ -37,6 +37,7 @@ from health import health_payload
from scraper_jobs import ScraperJobService from scraper_jobs import ScraperJobService
from telethon import TelegramClient from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError from telethon.errors import SessionPasswordNeededError
from telegram_scraper_with_forwarding import _ensure_session_wal
BASE_DIR = Path(__file__).resolve().parent BASE_DIR = Path(__file__).resolve().parent
@@ -462,6 +463,7 @@ class TelegramAuthManager:
if not api_id or not api_hash: if not api_id or not api_hash:
raise RuntimeError("Save api_id and api_hash first for this account.") 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: 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( self.clients[account_id] = TelegramClient(
account_session_path(SESSION_DIR, account_id), account_session_path(SESSION_DIR, account_id),
api_id, api_id,