NDOJ/dmoj/throttle_mail.py

30 lines
778 B
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
import traceback
from django.conf import settings
from django.core.cache import cache
from django.utils.log import AdminEmailHandler
DEFAULT_THROTTLE = (10, 60)
def new_email():
2022-05-14 17:57:27 +00:00
cache.add("error_email_throttle", 0, settings.DMOJ_EMAIL_THROTTLING[1])
return cache.incr("error_email_throttle")
2020-01-21 06:35:58 +00:00
class ThrottledEmailHandler(AdminEmailHandler):
def __init__(self, *args, **kwargs):
super(ThrottledEmailHandler, self).__init__(*args, **kwargs)
self.throttle = settings.DMOJ_EMAIL_THROTTLING[0]
def emit(self, record):
try:
count = new_email()
except Exception:
traceback.print_exc()
else:
if count >= self.throttle:
return
AdminEmailHandler.emit(self, record)