同步更新:重构路由、服务模块,更新前端构建
This commit is contained in:
@@ -43,6 +43,9 @@ from io import BytesIO
|
||||
|
||||
import db_pool
|
||||
from crypto_utils import encrypt_password, decrypt_password, is_encrypted
|
||||
from app_logger import get_logger
|
||||
|
||||
logger = get_logger("email_service")
|
||||
|
||||
|
||||
def parse_datetime(dt_str: str) -> datetime:
|
||||
@@ -230,7 +233,7 @@ def init_email_tables():
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
print("[邮件服务] 数据库表初始化完成")
|
||||
logger.info("[邮件服务] 数据库表初始化完成")
|
||||
|
||||
|
||||
# ============ SMTP配置管理 ============
|
||||
@@ -239,23 +242,6 @@ def get_email_settings() -> Dict[str, Any]:
|
||||
"""获取全局邮件设置"""
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
# 先检查表结构,添加新字段(兼容旧版本数据库)
|
||||
try:
|
||||
cursor.execute("SELECT register_verify_enabled FROM email_settings LIMIT 1")
|
||||
except:
|
||||
cursor.execute("ALTER TABLE email_settings ADD COLUMN register_verify_enabled INTEGER DEFAULT 0")
|
||||
conn.commit()
|
||||
try:
|
||||
cursor.execute("SELECT base_url FROM email_settings LIMIT 1")
|
||||
except:
|
||||
cursor.execute("ALTER TABLE email_settings ADD COLUMN base_url TEXT DEFAULT ''")
|
||||
conn.commit()
|
||||
try:
|
||||
cursor.execute("SELECT task_notify_enabled FROM email_settings LIMIT 1")
|
||||
except:
|
||||
cursor.execute("ALTER TABLE email_settings ADD COLUMN task_notify_enabled INTEGER DEFAULT 0")
|
||||
conn.commit()
|
||||
|
||||
cursor.execute("""
|
||||
SELECT enabled, failover_enabled, register_verify_enabled, base_url,
|
||||
task_notify_enabled, updated_at
|
||||
@@ -660,7 +646,7 @@ class EmailSender:
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"[邮件服务] SMTP连接失败 [{self.config['name']}]: {e}")
|
||||
logger.warning(f"[邮件服务] SMTP连接失败 [{self.config['name']}]: {e}")
|
||||
self.server = None
|
||||
raise
|
||||
|
||||
@@ -735,7 +721,7 @@ class EmailSender:
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"[邮件服务] 发送失败: {e}")
|
||||
logger.warning(f"[邮件服务] 发送失败: {e}")
|
||||
raise
|
||||
|
||||
|
||||
@@ -987,7 +973,7 @@ def reset_smtp_daily_quota():
|
||||
updated = cursor.rowcount
|
||||
conn.commit()
|
||||
if updated > 0:
|
||||
print(f"[邮件服务] 已重置 {updated} 个SMTP配置的每日配额")
|
||||
logger.info(f"[邮件服务] 已重置 {updated} 个SMTP配置的每日配额")
|
||||
return updated
|
||||
|
||||
|
||||
@@ -1639,7 +1625,7 @@ class EmailQueue:
|
||||
worker.start()
|
||||
self.workers.append(worker)
|
||||
|
||||
print(f"[邮件服务] 异步队列已启动 ({self.worker_count}个工作线程)")
|
||||
logger.info(f"[邮件服务] 异步队列已启动 ({self.worker_count}个工作线程)")
|
||||
|
||||
def stop(self):
|
||||
"""停止队列"""
|
||||
@@ -1657,7 +1643,7 @@ class EmailQueue:
|
||||
worker.join(timeout=5)
|
||||
|
||||
self.workers.clear()
|
||||
print("[邮件服务] 异步队列已停止")
|
||||
logger.info("[邮件服务] 异步队列已停止")
|
||||
|
||||
def _worker(self):
|
||||
"""工作线程"""
|
||||
@@ -1672,7 +1658,7 @@ class EmailQueue:
|
||||
except queue.Empty:
|
||||
continue
|
||||
except Exception as e:
|
||||
print(f"[邮件服务] 队列工作线程错误: {e}")
|
||||
logger.exception(f"[邮件服务] 队列工作线程错误: {e}")
|
||||
|
||||
def _process_task(self, task: Dict):
|
||||
"""处理邮件任务"""
|
||||
@@ -1702,7 +1688,7 @@ class EmailQueue:
|
||||
task['callback'](result)
|
||||
|
||||
except Exception as e:
|
||||
print(f"[邮件服务] 处理邮件任务失败: {e}")
|
||||
logger.exception(f"[邮件服务] 处理邮件任务失败: {e}")
|
||||
if task.get('callback'):
|
||||
task['callback']({'success': False, 'error': str(e)})
|
||||
|
||||
@@ -1730,7 +1716,7 @@ class EmailQueue:
|
||||
}, timeout=5)
|
||||
return True
|
||||
except queue.Full:
|
||||
print("[邮件服务] 邮件队列已满")
|
||||
logger.warning("[邮件服务] 邮件队列已满")
|
||||
return False
|
||||
|
||||
def enqueue_callable(self, func: Callable, args=None, kwargs=None, callback: Callable = None) -> bool:
|
||||
@@ -1744,7 +1730,7 @@ class EmailQueue:
|
||||
}, timeout=5)
|
||||
return True
|
||||
except queue.Full:
|
||||
print("[邮件服务] 邮件队列已满")
|
||||
logger.warning("[邮件服务] 邮件队列已满")
|
||||
return False
|
||||
|
||||
@property
|
||||
@@ -2158,7 +2144,7 @@ def send_batch_task_complete_email(
|
||||
try:
|
||||
zf.write(file_path, arcname=arcname)
|
||||
except Exception as e:
|
||||
print(f"[邮件] 写入ZIP失败: {e}")
|
||||
logger.warning(f"[邮件] 写入ZIP失败: {e}")
|
||||
|
||||
zip_size = os.path.getsize(zip_path) if zip_path and os.path.exists(zip_path) else 0
|
||||
if zip_size <= 0:
|
||||
@@ -2171,7 +2157,7 @@ def send_batch_task_complete_email(
|
||||
zip_filename = f"screenshots_{datetime.now(BEIJING_TZ).strftime('%Y%m%d_%H%M%S')}.zip"
|
||||
attachment_note = "截图已打包为ZIP附件,请查收。"
|
||||
except Exception as e:
|
||||
print(f"[邮件] 打包截图失败: {e}")
|
||||
logger.warning(f"[邮件] 打包截图失败: {e}")
|
||||
attachment_note = "截图打包失败,本次不附加附件。"
|
||||
finally:
|
||||
if zip_path and os.path.exists(zip_path):
|
||||
@@ -2200,26 +2186,13 @@ def send_batch_task_complete_email(
|
||||
body='',
|
||||
html_body=html_content,
|
||||
attachments=attachments,
|
||||
email_type='batch_task_complete'
|
||||
email_type='batch_task_complete',
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
if result['success']:
|
||||
# 记录发送日志
|
||||
log_email_send(
|
||||
email_type='batch_task_complete',
|
||||
to_email=email,
|
||||
subject=f'定时任务完成 - {schedule_name}',
|
||||
success=True
|
||||
)
|
||||
return {'success': True}
|
||||
else:
|
||||
log_email_send(
|
||||
email_type='batch_task_complete',
|
||||
to_email=email,
|
||||
subject=f'定时任务完成 - {schedule_name}',
|
||||
success=False,
|
||||
error=result.get('error', '')
|
||||
)
|
||||
return {'success': False, 'error': result.get('error', '发送失败')}
|
||||
|
||||
|
||||
@@ -2238,7 +2211,7 @@ def send_batch_task_complete_email_async(
|
||||
args=(user_id, email, username, schedule_name, browse_type, screenshots),
|
||||
)
|
||||
if not ok:
|
||||
print("[邮件] 邮件队列已满,批次任务邮件未发送")
|
||||
logger.warning("[邮件] 邮件队列已满,批次任务邮件未发送")
|
||||
|
||||
|
||||
# ============ 初始化 ============
|
||||
@@ -2247,7 +2220,10 @@ def init_email_service():
|
||||
"""初始化邮件服务"""
|
||||
init_email_tables()
|
||||
get_email_queue()
|
||||
print("[邮件服务] 初始化完成")
|
||||
try:
|
||||
logger.info("[邮件服务] 初始化完成")
|
||||
except Exception:
|
||||
print("[邮件服务] 初始化完成")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user