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
|
|
|
|
2021-05-18 01:14:53 +02:00
|
|
|
This management command is run via supervisor.
|
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
|
2021-05-18 01:04:03 +02:00
|
|
|
from django.db import transaction
|
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
|
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:
|
2019-01-15 03:02:43 +01:00
|
|
|
while True:
|
2021-05-18 01:04:03 +02:00
|
|
|
with transaction.atomic():
|
2022-01-29 23:11:15 +01:00
|
|
|
job = (
|
2021-08-14 03:19:07 +02:00
|
|
|
ScheduledEmail.objects.filter(scheduled_timestamp__lte=timezone_now())
|
|
|
|
.prefetch_related("users")
|
2022-01-29 23:11:15 +01:00
|
|
|
.select_for_update(skip_locked=True)
|
|
|
|
.order_by("scheduled_timestamp")
|
|
|
|
.first()
|
2021-08-14 03:19:07 +02:00
|
|
|
)
|
2022-01-29 23:11:15 +01:00
|
|
|
if job:
|
|
|
|
try:
|
|
|
|
deliver_scheduled_emails(job)
|
|
|
|
except EmailNotDeliveredException:
|
|
|
|
logger.warning("%r not delivered", job)
|
|
|
|
else:
|
2022-01-29 23:11:15 +01:00
|
|
|
time.sleep(10)
|