2013-11-01 19:31:00 +01:00
|
|
|
|
2017-08-04 00:46:34 +02:00
|
|
|
"""\
|
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).
|
|
|
|
|
2017-08-04 00:46:34 +02:00
|
|
|
This management command is run via supervisor. Do not run on multiple
|
|
|
|
machines, as you may encounter multiple sends in a specific race
|
|
|
|
condition. (Alternatively, you can set `EMAIL_DELIVERER_DISABLED=True`
|
|
|
|
on all but one machine to make the command have no effect.)
|
2013-11-01 19:31:00 +01:00
|
|
|
"""
|
|
|
|
|
2017-12-13 01:45:57 +01:00
|
|
|
import logging
|
2017-11-16 00:43:27 +01:00
|
|
|
import time
|
|
|
|
from typing import Any
|
|
|
|
|
2013-11-01 19:31:00 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.management.base import BaseCommand
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-11-16 00:43:27 +01:00
|
|
|
from ujson import loads
|
2013-11-01 19:31:00 +01:00
|
|
|
|
2017-12-13 01:45:57 +01:00
|
|
|
from zerver.lib.logging_util import log_to_file
|
2018-08-15 21:02:56 +02:00
|
|
|
from zerver.lib.management import sleep_forever
|
2018-12-04 23:34:04 +01:00
|
|
|
from zerver.lib.send_email import EmailNotDeliveredException, send_email, \
|
|
|
|
handle_send_email_format_changes
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import ScheduledEmail
|
2013-11-01 19:31:00 +01:00
|
|
|
|
|
|
|
## Setup ##
|
2017-12-13 01:45:57 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
log_to_file(logger, settings.EMAIL_DELIVERER_LOG_PATH)
|
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
|
|
|
"""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2016-08-13 04:24:45 +02:00
|
|
|
|
|
|
|
if settings.EMAIL_DELIVERER_DISABLED:
|
2018-08-15 21:02:56 +02:00
|
|
|
sleep_forever()
|
2016-08-13 04:24:45 +02:00
|
|
|
|
2019-01-15 03:02:43 +01:00
|
|
|
while True:
|
|
|
|
email_jobs_to_deliver = ScheduledEmail.objects.filter(
|
|
|
|
scheduled_timestamp__lte=timezone_now())
|
|
|
|
if email_jobs_to_deliver:
|
|
|
|
for job in email_jobs_to_deliver:
|
|
|
|
data = loads(job.data)
|
|
|
|
handle_send_email_format_changes(data)
|
|
|
|
try:
|
|
|
|
send_email(**data)
|
|
|
|
job.delete()
|
|
|
|
except EmailNotDeliveredException:
|
|
|
|
logger.warning("%r not delivered" % (job,))
|
|
|
|
time.sleep(10)
|
|
|
|
else:
|
|
|
|
# Less load on the db during times of activity,
|
|
|
|
# and more responsiveness when the load is low
|
|
|
|
time.sleep(2)
|