fix: add WAL mode for session SQLite to prevent 'database is locked', persist active account tab in localStorage
This commit is contained in:
@@ -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"],
|
||||
|
||||
+4
-2
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user