refactor: split AI processor and TG worker into separate threads
- Three independent threads: email poller, AI processor, TG worker - Two queues: _email_queue (raw emails→AI), _tg_queue (AI results→TG) - summarizer.py split: ai_process() (AI only) + tg_send_and_mark() (TG only) - AI retries no longer affect TG polling or email polling
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import logging
|
||||
from typing import Generator
|
||||
from typing import Generator, Optional
|
||||
from src.config import Config
|
||||
from src.email_client import fetch_unseen_emails, mark_as_seen, Email
|
||||
from src.ai_client import summarize_email
|
||||
@@ -21,12 +21,25 @@ def poll_accounts(cfg: Config) -> Generator[tuple[int, Email], None, None]:
|
||||
logger.error(f"轮询 {acct.username} 失败: {e}", exc_info=True)
|
||||
|
||||
|
||||
def process_email(cfg: Config, acct_idx: int, mail: Email):
|
||||
def ai_process(cfg: Config, acct_idx: int, mail: Email) -> Optional[dict]:
|
||||
acct = cfg.email_accounts[acct_idx]
|
||||
logger.info(f" 正在摘要: {mail.subject}")
|
||||
summary = summarize_email(cfg.ai, acct.username, mail.subject, mail.sender, mail.body)
|
||||
summary["recipient"] = acct.username
|
||||
text = format_summary(summary)
|
||||
send_summary(cfg.telegram, cfg.telegram.chat_id, text, summary, mail.body, acct_idx)
|
||||
mark_as_seen(acct, [mail.uid])
|
||||
logger.info(f" 已处理: {mail.subject}")
|
||||
return {
|
||||
"text": text,
|
||||
"data": summary,
|
||||
"original_body": mail.body,
|
||||
"acct_idx": acct_idx,
|
||||
"uid": mail.uid,
|
||||
}
|
||||
|
||||
|
||||
def tg_send_and_mark(cfg: Config, info: dict):
|
||||
acct = cfg.email_accounts[info["acct_idx"]]
|
||||
send_summary(cfg.telegram, cfg.telegram.chat_id,
|
||||
info["text"], info["data"],
|
||||
info["original_body"], info["acct_idx"])
|
||||
mark_as_seen(acct, [info["uid"]])
|
||||
logger.info(f" 已发送并标记已读")
|
||||
|
||||
Reference in New Issue
Block a user