From 44c2bd464ca42317a57555d0008737b102bc28d1 Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Thu, 27 Sep 2012 22:06:16 -0600 Subject: fixes bug in notifications delay --- askbot/models/__init__.py | 80 ++++++++++++++++++++++++++++++++++++++++++----- askbot/models/post.py | 13 +++++--- askbot/tasks.py | 69 ---------------------------------------- 3 files changed, 81 insertions(+), 81 deletions(-) diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 9c777ea7..e7e9b013 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -15,6 +15,8 @@ import hashlib import logging import urllib import uuid +from celery import states +from celery.task import task from django.core.urlresolvers import reverse, NoReverseMatch from django.db.models import signals as django_signals from django.template import Context @@ -3031,6 +3033,7 @@ def get_reply_to_addresses(user, post): return primary_addr, secondary_addr #todo: action +@task() def send_instant_notifications_about_activity_in_post( update_activity = None, post = None, @@ -3040,15 +3043,76 @@ def send_instant_notifications_about_activity_in_post( cache_key = 'instant-notification-%d' % post.thread.id old_task_id = cache.cache.get(cache_key) if old_task_id: - from celery.task.control import revoke - revoke(old_task_id, terminate=True) + if not send_instant_notifications_about_activity_in_post.ignore_result: + send_instant_notifications_about_activity_in_post.update_state(state=states.REVOKED) + return + + + if post.is_approved() is False: + return + + if recipients is None: + return + + acceptable_types = const.RESPONSE_ACTIVITY_TYPES_FOR_INSTANT_NOTIFICATIONS + + if update_activity.activity_type not in acceptable_types: + return + + #calculate some variables used in the loop below + from askbot.skins.loaders import get_template + update_type_map = const.RESPONSE_ACTIVITY_TYPE_MAP_FOR_TEMPLATES + update_type = update_type_map[update_activity.activity_type] + origin_post = post.get_origin_post() + headers = mail.thread_headers( + post, + origin_post, + update_activity.activity_type + ) + + logger = logging.getLogger() + if logger.getEffectiveLevel() <= logging.DEBUG: + log_id = uuid.uuid1() + message = 'email-alert %s, logId=%s' % (post.get_absolute_url(), log_id) + logger.debug(message) + else: + log_id = None + + + for user in recipients: + if user.is_blocked(): + continue + + reply_address, alt_reply_address = get_reply_to_addresses(user, post) + + subject_line, body_text = format_instant_notification_email( + to_user = user, + from_user = update_activity.user, + post = post, + reply_address = reply_address, + alt_reply_address = alt_reply_address, + update_type = update_type, + template = get_template('email/instant_notification.html') + ) + + headers['Reply-To'] = reply_address + try: + mail.send_mail( + subject_line=subject_line, + body_text=body_text, + recipient_list=[user.email], + related_object=origin_post, + activity_type=const.TYPE_ACTIVITY_EMAIL_UPDATE_SENT, + headers=headers, + raise_on_failure=True + ) + except askbot_exceptions.EmailNotSent, error: + logger.debug( + '%s, error=%s, logId=%s' % (user.email, error, log_id) + ) + else: + logger.debug('success %s, logId=%s' % (user.email, log_id)) - from askbot import tasks - result = tasks.send_instant_nofications.apply_async((update_activity, - post, recipients), - countdown = django_settings.NOTIFICATION_DELAY_TIME) - if not django_settings.CELERY_ALWAYS_EAGER: - cache.cache.set(cache_key, result.task_id, django_settings.NOTIFICATION_DELAY_TIME) def notify_author_of_published_revision( revision = None, was_approved = None, **kwargs diff --git a/askbot/models/post.py b/askbot/models/post.py index 57847088..edf88628 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -16,6 +16,7 @@ from django.utils.translation import ugettext as _ from django.utils.translation import ungettext from django.utils.http import urlquote as django_urlquote from django.core import exceptions as django_exceptions +from django.core import cache from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse from django.contrib.contenttypes.models import ContentType @@ -653,11 +654,15 @@ class Post(models.Model): notify_sets['for_email'] = \ [u for u in notify_sets['for_email'] if u.is_administrator()] + if not settings.CELERY_ALWAYS_EAGER: + cache_key = 'instant-notification-%d' % self.thread.id + cache.cache.set(cache_key, True, settings.NOTIFICATION_DELAY_TIME) from askbot.models import send_instant_notifications_about_activity_in_post - send_instant_notifications_about_activity_in_post( - update_activity=update_activity, - post=self, - recipients=notify_sets['for_email'], + send_instant_notifications_about_activity_in_post.apply_async(( + update_activity, + self, + notify_sets['for_email']), + countdown = settings.NOTIFICATION_DELAY_TIME ) def make_private(self, user, group_id=None): diff --git a/askbot/tasks.py b/askbot/tasks.py index 01cd3223..650b7aeb 100644 --- a/askbot/tasks.py +++ b/askbot/tasks.py @@ -168,72 +168,3 @@ def record_question_visit( actor = user, context_object = question_post, ) - -@task() -def send_instant_nofications(update_activity=None, - post=None, recipients=None): - - if post.is_approved() is False: - return - - if recipients is None: - return - - acceptable_types = const.RESPONSE_ACTIVITY_TYPES_FOR_INSTANT_NOTIFICATIONS - - if update_activity.activity_type not in acceptable_types: - return - - #calculate some variables used in the loop below - from askbot.skins.loaders import get_template - update_type_map = const.RESPONSE_ACTIVITY_TYPE_MAP_FOR_TEMPLATES - update_type = update_type_map[update_activity.activity_type] - origin_post = post.get_origin_post() - headers = mail.thread_headers( - post, - origin_post, - update_activity.activity_type - ) - - logger = logging.getLogger() - if logger.getEffectiveLevel() <= logging.DEBUG: - log_id = uuid.uuid1() - message = 'email-alert %s, logId=%s' % (post.get_absolute_url(), log_id) - logger.debug(message) - else: - log_id = None - - - for user in recipients: - if user.is_blocked(): - continue - - reply_address, alt_reply_address = get_reply_to_addresses(user, post) - - subject_line, body_text = format_instant_notification_email( - to_user = user, - from_user = update_activity.user, - post = post, - reply_address = reply_address, - alt_reply_address = alt_reply_address, - update_type = update_type, - template = get_template('email/instant_notification.html') - ) - - headers['Reply-To'] = reply_address - try: - mail.send_mail( - subject_line=subject_line, - body_text=body_text, - recipient_list=[user.email], - related_object=origin_post, - activity_type=const.TYPE_ACTIVITY_EMAIL_UPDATE_SENT, - headers=headers, - raise_on_failure=True - ) - except askbot_exceptions.EmailNotSent, error: - logger.debug( - '%s, error=%s, logId=%s' % (user.email, error, log_id) - ) - else: - logger.debug('success %s, logId=%s' % (user.email, log_id)) -- cgit v1.2.3-1-g7c22