mirror of https://github.com/zulip/zulip.git
72 lines
2.4 KiB
Python
Executable File
72 lines
2.4 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
"""\
|
|
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 django.utils.html import format_html
|
|
|
|
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, Dict
|
|
|
|
## 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)
|