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.utils.timezone import now as timezone_now
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2018-01-07 23:52:01 +01:00
|
|
|
|
2023-05-09 03:41:17 +02:00
|
|
|
from zerver.actions.scheduled_messages import try_deliver_one_scheduled_message
|
2018-01-07 23:52:01 +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
|
2018-01-07 23:52:01 +01:00
|
|
|
|
|
|
|
## Setup ##
|
|
|
|
logger = logging.getLogger(__name__)
|
2021-05-18 01:22:05 +02:00
|
|
|
log_to_file(logger, settings.DELIVER_SCHEDULED_MESSAGES_LOG_PATH)
|
2018-01-07 23:52:01 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-05-24 16:49:56 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2018-01-07 23:52:01 +01:00
|
|
|
help = """Deliver scheduled messages from the ScheduledMessage table.
|
|
|
|
Run this command under supervisor.
|
|
|
|
|
2021-05-18 01:14:53 +02:00
|
|
|
This management command is run via supervisor.
|
2018-01-25 23:41:17 +01:00
|
|
|
|
2018-01-07 23:52:01 +01:00
|
|
|
Usage: ./manage.py deliver_scheduled_messages
|
|
|
|
"""
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
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:
|
|
|
|
while True:
|
2023-05-09 03:41:17 +02:00
|
|
|
if try_deliver_one_scheduled_message(logger):
|
|
|
|
continue
|
2018-01-25 23:41:17 +01:00
|
|
|
|
2023-05-05 01:11:22 +02:00
|
|
|
# If there's no overdue scheduled messages, go to sleep until the next minute.
|
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
|