fix(celery): 优化PDF邮件任务配置并增强测试
调整Celery任务的retry和timeout配置,增加详细的日志记录 增强测试脚本的错误处理和诊断信息
This commit is contained in:
@@ -10,7 +10,7 @@ import re
|
||||
from django.db.models import F
|
||||
|
||||
|
||||
@shared_task(bind=True, max_retries=3, default_retry_delay=60)
|
||||
@shared_task(bind=True, max_retries=2, default_retry_delay=30, time_limit=600, soft_time_limit=500)
|
||||
def celery_send_test_email(self, test_mode=True):
|
||||
"""
|
||||
Celery异步发送测试邮件任务
|
||||
@@ -332,7 +332,7 @@ def celery_send_html_report_email(self, include_attachment=False):
|
||||
}
|
||||
|
||||
|
||||
@shared_task(bind=True, max_retries=3, default_retry_delay=60)
|
||||
@shared_task(bind=True, max_retries=2, default_retry_delay=30, time_limit=600, soft_time_limit=500)
|
||||
def celery_send_pdf_report_email(self):
|
||||
"""
|
||||
Celery异步生成PDF报告并发送邮件
|
||||
@@ -342,6 +342,7 @@ def celery_send_pdf_report_email(self):
|
||||
"""
|
||||
task_id = self.request.id if hasattr(self, 'request') else 'unknown'
|
||||
logger.info(f"[任务 {task_id}] 开始执行PDF报告邮件发送任务")
|
||||
logger.info(f"[任务 {task_id}] 重试次数: {self.request.retries if hasattr(self, 'request') else 0}")
|
||||
|
||||
try:
|
||||
# 检查WeasyPrint是否可用
|
||||
@@ -349,8 +350,9 @@ def celery_send_pdf_report_email(self):
|
||||
try:
|
||||
from weasyprint import HTML
|
||||
weasyprint_available = True
|
||||
except ImportError:
|
||||
logger.error("[任务 {task_id}] WeasyPrint库无法导入,PDF功能将不可用")
|
||||
logger.info(f"[任务 {task_id}] ✅ WeasyPrint库导入成功")
|
||||
except ImportError as e:
|
||||
logger.error(f"[任务 {task_id}] ❌ WeasyPrint库无法导入: {e}")
|
||||
raise ValueError("WeasyPrint库无法导入,PDF功能将不可用")
|
||||
|
||||
if not weasyprint_available:
|
||||
@@ -362,21 +364,27 @@ def celery_send_pdf_report_email(self):
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
config = SystemConfig.get_config()
|
||||
logger.info(f"[任务 {task_id}] ✅ 成功获取系统配置")
|
||||
|
||||
# 优先使用sender_email,其次使用smtp_username作为发件人
|
||||
from_email = config.sender_email or config.smtp_username
|
||||
if not from_email:
|
||||
logger.error(f"[任务 {task_id}] ❌ 未配置发件邮箱")
|
||||
raise ValueError("未配置发件邮箱")
|
||||
|
||||
# 验证发件人邮箱格式
|
||||
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
|
||||
if not re.match(email_pattern, from_email):
|
||||
logger.error(f"[任务 {task_id}] ❌ 发件人邮箱格式不正确: {from_email}")
|
||||
raise ValueError(f"发件人邮箱格式不正确: {from_email}")
|
||||
|
||||
to_email = config.recipient_email or from_email
|
||||
# 验证收件人邮箱格式
|
||||
if not re.match(email_pattern, to_email):
|
||||
logger.error(f"[任务 {task_id}] ❌ 收件人邮箱格式不正确: {to_email}")
|
||||
raise ValueError(f"收件人邮箱格式不正确: {to_email}")
|
||||
recipient_list = [to_email] if isinstance(to_email, str) else to_email
|
||||
logger.info(f"[任务 {task_id}] ✅ 邮箱配置验证通过,发件人: {from_email}, 收件人: {recipient_list}")
|
||||
|
||||
# 获取SMTP配置
|
||||
host = config.smtp_server or 'localhost'
|
||||
@@ -384,20 +392,31 @@ def celery_send_pdf_report_email(self):
|
||||
username = config.smtp_username or ''
|
||||
password = config.smtp_password or ''
|
||||
use_tls = True # 默认使用TLS
|
||||
logger.info(f"[任务 {task_id}] ✅ SMTP配置: 服务器={host}, 端口={port}, 用户名={username}")
|
||||
|
||||
# 准备报告数据
|
||||
today = timezone.now().date()
|
||||
yesterday = today - timedelta(days=1)
|
||||
logger.info(f"[任务 {task_id}] ✅ 报告日期: {today}, 昨日: {yesterday}")
|
||||
|
||||
# 获取昨日记录
|
||||
logger.info(f"[任务 {task_id}] 📊 获取昨日阅读记录...")
|
||||
yesterday_reading = ReadingRecord.objects.filter(date=yesterday)
|
||||
logger.info(f"[任务 {task_id}] ✅ 昨日阅读记录: {yesterday_reading.count()} 条")
|
||||
|
||||
logger.info(f"[任务 {task_id}] 📊 获取昨日感悟记录...")
|
||||
yesterday_insight = InsightRecord.objects.filter(date=yesterday)
|
||||
logger.info(f"[任务 {task_id}] ✅ 昨日感悟记录: {yesterday_insight.count()} 条")
|
||||
|
||||
# 获取今日计划
|
||||
logger.info(f"[任务 {task_id}] 📋 获取今日计划...")
|
||||
today_plan = TodayPlan.objects.filter(date=today)
|
||||
logger.info(f"[任务 {task_id}] ✅ 今日计划: {today_plan.count()} 项")
|
||||
|
||||
# 获取家庭事项统计
|
||||
logger.info(f"[任务 {task_id}] 📊 获取家庭事项统计...")
|
||||
family_task_stats = FamilyTask.objects.values('type').annotate(count=FamilyTask.objects.values('id').filter(type=F('type')).count())
|
||||
logger.info(f"[任务 {task_id}] ✅ 家庭事项统计: {len(family_task_stats)} 个类型")
|
||||
|
||||
# 准备上下文数据
|
||||
context = {
|
||||
@@ -410,31 +429,46 @@ def celery_send_pdf_report_email(self):
|
||||
}
|
||||
|
||||
# 渲染HTML模板
|
||||
logger.info(f"[任务 {task_id}] 🎨 渲染HTML模板...")
|
||||
html_string = render_to_string('core/report_pdf.html', context)
|
||||
logger.info(f"[任务 {task_id}] ✅ HTML模板渲染成功")
|
||||
|
||||
# 生成PDF
|
||||
today_str = today.strftime('%Y-%m-%d')
|
||||
pdf_file = f"report_{today_str}.pdf"
|
||||
pdf_path = os.path.join(settings.REPORTS_ROOT, pdf_file)
|
||||
logger.info(f"[任务 {task_id}] 📄 准备生成PDF,保存路径: {pdf_path}")
|
||||
|
||||
# 确保报告目录存在
|
||||
os.makedirs(settings.REPORTS_ROOT, exist_ok=True)
|
||||
logger.info(f"[任务 {task_id}] ✅ 报告目录已准备好")
|
||||
|
||||
# 使用WeasyPrint生成PDF
|
||||
# 使用WeasyPrint生成PDF,设置超时
|
||||
logger.info(f"[任务 {task_id}] 📄 正在生成PDF...")
|
||||
from weasyprint import HTML
|
||||
HTML(string=html_string).write_pdf(pdf_path)
|
||||
HTML(string=html_string).write_pdf(pdf_path, timeout=60) # 设置60秒超时
|
||||
logger.info(f"[任务 {task_id}] ✅ PDF报告生成成功: {pdf_path}")
|
||||
|
||||
logger.info(f"[任务 {task_id}] PDF报告生成成功: {pdf_path}")
|
||||
# 检查PDF文件大小
|
||||
file_size = 0
|
||||
if os.path.exists(pdf_path):
|
||||
file_size = os.path.getsize(pdf_path) / (1024 * 1024) # MB
|
||||
logger.info(f"[任务 {task_id}] 📄 PDF文件大小: {file_size:.2f} MB")
|
||||
|
||||
# 创建邮件后端
|
||||
# 创建邮件后端,设置超时
|
||||
logger.info(f"[任务 {task_id}] 📧 正在创建SMTP连接...")
|
||||
backend = EmailBackend(
|
||||
host=host,
|
||||
port=port,
|
||||
username=username,
|
||||
password=password,
|
||||
use_tls=use_tls,
|
||||
fail_silently=False
|
||||
fail_silently=False,
|
||||
timeout=30, # 设置30秒连接超时
|
||||
ssl_certfile=None,
|
||||
ssl_keyfile=None
|
||||
)
|
||||
logger.info(f"[任务 {task_id}] ✅ SMTP连接创建成功")
|
||||
|
||||
# 创建邮件
|
||||
subject = f"[Celery] 家庭日报 {today_str} - PDF报告"
|
||||
@@ -467,23 +501,30 @@ def celery_send_pdf_report_email(self):
|
||||
)
|
||||
email.content_subtype = 'plain'
|
||||
email.encoding = 'utf-8'
|
||||
logger.info(f"[任务 {task_id}] ✅ 邮件对象创建成功")
|
||||
|
||||
# 添加PDF附件
|
||||
logger.info(f"[任务 {task_id}] 📎 正在添加PDF附件...")
|
||||
with open(pdf_path, 'rb') as f:
|
||||
email.attach(pdf_file, f.read(), 'application/pdf')
|
||||
|
||||
logger.info(f"[任务 {task_id}] 正在发送包含PDF附件的邮件...")
|
||||
logger.info(f"[任务 {task_id}] ✅ PDF附件添加成功")
|
||||
|
||||
# 发送邮件
|
||||
logger.info(f"[任务 {task_id}] 📧 正在发送邮件...")
|
||||
logger.info(f"[任务 {task_id}] 发件人: {from_email}")
|
||||
logger.info(f"[任务 {task_id}] 收件人: {recipient_list}")
|
||||
logger.info(f"[任务 {task_id}] 主题: {subject}")
|
||||
|
||||
sent_count = email.send(fail_silently=False)
|
||||
logger.info(f"[任务 {task_id}] 📧 邮件发送返回结果: {sent_count}")
|
||||
|
||||
if sent_count > 0:
|
||||
logger.success(f"[任务 {task_id}] 包含PDF附件的邮件发送成功!")
|
||||
logger.success(f"[任务 {task_id}] ✅ 包含PDF附件的邮件发送成功!")
|
||||
|
||||
# 清理生成的PDF文件
|
||||
if os.path.exists(pdf_path):
|
||||
os.remove(pdf_path)
|
||||
logger.info(f"[任务 {task_id}] 已清理生成的PDF文件: {pdf_path}")
|
||||
logger.info(f"[任务 {task_id}] 🧹 已清理生成的PDF文件: {pdf_path}")
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
@@ -491,28 +532,30 @@ def celery_send_pdf_report_email(self):
|
||||
'type': 'pdf_report',
|
||||
'sent_to': len(recipient_list),
|
||||
'pdf_file': pdf_file,
|
||||
'pdf_size': file_size,
|
||||
'timestamp': timezone.now().isoformat()
|
||||
}
|
||||
else:
|
||||
logger.error(f"[任务 {task_id}] ❌ 邮件发送返回0")
|
||||
raise Exception("邮件发送返回0")
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
error_traceback = traceback.format_exc()
|
||||
|
||||
logger.error(f"[任务 {task_id}] PDF报告邮件发送失败: {error_msg}")
|
||||
logger.error(f"[任务 {task_id}] 错误详情:\n{error_traceback}")
|
||||
logger.error(f"[任务 {task_id}] ❌ PDF报告邮件发送失败: {error_msg}")
|
||||
logger.error(f"[任务 {task_id}] 📋 错误详情:\n{error_traceback}")
|
||||
|
||||
# 检查是否应该重试
|
||||
if hasattr(self, 'request') and self.request.retries < self.max_retries:
|
||||
logger.info(f"[任务 {task_id}] 准备第 {self.request.retries + 1} 次重试...")
|
||||
logger.info(f"[任务 {task_id}] 🔄 准备第 {self.request.retries + 1} 次重试...")
|
||||
# 重试延迟指数增长
|
||||
retry_delay = 60 * (2 ** self.request.retries)
|
||||
logger.info(f"[任务 {task_id}] {retry_delay}秒后进行重试")
|
||||
retry_delay = 30 * (2 ** self.request.retries)
|
||||
logger.info(f"[任务 {task_id}] ⏱️ {retry_delay}秒后进行重试")
|
||||
|
||||
raise self.retry(exc=e, countdown=retry_delay)
|
||||
else:
|
||||
logger.error(f"[任务 {task_id}] 已达到最大重试次数 {self.max_retries},放弃重试")
|
||||
logger.error(f"[任务 {task_id}] 💥 已达到最大重试次数 {self.max_retries},放弃重试")
|
||||
|
||||
# 返回错误信息而不是抛出异常
|
||||
return {
|
||||
@@ -520,6 +563,7 @@ def celery_send_pdf_report_email(self):
|
||||
'task_id': task_id,
|
||||
'type': 'pdf_report',
|
||||
'error': error_msg,
|
||||
'error_detail': error_traceback[:500] if error_traceback else '无',
|
||||
'retries': self.request.retries if hasattr(self, 'request') else 0,
|
||||
'timestamp': timezone.now().isoformat()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user