-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmail.py
More file actions
480 lines (438 loc) · 19.1 KB
/
mail.py
File metadata and controls
480 lines (438 loc) · 19.1 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
"""Module containing the EMailDaemon class."""
import datetime
import queue
import smtplib
import ssl
import threading
import time
from dataclasses import dataclass, field
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from typing import Any
import config
from config import print2
from utils import int_to_total_time
@dataclass(order=True)
class PrioritizedItem:
priority: int
item: Any = field(compare=False)
class EMailDaemon:
"""A daemon that receives notification messages and queues them for
sending as e-mail alerts."""
def __init__(self):
self.config = {
"smtp_server": config.MAIL_SERVER,
"smtp_port": config.MAIL_PORT,
"login": config.MAIL_LOGIN,
"password": config.MAIL_PASSWORD,
"from_address": config.MAIL_FROM_ADDRESS,
"to_address": config.MAIL_TO_ADDRESS,
"use_ssl": config.MAIL_USE_SSL,
"use_starttls": config.MAIL_USE_STARTTLS,
}
self.ssl_context = (
ssl.create_default_context()
if self.config["use_ssl"] or self.config["use_starttls"]
else None
)
self.retries = 3
self.retry_delay = 1
self.retry_delay_max = 128
self.queue = queue.PriorityQueue(maxsize=10)
self.last_sent = {}
self._lock = threading.Lock()
self.running = True
self.logged_in = False
self.last_exception = None
self.last_exception_time = datetime.datetime.now(datetime.timezone.utc)
self.thread = threading.Thread(target=self.run, daemon=True)
self.thread.start()
def run(self):
while self.running:
try:
msg, alert_type, bypass_interval = self.queue.get(timeout=1).item
if self._send_email_if_allowed(msg, alert_type, bypass_interval):
self.retry_delay = 1
else:
# If an e-mail could not be sent, reinsert it into the queue with
# retry priority and try again.
print2(
"error",
f"Message \"{msg['Subject']}\" failed to send. Retrying in {self.retry_delay} seconds.",
)
with self._lock:
self.queue.put_nowait(
PrioritizedItem(1, (msg, alert_type, bypass_interval))
)
time.sleep(self.retry_delay)
self.retry_delay = min(self.retry_delay * 2, self.retry_delay_max)
continue
except queue.Empty:
pass
time.sleep(1)
def clear_queue(self):
with self._lock:
try:
while True:
self.queue.get_nowait()
except queue.Empty:
pass
self.last_sent = {}
def _login(self, server, timeout=10):
try:
if self.config["use_ssl"]:
server = smtplib.SMTP_SSL(
self.config["smtp_server"],
self.config["smtp_port"],
self.ssl_context,
timeout=timeout,
)
else:
server = smtplib.SMTP(
self.config["smtp_server"],
self.config["smtp_port"],
timeout=timeout,
)
if self.config["use_starttls"]:
server.starttls(context=self.ssl_context)
server.login(self.config["login"], self.config["password"])
return server
except Exception as e:
raise e
def _send_email_if_allowed(
self, msg: MIMEMultipart, alert_type: str, bypass_interval: bool
):
"""Sends an e-mail message.
Messages of a given `alert_type` are sent only once every hour
and further messages of the same type within that period are
discarded, unless `bypass_interval` is True. Returns True if
the message is sent successfully or was discarded for that
reason, False otherwise.
"""
current_time = datetime.datetime.now(datetime.timezone.utc)
with self._lock:
last_sent_time: datetime.datetime = self.last_sent.get(
alert_type, datetime.datetime.min.replace(tzinfo=datetime.timezone.utc)
)
if bypass_interval or (
current_time - last_sent_time >= datetime.timedelta(hours=1)
):
sent = self._send_email(msg)
if sent:
# Add extra delay time for alert_type "schedule_error"
# to reduce the amount of redundant messages when
# multiple schedules are generated in a short time with
# the same faulty files. The extra time is the maximum
# length of a schedule, minus 1 hour, but not less than
# 0.
if alert_type != "schedule_error":
self.last_sent[alert_type] = current_time
else:
self.last_sent["schedule_error"] = (
current_time
+ datetime.timedelta(
minutes=max(0, config.SCHEDULE_UPCOMING_LENGTH - 60)
)
)
return True
return False
print2(
"notice",
f"Alert {alert_type} not sent: Less than 1 hour since last alert was sent ({last_sent_time.astimezone().strftime('%Y-%m-%d %H:%M:%S')}).",
)
return True
def _send_email(self, msg: MIMEMultipart, timeout=10):
"""Returns True if the e-mail was sent successfully, False if
an error occurred.
"""
server = None
# If the login test on program startup failed, try to log in again.
if not self.logged_in:
test_result = self.test_login()
if not test_result:
return False
retries = self.retries
while retries > 0:
try:
server = self._login(server, timeout)
server.sendmail(
self.config["from_address"],
self.config["to_address"],
msg.as_string(),
)
print2("verbose", f"Sent e-mail: \"{msg['Subject']}\"")
return True
except (
smtplib.SMTPAuthenticationError,
smtplib.SMTPNotSupportedError,
) as e:
print2(
"error",
f"Failed to login to e-mail server {self.config['smtp_server']}: {e}",
)
print2("error", "Mail features disabled.")
self.stop()
return False
except TimeoutError as e:
print2(
"error",
f"Timed out while trying to send e-mail \"{msg['Subject']}\": {e}",
)
retries -= 1
continue
except Exception as e:
print2("error", f"Failed to send e-mail \"{msg['Subject']}\": {e}")
retries -= 1
continue
finally:
if server is not None:
# Only disconnect if a connection exists.
try:
server.quit()
except smtplib.SMTPServerDisconnected as e:
print2("error",f"SMTP server error: {e}")
print2(
"error",
f"Failed to send e-mail alert \"{msg['Subject']}\" after {self.retries} attempts.",
)
return False
def add_alert(
self, alert_type: str, message="", bypass_interval=False, urgent=False, **kwargs
):
"""Add an alert to be sent by e-mail. By default, alerts are
added to a queue, to be sent 1 hour after the last e-mail of
the same `alert_type` was sent.
If `bypass_interval` is True, the alert will be sent regardless
of the last time an alert of the same `alert_type` was sent.
If `urgent` is True, the e-mail is sent immediately, bypassing
the queue and blocking execution until it is sent.
For playlist-related messages, the keyword arguments `line_num`
can be given a number.
For exception-related messages, the keyword arguments
`exception`, `exception_time`, and `traceback` can be given an
Exception object, a datetime object, and a string containing
traceback information respectively. The keyword arguments
`total_time` and `total_videos` can be given a string and int
respectively.
"""
if not self.running:
print2("verbose", f"Alert {alert_type} not sent: Mail alerts are disabled.")
return
local_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
line_num = kwargs.get("line_num")
total_time = kwargs.get("total_time")
total_videos = kwargs.get("total_videos")
priority = 0
if exception := kwargs.get("exception"):
exception_name = type(exception).__name__
exception_string = f"{exception_name}: {str(exception)}"
traceback_string = kwargs.get("traceback", "")
else:
exception_name = ""
exception_string = ""
traceback_string = ""
if exception_time := kwargs.get("exception_time"):
exception_timestamp = exception_time.strftime("%Y-%m-%d %H:%M:%S")
downtime_length = int_to_total_time(datetime.datetime.now(datetime.timezone.utc) - exception_time)
else:
exception_timestamp = ""
downtime_length = ""
# Priority list:
# 0: Urgent, send before all other messages
# 1: Retried message
# 10: Normal priority
alert_types = {
"stream_down": (
0,
"Stream offline",
f'The stream went offline due to the error "{exception_name}" at {exception_timestamp}.\n\nBefore this error, not including restarts, the stream ran for {total_time}{f" and played {total_videos} videos" if total_videos is not None else ""}.\n\nException details:\n{exception_string}',
),
"stream_resume": (
10,
"Stream resumed",
(
f'The stream reconnected at {local_time}. It recovered from the error "{exception_name}", which occurred at {exception_timestamp}. The stream was offline for {downtime_length}.\n\nException details:\n{exception_string}'
if exception and exception_time
else f"The stream reconnected at {local_time}. It was offline for {downtime_length}."
),
),
"file_retry": (
0,
f"Video {message} not found - Now retrying infinitely",
f"The video {message} {f'on playlist line {line_num} ' if line_num is not None else ''}could not be found at {local_time}. Because RETRY_ATTEMPTS is -1, it is currently retrying before the stream resumes.\n\nWarning: Due to the nature of this error, it is likely that more files in the playlist are also missing. Check {config.BASE_PATH}.",
),
"file_not_found": (
0,
f"Video {message} not found - Skipping in schedule",
f"The video {message} {f'on playlist line {line_num} ' if line_num is not None else ''}could not be found at {local_time}. The video has been skipped.\n\nWarning: Due to the nature of this error, it is likely that more files in the playlist are also missing. Check {config.BASE_PATH}.",
),
"schedule_error": (
0,
"Errors generating the schedule",
f"The following errors occurred when generating the schedule, causing videos to be skipped:\n{message}",
),
"program_error": (
0,
"Program error",
f"Mr. OTCS exited at {exception_timestamp} due to an unrecoverable error: {exception_string}\n\nMr. OTCS ran for {total_time}{f' and played {total_videos} videos' if total_videos is not None else ''}."
+ f"\n\n{traceback_string}"
if traceback_string != ""
else "",
),
"remote_success_after_error": (
0,
"Schedule upload succeeded with errors",
f"The schedule file upload to {config.REMOTE_ADDRESS} succeeded, but the following errors occurred:\n{message}",
),
"remote_error": (
0,
"Schedule upload failed",
f"The following errors occurred while trying to upload the schedule file to {config.REMOTE_ADDRESS}:\n{message}",
),
"remote_auth_failed": (
0,
"Schedule uploads disabled after authentication failure",
f"Authentication to {config.REMOTE_ADDRESS} failed, and remote uploading of the schedule file has been disabled. Reason:\n{message}",
),
"playlist_loop": (
10,
"Playlist looped",
f"The playlist looped at {local_time}.",
),
"playlist_stop": (
0,
"Playlist stopped",
f"The playlist reached a %STOP command on line {line_num} at {local_time}, and Mr. OTCS has exited.\n\nMr. OTCS ran for {total_time}{f' and played {total_videos} videos' if total_videos is not None else ''}.",
),
"playlist_end": (
0,
"Playlist ended",
f"The playlist reached the end at {local_time}, and Mr. OTCS has exited.\n\nMr. OTCS ran for {total_time}{f' and played {total_videos} videos' if total_videos is not None else ''}.",
),
"mail_command": (
10,
f"%MAIL command: {message[:50]}" if message else "%MAIL command",
f"The playlist reached a %MAIL command on line {line_num} at {local_time}."
+ (f" The message is:\n\n{message}" if message else ""),
),
"new_version": (
10,
f"New version available: {kwargs.get('version')}",
(
f"A new version of Mr. OTCS is available: {kwargs.get('version')}\n"
f"The new version can be found at {kwargs.get('url')}.\n\n"
f"Release notes:\n\n{message}"
),
),
"status_report": (
10,
"Status report",
"This is the regular Mr. OTCS status report.\n\n" + message,
),
"general": (10, "General message", message),
}
if alert_type in alert_types:
priority, subject, body = alert_types[alert_type]
body += f"\n\n\nGenerated by Mr. OTCS version {config.SCRIPT_VERSION}."
msg = MIMEMultipart()
msg["From"] = self.config["from_address"]
msg["To"] = self.config["to_address"]
msg["Subject"] = f"[{config.MAIL_PROGRAM_NAME}] {subject}"
msg["Date"] = formatdate(localtime=True)
msg.attach(MIMEText(body, "plain"))
if config.MAIL_ALERT_HIGH_PRIORITY_ERROR and alert_type in [
"stream_down",
"file_retry",
"file_not_found",
"schedule_error",
"program_error",
"remote_success_after_error",
"remote_error",
"remote_auth_failed",
]:
msg["Importance"] = "High"
msg["X-MSMail-Priority"] = "High"
msg["X-Priority"] = "1"
if not urgent:
print2(
"verbose",
f"Adding e-mail alert type {alert_type} with priority {priority} to queue:",
)
print2("verbose", f"Subject: [{config.MAIL_PROGRAM_NAME}] {subject}")
print2("verbose", body)
try:
with self._lock:
if self.queue.qsize() >= 10:
print2(
"error",
"E-mail alert queue is full. Discarding previous messages.",
)
self.clear_queue()
self.queue.put_nowait(
PrioritizedItem(
priority, (msg, alert_type, bypass_interval)
)
)
except queue.Full:
print2(
"error",
f"E-mail alert queue is full. Message \"{msg['Subject']}\" discarded.",
)
else:
print2("verbose", f"Sending urgent e-mail alert type {alert_type}:")
print2("verbose", f"Subject: [{config.MAIL_PROGRAM_NAME}] {subject}")
print2("verbose", body)
with self._lock:
sent = self._send_email(msg)
if sent:
self.last_sent[alert_type] = datetime.datetime.now(
datetime.timezone.utc
)
else:
raise ValueError(f"Unrecognized alert type: {alert_type}")
def test_login(self, timeout=10, retries=3):
"""Log in to the mail server and immediately exit."""
server = None
retries = self.retries
while retries > 0:
try:
server = self._login(server, timeout)
self.logged_in = True
return True
except (
smtplib.SMTPAuthenticationError,
smtplib.SMTPNotSupportedError,
) as e:
print2(
"error",
f"Failed to login to e-mail server {self.config['smtp_server']}: {e}",
)
print2("error", "Mail features disabled.")
self.stop()
return False
except TimeoutError:
print2(
"error",
"Timed out while trying to login.",
)
retries -= 1
continue
except Exception as e:
print2("error", f"Failed to login: {e}")
retries -= 1
continue
finally:
if server is not None:
server.quit()
print2(
"error",
f"Login test to e-mail server {self.config['smtp_server']} failed after {self.retries} attempts. Will retry upon next mail alert.",
)
return False
def stop(self):
self.running = False
self.clear_queue()
self.thread.join()
if __name__ == "__main__":
print("Run python3 main.py to start this program.")