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
|
2023-06-30 21:18:29 +02:00
|
|
|
various things (e.g. invitation reminders and welcome 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
|
|
|
"""
|
2024-01-29 00:32:21 +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
|
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
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
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
|
2024-05-24 16:49:56 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.send_email import EmailNotDeliveredError, 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
|
|
|
|
2024-05-24 16:49:56 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
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
|
|
|
"""
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2022-02-18 19:52:36 +01:00
|
|
|
try:
|
|
|
|
while True:
|
2024-11-04 06:19:11 +01:00
|
|
|
with transaction.atomic(durable=True):
|
2022-02-18 19:52:36 +01:00
|
|
|
job = (
|
|
|
|
ScheduledEmail.objects.filter(scheduled_timestamp__lte=timezone_now())
|
|
|
|
.prefetch_related("users")
|
|
|
|
.select_for_update(skip_locked=True)
|
|
|
|
.order_by("scheduled_timestamp")
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if job:
|
|
|
|
try:
|
|
|
|
deliver_scheduled_emails(job)
|
2022-11-17 09:30:48 +01:00
|
|
|
except EmailNotDeliveredError:
|
2022-02-18 19:52:36 +01:00
|
|
|
logger.warning("%r not delivered", job)
|
|
|
|
else:
|
|
|
|
time.sleep(10)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|