refactor: replace all in-memory state with SQLite database

This commit is contained in:
2026-07-14 14:49:23 +08:00
parent c3132f746a
commit 54c4bf00fb
4 changed files with 218 additions and 51 deletions

47
main.py
View File

@@ -9,6 +9,7 @@ from concurrent.futures import ThreadPoolExecutor
from src.config import load_config
from src.summarizer import poll_accounts, ai_process, tg_send_and_mark
from src.tg_bot import poll_telegram, send_thinking
from src.database import init_db, is_uids_processed, mark_fetched, mark_processing, mark_sent, mark_failed
# Thread-safe logging: all log records go through a single QueueListener thread
_log_queue = queue_module.Queue(-1)
@@ -29,9 +30,6 @@ logger = logging.getLogger("main")
_running = True
_shutdown_event = threading.Event()
_pending_lock = threading.Lock()
_pending_uids: set[bytes] = set()
_processing_uids: set[bytes] = set() # 正在处理含等待TG发送的邮件UID
# Queue 1: raw emails → AI processor
_email_queue: queue_module.Queue = queue_module.Queue()
@@ -50,19 +48,18 @@ def _email_poller(cfg):
logger.info("邮件轮询线程已启动")
while _running:
try:
for acct_idx, mail in poll_accounts(cfg, skip_uids=_processing_uids):
for acct_idx, mail in poll_accounts(cfg):
if not _running:
return
with _pending_lock:
if mail.uid not in _processing_uids:
_processing_uids.add(mail.uid)
_pending_uids.add(mail.uid)
# 发送"获取邮件中"提示
try:
tg_msg_id = send_thinking(cfg.telegram, cfg.telegram.chat_id, "📥 获取邮件中...")
except Exception:
tg_msg_id = 0
_email_queue.put((acct_idx, mail, tg_msg_id))
uid_str = mail.uid.decode("utf-8", errors="replace") if isinstance(mail.uid, bytes) else str(mail.uid)
if not is_uid_processed(uid_str):
mark_fetched(uid_str, mail.account_email, mail.folder,
mail.subject, mail.sender)
try:
tg_msg_id = send_thinking(cfg.telegram, cfg.telegram.chat_id, "📥 获取邮件中…")
except Exception:
tg_msg_id = 0
_email_queue.put((acct_idx, mail, tg_msg_id))
except Exception as e:
logger.error(f"邮件轮询线程异常: {e}", exc_info=True)
time.sleep(5)
@@ -81,6 +78,8 @@ def _ai_processor(cfg):
acct_idx, mail, tg_msg_id = _email_queue.get_nowait()
except queue_module.Empty:
break
uid_str = mail.uid.decode("utf-8", errors="replace") if isinstance(mail.uid, bytes) else str(mail.uid)
mark_processing(uid_str)
future = executor.submit(ai_process, cfg, acct_idx, mail, tg_msg_id)
pending_futures[future] = (acct_idx, mail)
@@ -93,10 +92,8 @@ def _ai_processor(cfg):
_tg_queue.put(info)
except Exception as e:
logger.error(f"AI 处理邮件失败: {e}", exc_info=True)
# AI 失败也要移除 pending否则永远卡住
with _pending_lock:
_pending_uids.discard(mail.uid)
_processing_uids.discard(mail.uid)
uid_str = mail.uid.decode("utf-8", errors="replace") if isinstance(mail.uid, bytes) else str(mail.uid)
mark_failed(uid_str)
time.sleep(0.1 if pending_futures else 0.5)
@@ -114,16 +111,15 @@ def _tg_worker(cfg):
try:
info = _tg_queue.get_nowait()
tg_send_and_mark(cfg, info)
# 发送并标记已读成功后移除 pending_processing_uids 保留直到重启)
with _pending_lock:
_pending_uids.discard(info["uid"])
uid_str = info["uid"].decode("utf-8", errors="replace") if isinstance(info["uid"], bytes) else str(info["uid"])
mark_sent(uid_str)
except queue_module.Empty:
break
except Exception as e:
logger.error(f"TG 发送失败: {e}", exc_info=True)
try:
with _pending_lock:
_pending_uids.discard(info["uid"])
uid_str = info["uid"].decode("utf-8", errors="replace") if isinstance(info["uid"], bytes) else str(info["uid"])
mark_failed(uid_str)
except Exception:
pass
@@ -138,6 +134,7 @@ def main():
cfg_path = sys.argv[1] if len(sys.argv) > 1 else "config.yaml"
cfg = load_config(cfg_path)
init_db()
logger.info(f"AI邮件摘要机器人已启动轮询间隔: {cfg.polling.interval_seconds}s")
threads = [
@@ -155,8 +152,8 @@ def main():
while _running:
tick += 1
if tick % 30 == 0:
logger.info("队列状态: email_queue=%d tg_queue=%d pending_uids=%d",
_email_queue.qsize(), _tg_queue.qsize(), len(_pending_uids))
logger.info("队列状态: email_queue=%d tg_queue=%d",
_email_queue.qsize(), _tg_queue.qsize())
_shutdown_event.wait(1)
except KeyboardInterrupt:
_running = False