From 70363cf4608506298f77d4ddf99c99896e53037f Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Wed, 8 Oct 2014 10:05:07 +0400 Subject: added try/except around each user when sending delayed email alerts --- askbot/management/commands/send_email_alerts.py | 35 +++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/askbot/management/commands/send_email_alerts.py b/askbot/management/commands/send_email_alerts.py index 39f976c4..094c35f0 100644 --- a/askbot/management/commands/send_email_alerts.py +++ b/askbot/management/commands/send_email_alerts.py @@ -13,12 +13,15 @@ from django.conf import settings as django_settings from askbot.conf import settings as askbot_settings from django.utils.datastructures import SortedDict from django.contrib.contenttypes.models import ContentType +from django.contrib.sites.models import Site from askbot import const from askbot import mail from askbot.utils.slug import slugify from askbot.utils.html import site_url +import traceback DEBUG_THIS_COMMAND = False +SITE_ID = Site.objects.get_current().id def get_all_origin_posts(mentions): origin_posts = set() @@ -87,10 +90,38 @@ class Command(NoArgsCommand): for user in User.objects.all(): try: self.send_email_alerts(user, template) - except Exception, e: - print e + except Exception: + self.report_exception(user) connection.close() + def format_debug_msg(self, user, content): + msg = u"%s site_id=%d user=%s: %s" % ( + datetime.datetime.now().strftime('%y-%m-%d %h:%m:%s'), + SITE_ID, + repr(user.username), + content + ) + return msg.encode('utf-8') + + def report_exception(self, user): + """reports exception that happened during sending email alert to user""" + message = self.format_debug_msg(user, traceback.format_exc()) + print message + admin_email = askbot_settings.ADMIN_EMAIL + try: + subject_line = u"Error processing daily/weekly notification for User '%s' for Site '%s'" % (user.username, SITE_ID) + mail.send_mail( + subject_line=subject_line.encode('utf-8'), + body_text=message, + recipient_list=[admin_email,] + ) + except: + message = u"ERROR: was unable to report this exception to %s: %s" % (admin_email, traceback.format_exc()) + print self.format_debug_msg(user, message) + else: + message = u"Sent email reporting this exception to %s" % admin_email + print self.format_debug_msg(user, message) + def get_updated_questions_for_user(self, user): """ retreive relevant question updates for the user -- cgit v1.2.3-1-g7c22