2016-04-07 15:03:22 +02:00
|
|
|
#!/usr/bin/env python
|
2013-11-01 19:31:00 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
Deliver email messages that have been queued by various things
|
|
|
|
(at this time invitation reminders and day1/day2 followup emails).
|
|
|
|
|
|
|
|
This management command is run via supervisor. Do not run on multiple machines,
|
|
|
|
as you may encounter multiple sends in a specific race condition.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.management.base import BaseCommand
|
2017-01-07 12:07:12 +01:00
|
|
|
from django.core.mail import get_connection, send_mail
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2013-11-01 19:31:00 +01:00
|
|
|
from django.utils.html import format_html
|
|
|
|
|
|
|
|
from zerver.models import ScheduledJob
|
|
|
|
from zerver.lib.context_managers import lockfile
|
|
|
|
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
from datetime import datetime
|
|
|
|
from ujson import loads
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict
|
2013-11-01 19:31:00 +01:00
|
|
|
|
|
|
|
## Setup ##
|
|
|
|
log_format = "%(asctime)s: %(message)s"
|
|
|
|
logging.basicConfig(format=log_format)
|
|
|
|
|
|
|
|
formatter = logging.Formatter(log_format)
|
|
|
|
file_handler = logging.FileHandler(settings.EMAIL_DELIVERER_LOG_PATH)
|
|
|
|
file_handler.setFormatter(formatter)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(file_handler)
|
|
|
|
|
|
|
|
def send_email_job(job):
|
2016-09-12 08:07:51 +02:00
|
|
|
# type: (ScheduledJob) -> bool
|
2013-11-01 19:31:00 +01:00
|
|
|
data = loads(job.data)
|
2017-01-07 12:07:12 +01:00
|
|
|
subject = data["email_subject"]
|
|
|
|
message = data["email_text"]
|
2017-05-04 02:47:55 +02:00
|
|
|
from_email = data["from_email"]
|
2017-05-04 03:23:33 +02:00
|
|
|
to_email = data["to_email"]
|
2013-11-01 19:31:00 +01:00
|
|
|
|
|
|
|
if data["email_html"]:
|
2017-01-07 12:07:12 +01:00
|
|
|
html_message = data["email_html"]
|
|
|
|
return send_mail(subject, message, from_email, [to_email], html_message=html_message) > 0
|
|
|
|
return send_mail(subject, message, from_email, [to_email]) > 0
|
2013-11-01 19:31:00 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2016-07-17 19:38:15 +02:00
|
|
|
help = """Deliver emails queued by various parts of Zulip
|
|
|
|
(either for immediate sending or sending at a specified time).
|
2013-11-01 19:31:00 +01:00
|
|
|
|
2017-05-04 02:06:31 +02:00
|
|
|
Run this command under supervisor. This is for SMTP email delivery.
|
2013-11-01 19:31:00 +01:00
|
|
|
|
2016-11-22 01:44:16 +01:00
|
|
|
Usage: ./manage.py deliver_email
|
2013-11-01 19:31:00 +01:00
|
|
|
"""
|
|
|
|
|
2013-11-13 22:59:04 +01:00
|
|
|
def handle(self, *args, **options):
|
2016-09-12 08:07:51 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2013-11-01 19:31:00 +01:00
|
|
|
# TODO: this only acquires a lock on the system, not on the DB:
|
|
|
|
# be careful not to run this on multiple systems.
|
2016-08-13 04:24:45 +02:00
|
|
|
|
|
|
|
# In the meantime, we have an option to prevent this job from
|
|
|
|
# running on >1 machine
|
|
|
|
if settings.EMAIL_DELIVERER_DISABLED:
|
|
|
|
return
|
|
|
|
|
2013-11-01 19:31:00 +01:00
|
|
|
with lockfile("/tmp/zulip_email_deliver.lockfile"):
|
|
|
|
while True:
|
2014-02-10 21:18:13 +01:00
|
|
|
email_jobs_to_deliver = ScheduledJob.objects.filter(type=ScheduledJob.EMAIL,
|
2017-04-15 04:03:56 +02:00
|
|
|
scheduled_timestamp__lte=timezone_now())
|
2013-11-01 19:31:00 +01:00
|
|
|
if email_jobs_to_deliver:
|
|
|
|
for job in email_jobs_to_deliver:
|
|
|
|
if not send_email_job(job):
|
|
|
|
logger.warn("No exception raised, but %r sent as 0 bytes" % (job,))
|
|
|
|
else:
|
|
|
|
job.delete()
|
|
|
|
time.sleep(10)
|
|
|
|
else:
|
|
|
|
# Less load on the db during times of activity, and more responsiveness when the load is low
|
|
|
|
time.sleep(2)
|