2017-08-04 00:46:34 +02:00
|
|
|
"""\
|
2021-05-11 03:32:31 +02:00
|
|
|
Send email messages that have been queued for later delivery by
|
|
|
|
various things (at this time invitation reminders and day1/day2
|
|
|
|
followup emails).
|
2013-11-01 19:31:00 +01:00
|
|
|
|
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.)
|
2021-05-11 03:32:31 +02:00
|
|
|
|
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
|
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
|
2021-05-11 03:32:31 +02:00
|
|
|
from zerver.lib.send_email import EmailNotDeliveredException, deliver_scheduled_emails
|
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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2013-11-01 19:31:00 +01:00
|
|
|
class Command(BaseCommand):
|
2021-05-11 03:32:31 +02:00
|
|
|
help = """Send emails queued by various parts of Zulip
|
|
|
|
for later delivery.
|
2013-11-01 19:31:00 +01:00
|
|
|
|
2021-05-11 03:32:31 +02:00
|
|
|
Run this command under supervisor.
|
2013-11-01 19:31:00 +01:00
|
|
|
|
2021-05-11 03:32:31 +02:00
|
|
|
Usage: ./manage.py deliver_scheduled_emails
|
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(
|
2021-02-12 08:19:30 +01:00
|
|
|
scheduled_timestamp__lte=timezone_now()
|
|
|
|
)
|
2019-01-15 03:02:43 +01:00
|
|
|
if email_jobs_to_deliver:
|
|
|
|
for job in email_jobs_to_deliver:
|
|
|
|
try:
|
2021-05-11 03:32:31 +02:00
|
|
|
deliver_scheduled_emails(job)
|
2019-01-15 03:02:43 +01:00
|
|
|
except EmailNotDeliveredException:
|
2020-05-02 08:44:14 +02:00
|
|
|
logger.warning("%r not delivered", job)
|
2019-01-15 03:02:43 +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)
|