程序异常时发送邮件提醒,且会自动进行重试

This commit is contained in:
2024-05-21 15:42:34 +08:00
parent 72b40f4387
commit a47a22b9a0
5 changed files with 96 additions and 2 deletions

0
auto_email/__init__.py Normal file
View File

68
auto_email/error_email.py Normal file
View File

@@ -0,0 +1,68 @@
import datetime
import logging
import smtplib
from email.mime.text import MIMEText
from config.email import ERROR_EMAIL_CONFIG
def send_an_error_email(program_name, error_name, error_detail):
"""
@program_name: 运行的程序名
@error_name: 错误名
@error_detail: 错误的详细信息
@description: 程序出错是发送邮件提醒
"""
# SMTP 服务器配置
smtp_server = ERROR_EMAIL_CONFIG['smtp_server']
sender = ERROR_EMAIL_CONFIG['sender']
authorization_code = ERROR_EMAIL_CONFIG['authorization_code']
receivers = ERROR_EMAIL_CONFIG['receivers']
# 获取程序出错的时间
error_time = datetime.datetime.strftime(datetime.datetime.today(), "%Y-%m-%d %H:%M:%S:%f")
# 邮件内容
subject = "【程序异常提醒】{name} {date}".format(name=program_name, date=error_time) # 邮件的标题
content = '''<div class="emailcontent" style="width:100%;max-width:720px;text-align:left;margin:0 auto;padding-top:80px;padding-bottom:20px">
<div class="emailtitle">
<h1 style="color:#fff;background:#51a0e3;line-height:70px;font-size:24px;font-weight:400;padding-left:40px;margin:0">程序运行异常通知</h1>
<div class="emailtext" style="background:#fff;padding:20px 32px 20px">
<p style="color:#6e6e6e;font-size:13px;line-height:24px">程序:<span style="color:red;">【{program_name}】</span>运行过程中出现异常错误,下面是具体的异常信息,请及时核查处理!</p>
<table cellpadding="0" cellspacing="0" border="0" style="width:100%;border-top:1px solid #eee;border-left:1px solid #eee;color:#6e6e6e;font-size:16px;font-weight:normal">
<thead>
<tr>
<th colspan="2" style="padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center;background:#f8f8f8">程序异常详细信息</th>
</tr>
</thead>
<tbody>
<tr>
<td style="padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center;width:100px">异常简述</td>
<td style="padding:10px 20px 10px 30px;border-right:1px solid #eee;border-bottom:1px solid #eee;line-height:30px">{error_name}</td>
</tr>
<tr>
<td style="padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center">异常详情</td>
<td style="padding:10px 20px 10px 30px;border-right:1px solid #eee;border-bottom:1px solid #eee;line-height:30px">{error_detail}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
'''.format(program_name=program_name, error_name=error_name, error_detail=error_detail) # 邮件的正文部分
# 实例化一个文本对象
massage = MIMEText(content, 'html', 'utf-8')
massage['Subject'] = subject # 标题
massage['From'] = sender # 发件人
receivers_str = ','.join(receivers)
massage['To'] = receivers_str # 收件人
log = logging.getLogger()
try:
mail = smtplib.SMTP_SSL(smtp_server, 994) # 连接SMTP服务默认465和944这里用994
mail.login(sender, authorization_code) # 登录到SMTP服务
mail.sendmail(sender, receivers, massage.as_string()) # 发送邮件
mail.quit()
log.info("成功发送了一封邮件到" + receivers_str)
except smtplib.SMTPException:
log.warning("邮件发送失败!")

11
config/email.py Normal file
View File

@@ -0,0 +1,11 @@
# 程序异常短信配置
ERROR_EMAIL_CONFIG = {
# SMTP服务器地址
'smtp_server': 'smtp.163.com',
# 发件人邮箱地址
'sender': 'EchoLiu618@163.com',
# 授权码--用于登录第三方邮件客户端的专用密码,不是邮箱密码
'authorization_code': 'OKPQLIIVLVGRZYVH',
# 收件人邮箱地址
'receivers': ['1515783401@qq.com']
}

11
main.py
View File

@@ -1,5 +1,7 @@
import logging.config
import traceback
from auto_email.error_email import send_an_error_email
from config.log import LOGGING_CONFIG
from photo_review.photo_review import main
@@ -8,4 +10,13 @@ if __name__ == '__main__':
logging.config.dictConfig(LOGGING_CONFIG)
log = logging.getLogger()
log.info("照片审核开始")
# 崩溃后的重试次数
retry_time = 2
for _ in range(++retry_time):
try:
main()
except Exception as e:
log.error(traceback.format_exc())
send_an_error_email(program_name='照片审核关键信息抽取脚本', error_name=repr(e), error_detail=traceback.format_exc())
continue

View File

@@ -1,4 +1,5 @@
import json
import logging
from time import sleep
from paddlenlp import Taskflow
@@ -213,4 +214,7 @@ def main():
last_pk_phhd = pk_phhd
else:
# 没有查询到新案子,等待 5 分钟后再查
sleep(5 * 60)
sleep_minutes = 5
log = logging.getLogger()
log.info(f"暂未查询到新案子,等待{sleep_minutes}分钟...")
sleep(sleep_minutes * 60)