2018-01-07 23:52:01 +01:00
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
from datetime import timedelta
|
2020-11-24 12:31:28 +01:00
|
|
|
from typing import Any
|
2018-01-07 23:52:01 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2020-09-29 20:34:38 +02:00
|
|
|
from zerver.lib.actions import build_message_send_dict, do_send_messages
|
2018-01-07 23:52:01 +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
|
2020-11-24 12:31:28 +01:00
|
|
|
from zerver.lib.message import SendMessageRequest
|
2020-01-14 21:59:46 +01:00
|
|
|
from zerver.models import Message, ScheduledMessage, get_user_by_delivery_email
|
2018-01-07 23:52:01 +01:00
|
|
|
|
|
|
|
## Setup ##
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
log_to_file(logger, settings.SCHEDULED_MESSAGE_DELIVERER_LOG_PATH)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-01-07 23:52:01 +01:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Deliver scheduled messages from the ScheduledMessage table.
|
|
|
|
Run this command under supervisor.
|
|
|
|
|
2018-01-25 23:41:17 +01: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.)
|
|
|
|
|
2018-01-07 23:52:01 +01:00
|
|
|
Usage: ./manage.py deliver_scheduled_messages
|
|
|
|
"""
|
|
|
|
|
2020-11-24 12:31:28 +01:00
|
|
|
def construct_message(self, scheduled_message: ScheduledMessage) -> SendMessageRequest:
|
2018-01-07 23:52:01 +01:00
|
|
|
message = Message()
|
2018-01-12 12:38:45 +01:00
|
|
|
original_sender = scheduled_message.sender
|
2018-01-07 23:52:01 +01:00
|
|
|
message.content = scheduled_message.content
|
|
|
|
message.recipient = scheduled_message.recipient
|
|
|
|
message.subject = scheduled_message.subject
|
2019-08-28 02:43:19 +02:00
|
|
|
message.date_sent = timezone_now()
|
2018-01-07 23:52:01 +01:00
|
|
|
message.sending_client = scheduled_message.sending_client
|
|
|
|
|
2018-01-12 12:38:45 +01:00
|
|
|
delivery_type = scheduled_message.delivery_type
|
|
|
|
if delivery_type == ScheduledMessage.SEND_LATER:
|
|
|
|
message.sender = original_sender
|
|
|
|
elif delivery_type == ScheduledMessage.REMIND:
|
2021-02-12 08:19:30 +01:00
|
|
|
message.sender = get_user_by_delivery_email(
|
|
|
|
settings.NOTIFICATION_BOT, original_sender.realm
|
|
|
|
)
|
2018-01-12 12:38:45 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
message_dict = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"message": message,
|
|
|
|
"stream": scheduled_message.stream,
|
|
|
|
"realm": scheduled_message.realm,
|
2021-02-12 08:19:30 +01:00
|
|
|
}
|
2020-09-29 20:34:38 +02:00
|
|
|
return build_message_send_dict(message_dict)
|
2018-01-07 23:52:01 +01:00
|
|
|
|
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2020-10-07 04:55:33 +02:00
|
|
|
try:
|
|
|
|
if settings.EMAIL_DELIVERER_DISABLED:
|
|
|
|
# Here doing a check and sleeping indefinitely on this setting might
|
|
|
|
# not sound right. Actually we do this check to avoid running this
|
|
|
|
# process on every server that might be in service to a realm. See
|
|
|
|
# the comment in zproject/default_settings.py file about renaming this
|
|
|
|
# setting.
|
|
|
|
sleep_forever()
|
2018-01-25 23:41:17 +01:00
|
|
|
|
2020-10-07 04:55:33 +02:00
|
|
|
while True:
|
|
|
|
messages_to_deliver = ScheduledMessage.objects.filter(
|
2021-02-12 08:19:30 +01:00
|
|
|
scheduled_timestamp__lte=timezone_now(), delivered=False
|
|
|
|
)
|
2020-10-09 06:33:19 +02:00
|
|
|
for message in messages_to_deliver:
|
|
|
|
do_send_messages([self.construct_message(message)])
|
|
|
|
message.delivered = True
|
2021-02-12 08:20:45 +01:00
|
|
|
message.save(update_fields=["delivered"])
|
2018-01-25 23:41:17 +01:00
|
|
|
|
2020-10-07 04:55:33 +02:00
|
|
|
cur_time = timezone_now()
|
|
|
|
time_next_min = (cur_time + timedelta(minutes=1)).replace(second=0, microsecond=0)
|
|
|
|
sleep_time = (time_next_min - cur_time).total_seconds()
|
|
|
|
time.sleep(sleep_time)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|