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-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2013-11-01 19:31:00 +01:00
|
|
|
|
2017-07-02 21:10:41 +02:00
|
|
|
from zerver.models import ScheduledEmail
|
2013-11-01 19:31:00 +01:00
|
|
|
from zerver.lib.context_managers import lockfile
|
2017-07-12 01:05:59 +02:00
|
|
|
from zerver.lib.send_email import send_email, EmailNotDeliveredException
|
2013-11-01 19:31:00 +01:00
|
|
|
|
|
|
|
import time
|
2017-08-25 15:25:00 +02:00
|
|
|
from zerver.lib.logging_util import create_logger
|
2013-11-01 19:31:00 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from ujson import loads
|
2017-08-07 20:04:07 +02:00
|
|
|
from typing import Any
|
2013-11-01 19:31:00 +01:00
|
|
|
|
|
|
|
## Setup ##
|
2017-08-25 15:25:00 +02:00
|
|
|
logger = create_logger(__name__, settings.EMAIL_DELIVERER_LOG_PATH, 'DEBUG')
|
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
|
2016-08-13 04:24:45 +02:00
|
|
|
|
|
|
|
if settings.EMAIL_DELIVERER_DISABLED:
|
2017-08-04 00:46:34 +02:00
|
|
|
while True:
|
|
|
|
time.sleep(10*9)
|
2016-08-13 04:24:45 +02:00
|
|
|
|
2013-11-01 19:31:00 +01:00
|
|
|
with lockfile("/tmp/zulip_email_deliver.lockfile"):
|
|
|
|
while True:
|
2017-07-02 21:10:41 +02:00
|
|
|
email_jobs_to_deliver = ScheduledEmail.objects.filter(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:
|
2017-07-12 01:05:59 +02:00
|
|
|
try:
|
|
|
|
send_email(**loads(job.data))
|
2013-11-01 19:31:00 +01:00
|
|
|
job.delete()
|
2017-07-12 01:05:59 +02:00
|
|
|
except EmailNotDeliveredException:
|
|
|
|
logger.warn("%r not delivered" % (job,))
|
2013-11-01 19:31:00 +01:00
|
|
|
time.sleep(10)
|
|
|
|
else:
|
|
|
|
# Less load on the db during times of activity, and more responsiveness when the load is low
|
|
|
|
time.sleep(2)
|