2
0
Fork 0

Do not abort SMS send task if one fails

This commit is contained in:
Luca 2022-05-16 16:04:28 +02:00
parent 7a9e51704d
commit 48cb7d98bd
1 changed files with 35 additions and 25 deletions

View File

@ -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 return
else: send(msg)
print(f"message {msgid} not found in retry, giving up") msg.sent_at = timezone.now()
return msg.save()
msg = msgs[0] except Message.DoesNotExist:
if msg.sent_at: if not is_retry:
return print("message not found, retrying")
send(msg) send_message.apply_async((msgid, True), countdown=0.2)
msg.sent_at = timezone.now() else:
msg.save() 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