-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_after.py
More file actions
89 lines (69 loc) · 2.57 KB
/
task_after.py
File metadata and controls
89 lines (69 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import sys
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
import glob
# Email Configuration
SMTP_SERVER = "smtp.example.com"
SMTP_PORT = 465
SMTP_USER = "your_email@example.com"
SMTP_PASS = "your_email_password"
SENDER_EMAIL = "your_email@example.com"
RECEIVER_EMAIL = "receiver_email@example.com"
SUCCESS_KEYWORD = "Success"
def find_latest_log(script_name, log_base_dir="/ql/data/log"):
script_dir_name = os.path.splitext(script_name)[0]
script_log_dir = os.path.join(log_base_dir, script_dir_name)
if not os.path.exists(script_log_dir):
return None
try:
log_files = glob.glob(os.path.join(script_log_dir, "*.log"))
if not log_files:
return None
log_files.sort(key=os.path.getmtime, reverse=True)
return log_files[0]
except Exception:
return None
def send_email(subject, content):
try:
message = MIMEText(content, 'plain', 'utf-8')
sender_name = "青龙任务通知"
message['From'] = formataddr((Header(sender_name, 'utf-8').encode(), SENDER_EMAIL))
message['To'] = Header(RECEIVER_EMAIL, 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
if SMTP_PORT == 465:
smtpObj = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
else:
smtpObj = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
smtpObj.starttls()
smtpObj.login(SMTP_USER, SMTP_PASS)
smtpObj.sendmail(SENDER_EMAIL, [RECEIVER_EMAIL], message.as_string())
smtpObj.quit()
except smtplib.SMTPException:
pass
def main():
if len(sys.argv) < 2:
sys.exit(1)
script_path = sys.argv[1]
script_name = os.path.basename(script_path)
ql_data_dir = os.environ.get("QL_DATA_DIR", "/ql/data")
log_base_dir = os.path.join(ql_data_dir, "log")
log_file = find_latest_log(script_name, log_base_dir)
log_content = ""
if log_file:
try:
with open(log_file, 'r', encoding='utf-8') as f:
log_content = f.read()
except Exception as e:
log_content = f"Failed to read log file: {e}"
else:
log_content = f"Log file not found for script: {script_name}"
if SUCCESS_KEYWORD in log_content:
subject = f"青龙自动化任务成功: {script_name}"
else:
subject = f"青龙自动化任务失败: {script_name}"
send_email(subject, log_content)
if __name__ == "__main__":
main()