Files
zulip/zerver/management/commands/deliver_email.py
Greg Price a099e698e2 py3: Switch almost all shebang lines to use python3.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2.  In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.

One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2.  See discussion on the respective previous
commits that made those explicit.  There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-16 17:54:43 -07:00

71 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""\
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. (Alternatively, you can set `EMAIL_DELIVERER_DISABLED=True`
on all but one machine to make the command have no effect.)
"""
from __future__ import absolute_import
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils.timezone import now as timezone_now
from zerver.models import ScheduledEmail
from zerver.lib.context_managers import lockfile
from zerver.lib.send_email import send_email, EmailNotDeliveredException
import time
import logging
from datetime import datetime
from ujson import loads
from typing import Any
## 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)
class Command(BaseCommand):
help = """Deliver emails queued by various parts of Zulip
(either for immediate sending or sending at a specified time).
Run this command under supervisor. This is for SMTP email delivery.
Usage: ./manage.py deliver_email
"""
def handle(self, *args, **options):
# type: (*Any, **Any) -> None
if settings.EMAIL_DELIVERER_DISABLED:
while True:
time.sleep(10*9)
with lockfile("/tmp/zulip_email_deliver.lockfile"):
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:
try:
send_email(**loads(job.data))
job.delete()
except EmailNotDeliveredException:
logger.warn("%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)