Skip to content

Commit

Permalink
deliver_scheduled_messages: Exit cleanly on Ctrl+C.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Oct 7, 2020
1 parent e23c0a9 commit efd9b19
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions zerver/management/commands/deliver_scheduled_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,29 @@ def construct_message(self, scheduled_message: ScheduledMessage) -> Dict[str, An
return build_message_send_dict(message_dict)

def handle(self, *args: Any, **options: Any) -> None:
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()

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()
while True:
messages_to_deliver = ScheduledMessage.objects.filter(
scheduled_timestamp__lte=timezone_now(),
delivered=False)
if messages_to_deliver:
for message in messages_to_deliver:
with transaction.atomic():
do_send_messages([self.construct_message(message)])
message.delivered = True
Message.objects.bulk_update(messages_to_deliver, ['delivered'])

while True:
messages_to_deliver = ScheduledMessage.objects.filter(
scheduled_timestamp__lte=timezone_now(),
delivered=False)
if messages_to_deliver:
for message in messages_to_deliver:
with transaction.atomic():
do_send_messages([self.construct_message(message)])
message.delivered = True
Message.objects.bulk_update(messages_to_deliver, ['delivered'])

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)
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

0 comments on commit efd9b19

Please sign in to comment.