2013-08-29 23:41:03 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-10-16 03:55:26 +02:00
|
|
|
from django.db.transaction import commit_on_success
|
2013-09-03 22:33:20 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from postmonkey import PostMonkey, MailChimpException
|
2013-09-04 00:00:44 +02:00
|
|
|
from zerver.models import UserActivityInterval, get_user_profile_by_email, \
|
|
|
|
get_user_profile_by_id, get_prereg_user_by_email, get_client
|
2013-09-07 00:27:10 +02:00
|
|
|
from zerver.lib.queue import SimpleQueueClient, queue_json_publish
|
2013-09-04 00:00:44 +02:00
|
|
|
from zerver.lib.timestamp import timestamp_to_datetime
|
|
|
|
from zerver.lib.actions import handle_missedmessage_emails, do_send_confirmation_email, \
|
2013-09-30 17:53:46 +02:00
|
|
|
do_update_user_activity, do_update_user_activity_interval, do_update_user_presence, \
|
2013-09-07 00:27:10 +02:00
|
|
|
internal_send_message, send_local_email_template_with_delay, clear_followup_emails_queue, \
|
|
|
|
check_send_message, extract_recipients
|
2013-09-04 00:00:44 +02:00
|
|
|
from zerver.decorator import JsonableError
|
2013-10-10 20:39:43 +02:00
|
|
|
from confirmation.models import Confirmation
|
|
|
|
|
2013-09-03 22:33:20 +02:00
|
|
|
import os
|
2013-10-17 22:55:09 +02:00
|
|
|
import sys
|
2013-09-03 22:33:20 +02:00
|
|
|
import ujson
|
|
|
|
import traceback
|
|
|
|
from collections import defaultdict
|
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
import logging
|
2013-10-17 22:55:09 +02:00
|
|
|
import simplejson
|
2013-08-29 23:41:03 +02:00
|
|
|
|
|
|
|
def assign_queue(queue_name):
|
|
|
|
def decorate(clazz):
|
|
|
|
clazz.queue_name = queue_name
|
|
|
|
register_worker(queue_name, clazz)
|
|
|
|
return clazz
|
|
|
|
return decorate
|
|
|
|
|
|
|
|
worker_classes = {}
|
|
|
|
def register_worker(queue_name, clazz):
|
|
|
|
worker_classes[queue_name] = clazz
|
|
|
|
|
|
|
|
def get_worker(queue_name):
|
|
|
|
return worker_classes[queue_name]()
|
|
|
|
|
|
|
|
class QueueProcessingWorker(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.q = SimpleQueueClient()
|
|
|
|
|
2013-10-17 18:55:23 +02:00
|
|
|
def consume_and_commit(self, *args):
|
|
|
|
with commit_on_success():
|
|
|
|
self.consume(*args)
|
|
|
|
|
2013-08-29 23:41:03 +02:00
|
|
|
def start(self):
|
2013-10-17 18:55:23 +02:00
|
|
|
self.q.register_json_consumer(self.queue_name, self.consume_and_commit)
|
2013-08-29 23:41:03 +02:00
|
|
|
self.q.start_consuming()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.q.stop_consuming()
|
2013-09-03 22:33:20 +02:00
|
|
|
|
|
|
|
@assign_queue('signups')
|
|
|
|
class SignupWorker(QueueProcessingWorker):
|
|
|
|
def __init__(self):
|
|
|
|
super(SignupWorker, self).__init__()
|
|
|
|
self.pm = PostMonkey(settings.MAILCHIMP_API_KEY, timeout=10)
|
|
|
|
|
2013-10-07 17:36:38 +02:00
|
|
|
# Changes to this should also be reflected in
|
|
|
|
# zerver/management/commands/queue_followup_emails.py:queue()
|
2013-09-03 22:33:20 +02:00
|
|
|
def consume(self, ch, method, properties, data):
|
2013-10-07 17:36:38 +02:00
|
|
|
merge_vars=data['merge_vars']
|
2013-10-10 20:39:43 +02:00
|
|
|
|
|
|
|
# This should clear out any invitation reminder emails
|
|
|
|
clear_followup_emails_queue(data["EMAIL"], from_email="zulip@zulip.com")
|
|
|
|
|
2013-09-03 22:33:20 +02:00
|
|
|
try:
|
|
|
|
self.pm.listSubscribe(
|
|
|
|
id=settings.ZULIP_FRIENDS_LIST_ID,
|
|
|
|
email_address=data['EMAIL'],
|
2013-10-07 17:36:38 +02:00
|
|
|
merge_vars=merge_vars,
|
2013-09-03 22:33:20 +02:00
|
|
|
double_optin=False,
|
|
|
|
send_welcome=False)
|
|
|
|
except MailChimpException, e:
|
|
|
|
if e.code == 214:
|
|
|
|
logging.warning("Attempted to sign up already existing email to list: %s" % (data['EMAIL'],))
|
|
|
|
else:
|
|
|
|
raise e
|
|
|
|
|
2013-10-07 17:36:38 +02:00
|
|
|
email = data.get("EMAIL")
|
|
|
|
name = merge_vars.get("NAME")
|
|
|
|
#Send day 1 email
|
|
|
|
send_local_email_template_with_delay([{'email': email, 'name': name}],
|
|
|
|
"zerver/emails/followup/day1",
|
|
|
|
{'name': name},
|
|
|
|
datetime.timedelta(hours=1),
|
|
|
|
tags=["followup-emails"],
|
|
|
|
sender={'email': 'wdaher@zulip.com', 'name': 'Waseem Daher'})
|
|
|
|
#Send day 2 email
|
|
|
|
tomorrow = datetime.datetime.utcnow() + datetime.timedelta(hours=24)
|
|
|
|
# 11 AM EDT
|
|
|
|
tomorrow_morning = datetime.datetime(tomorrow.year, tomorrow.month, tomorrow.day, 15, 0)
|
|
|
|
assert(datetime.datetime.utcnow() < tomorrow_morning)
|
|
|
|
send_local_email_template_with_delay([{'email': email, 'name': name}],
|
|
|
|
"zerver/emails/followup/day2",
|
|
|
|
{'name': name},
|
|
|
|
tomorrow_morning - datetime.datetime.utcnow(),
|
|
|
|
tags=["followup-emails"],
|
|
|
|
sender={'email': 'wdaher@zulip.com', 'name': 'Waseem Daher'})
|
|
|
|
|
2013-09-03 22:33:20 +02:00
|
|
|
@assign_queue('invites')
|
|
|
|
class ConfirmationEmailWorker(QueueProcessingWorker):
|
|
|
|
def consume(self, ch, method, properties, data):
|
|
|
|
invitee = get_prereg_user_by_email(data["email"])
|
|
|
|
referrer = get_user_profile_by_email(data["referrer_email"])
|
|
|
|
do_send_confirmation_email(invitee, referrer)
|
|
|
|
|
2013-10-10 20:39:43 +02:00
|
|
|
# queue invitation reminder for two days from now.
|
|
|
|
link = Confirmation.objects.get_link_for_object(invitee)
|
|
|
|
send_local_email_template_with_delay([{'email': data["email"], 'name': ""}],
|
|
|
|
"zerver/emails/invitation/invitation_reminder_email",
|
|
|
|
{'activate_url': link, 'referrer': referrer},
|
|
|
|
datetime.timedelta(days=2),
|
|
|
|
tags=["invitation-reminders"],
|
|
|
|
sender={'email': 'zulip@zulip.com', 'name': 'Zulip'})
|
|
|
|
|
2013-09-03 22:33:20 +02:00
|
|
|
@assign_queue('user_activity')
|
|
|
|
class UserActivityWorker(QueueProcessingWorker):
|
|
|
|
def consume(self, ch, method, properties, event):
|
2013-09-04 00:00:44 +02:00
|
|
|
user_profile = get_user_profile_by_id(event["user_profile_id"])
|
|
|
|
client = get_client(event["client"])
|
|
|
|
log_time = timestamp_to_datetime(event["time"])
|
|
|
|
query = event["query"]
|
|
|
|
do_update_user_activity(user_profile, client, query, log_time)
|
|
|
|
|
|
|
|
@assign_queue('user_activity_interval')
|
|
|
|
class UserActivityIntervalWorker(QueueProcessingWorker):
|
|
|
|
def consume(self, ch, method, properties, event):
|
|
|
|
user_profile = get_user_profile_by_id(event["user_profile_id"])
|
|
|
|
log_time = timestamp_to_datetime(event["time"])
|
|
|
|
do_update_user_activity_interval(user_profile, log_time)
|
|
|
|
|
|
|
|
@assign_queue('user_presence')
|
|
|
|
class UserPresenceWorker(QueueProcessingWorker):
|
|
|
|
def consume(self, ch, method, properties, event):
|
|
|
|
logging.info("Received event: %s" % (event),)
|
|
|
|
user_profile = get_user_profile_by_id(event["user_profile_id"])
|
|
|
|
client = get_client(event["client"])
|
|
|
|
log_time = timestamp_to_datetime(event["time"])
|
|
|
|
status = event["status"]
|
|
|
|
do_update_user_presence(user_profile, client, log_time, status)
|
2013-09-03 22:33:20 +02:00
|
|
|
|
|
|
|
@assign_queue('missedmessage_emails')
|
|
|
|
class MissedMessageWorker(QueueProcessingWorker):
|
|
|
|
def start(self):
|
|
|
|
while True:
|
|
|
|
missed_events = self.q.drain_queue("missedmessage_emails", json=True)
|
|
|
|
by_recipient = defaultdict(list)
|
|
|
|
|
|
|
|
for event in missed_events:
|
|
|
|
logging.info("Received event: %s" % (event,))
|
|
|
|
by_recipient[event['user_profile_id']].append(event)
|
|
|
|
|
|
|
|
for user_profile_id, events in by_recipient.items():
|
|
|
|
handle_missedmessage_emails(user_profile_id, events)
|
|
|
|
|
|
|
|
# Aggregate all messages received every 2 minutes to let someone finish sending a batch
|
|
|
|
# of messages
|
|
|
|
time.sleep(2 * 60)
|
2013-09-30 17:53:46 +02:00
|
|
|
|
2013-10-17 22:55:09 +02:00
|
|
|
@assign_queue('feedback_messages')
|
|
|
|
class FeedbackBot(QueueProcessingWorker):
|
|
|
|
def start(self):
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '../../api'))
|
|
|
|
import zulip
|
|
|
|
self.staging_client = zulip.Client(
|
|
|
|
email=settings.DEPLOYMENT_ROLE_NAME,
|
|
|
|
api_key=settings.DEPLOYMENT_ROLE_KEY,
|
|
|
|
verbose=True,
|
|
|
|
site=settings.FEEDBACK_TARGET)
|
|
|
|
self.staging_client._register(
|
|
|
|
'forward_feedback',
|
|
|
|
method='POST',
|
|
|
|
url='deployments/feedback',
|
|
|
|
make_request=(lambda request: {'message': simplejson.dumps(request)}),
|
|
|
|
)
|
|
|
|
QueueProcessingWorker.start(self)
|
|
|
|
|
|
|
|
def consume(self, ch, method, properties, event):
|
|
|
|
self.staging_client.forward_feedback(event)
|
|
|
|
|
2013-09-30 17:53:46 +02:00
|
|
|
@assign_queue('slow_queries')
|
|
|
|
class SlowQueryWorker(QueueProcessingWorker):
|
|
|
|
def start(self):
|
|
|
|
while True:
|
|
|
|
slow_queries = self.q.drain_queue("slow_queries", json=True)
|
|
|
|
|
|
|
|
if len(slow_queries) > 0:
|
|
|
|
topic = "%s: slow queries" % (settings.STATSD_PREFIX,)
|
|
|
|
|
2013-10-02 16:20:32 +02:00
|
|
|
content = ""
|
2013-09-30 17:53:46 +02:00
|
|
|
for query in slow_queries:
|
|
|
|
content += " %s\n" % (query,)
|
|
|
|
|
2013-10-16 03:55:26 +02:00
|
|
|
with commit_on_success():
|
|
|
|
internal_send_message("error-bot@zulip.com", "stream", "logs", topic, content)
|
2013-09-30 17:53:46 +02:00
|
|
|
|
|
|
|
# Aggregate all slow query messages in 1-minute chunks to avoid message spam
|
2013-10-07 17:36:38 +02:00
|
|
|
time.sleep(1 * 60)
|
2013-09-07 00:27:10 +02:00
|
|
|
|
|
|
|
@assign_queue("message_sender")
|
|
|
|
class MessageSenderWorker(QueueProcessingWorker):
|
|
|
|
def consume(self, ch, method, properties, event):
|
|
|
|
req = event['request']
|
|
|
|
try:
|
|
|
|
sender = get_user_profile_by_id(req['sender_id'])
|
|
|
|
client = get_client(req['client_name'])
|
|
|
|
|
|
|
|
msg_id = check_send_message(sender, client, req['type'],
|
|
|
|
extract_recipients(req['to']),
|
|
|
|
req['subject'], req['content'])
|
|
|
|
resp = {"result": "success", "msg": "", "id": msg_id}
|
|
|
|
except JsonableError as e:
|
|
|
|
resp = {"result": "error", "msg": str(e)}
|
|
|
|
|
|
|
|
result = {'response': resp, 'client_meta': event['client_meta'],
|
|
|
|
'server_meta': event['server_meta']}
|
|
|
|
queue_json_publish(event['server_meta']['return_queue'], result, lambda e: None)
|
|
|
|
|