Skip to content
Snippets Groups Projects
Commit 2f4cb25f authored by Martin Mareš's avatar Martin Mareš
Browse files

Maily o chybách: Obecná funkce na odesílání

parent 69380288
No related branches found
No related tags found
1 merge request!120Mailové upozornění na interní chyby
This commit is part of merge request !120. Comments created here will be created in the context of that merge request.
...@@ -25,6 +25,9 @@ MAIL_CONTACT = "osmo@mo.mff.cuni.cz" ...@@ -25,6 +25,9 @@ MAIL_CONTACT = "osmo@mo.mff.cuni.cz"
# Odesilatel generovaných mailů (není-li definován, neposílají se) # Odesilatel generovaných mailů (není-li definován, neposílají se)
# MAIL_FROM = "osmo-auto@mo.mff.cuni.cz" # MAIL_FROM = "osmo-auto@mo.mff.cuni.cz"
# Kam posíláme maily o interních chybách (není-li definováno, neposílají se)
# MAIL_ERRORS_TO = "osmo@mo.mff.cuni.cz"
# Pro testování je možné všechny odesílané maily přesměrovat na jinou adresu # Pro testování je možné všechny odesílané maily přesměrovat na jinou adresu
# MAIL_INSTEAD = "mares@kam.mff.cuni.cz" # MAIL_INSTEAD = "mares@kam.mff.cuni.cz"
......
...@@ -5,11 +5,14 @@ import email.message ...@@ -5,11 +5,14 @@ import email.message
import email.headerregistry import email.headerregistry
import subprocess import subprocess
import textwrap import textwrap
import token_bucket
import traceback
from typing import Mapping, Optional
import urllib.parse import urllib.parse
import mo.db as db import mo.db as db
import mo.config as config import mo.config as config
from mo.util import logger from mo.util import logger, ExceptionInfo
def send_email(send_to: str, full_name: str, subject: str, body: str) -> bool: def send_email(send_to: str, full_name: str, subject: str, body: str) -> bool:
...@@ -177,3 +180,36 @@ def send_grading_info_email(dest: db.User, round: db.Round) -> bool: ...@@ -177,3 +180,36 @@ def send_grading_info_email(dest: db.User, round: db.Round) -> bool:
Váš OSMO Váš OSMO
'''), add_footer=True) '''), add_footer=True)
# Omezení rychlosti odesílání mailů o chybách děravým kyblíkem
# (pozor, každý proces má svůj kyblík)
error_tbf = token_bucket.Limiter(rate=1/300, capacity=10, storage=token_bucket.MemoryStorage())
errors_supressed = 0
def send_internal_error_email(place: str, attrs: Mapping[str, Optional[str]], exc_info: ExceptionInfo):
errors_to = getattr(config, 'MAIL_ERRORS_TO', None)
if errors_to is None:
return
global errors_supressed
if not error_tbf.consume(key="K"):
errors_supressed += 1
logger.info('Mail: Příliš mnoho chybových zpráv')
return
exc_type, exc_value, exc_traceback = exc_info
lines = [f'{key+":":10s} {val if val is not None else "-"}\n' for key, val in sorted(attrs.items())]
if exc_traceback is not None:
lines.append('\n')
lines.extend(traceback.format_exception(exc_type, exc_value, exc_traceback))
if errors_supressed > 0:
lines.append('\n')
lines.append(f'### Předchozích {errors_supressed} chyb neohlášeno\n')
logger.info('Mail: Zpráva o interní chybě')
send_email(errors_to, "", f'Interní chyba ({place})', "".join(lines))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment