Do not abort SMS send task if one fails

pull/1/head
Luca 3 years ago
parent 7a9e51704d
commit 48cb7d98bd

@ -27,13 +27,18 @@ def send_messages():
if not global_preferences["helper__send_sms"]: if not global_preferences["helper__send_sms"]:
print("sms disabled, not sending") print("sms disabled, not sending")
return return
msgs = Message.objects.select_for_update().filter(sent_at__isnull=True)
with transaction.atomic(): with transaction.atomic():
for msg in Message.objects.select_for_update().filter(sent_at__isnull=True): for msg in msgs:
if msg.sent_at: if msg.sent_at:
continue continue
send(msg) try:
msg.sent_at = timezone.now() send(msg)
msg.save() msg.sent_at = timezone.now()
msg.save()
except:
pass
# singlemessage so registration links arrive faster. # singlemessage so registration links arrive faster.
@ -44,30 +49,35 @@ def send_messages():
def send_message(msgid, is_retry=False): def send_message(msgid, is_retry=False):
if not global_preferences["helper__send_sms"]: if not global_preferences["helper__send_sms"]:
return return
with transaction.atomic():
msgs = Message.objects.select_for_update().filter(pk=msgid)[:1] try:
if not msgs: with transaction.atomic():
if not is_retry: msg = Message.objects.select_for_update().get(pk=msgid)
print("message not found, retrying") if msg.sent_at:
send_message.apply_async((msgid, True), countdown=0.2)
return
else:
print(f"message {msgid} not found in retry, giving up")
return return
msg = msgs[0] send(msg)
if msg.sent_at: msg.sent_at = timezone.now()
return msg.save()
send(msg) except Message.DoesNotExist:
msg.sent_at = timezone.now() if not is_retry:
msg.save() print("message not found, retrying")
send_message.apply_async((msgid, True), countdown=0.2)
else:
print(f"message {msgid} not found in retry, giving up")
@shared_task @shared_task
def send_reminders(): def send_reminders():
regs = ShiftRegistration.objects.select_for_update().filter(
reminder_sent=False,
shift__start_at__lte=timezone.now()
+ global_preferences["helper__reminder_time"],
)
with transaction.atomic(): with transaction.atomic():
for reg in ShiftRegistration.objects.select_for_update().filter( for reg in regs:
reminder_sent=False, if reg.reminder_sent:
shift__start_at__lte=timezone.now() continue
+ global_preferences["helper__reminder_time"], try:
): reg.send_reminder()
reg.send_reminder() except:
pass

Loading…
Cancel
Save