diff --git a/shiftregister/app/tasks.py b/shiftregister/app/tasks.py index 95fd070..8f3f70c 100644 --- a/shiftregister/app/tasks.py +++ b/shiftregister/app/tasks.py @@ -27,13 +27,18 @@ def send_messages(): if not global_preferences["helper__send_sms"]: print("sms disabled, not sending") return + + msgs = Message.objects.select_for_update().filter(sent_at__isnull=True) with transaction.atomic(): - for msg in Message.objects.select_for_update().filter(sent_at__isnull=True): + for msg in msgs: if msg.sent_at: continue - send(msg) - msg.sent_at = timezone.now() - msg.save() + try: + send(msg) + msg.sent_at = timezone.now() + msg.save() + except: + pass # singlemessage so registration links arrive faster. @@ -44,30 +49,35 @@ def send_messages(): def send_message(msgid, is_retry=False): if not global_preferences["helper__send_sms"]: return - with transaction.atomic(): - msgs = Message.objects.select_for_update().filter(pk=msgid)[:1] - if not msgs: - if not is_retry: - print("message not found, retrying") - send_message.apply_async((msgid, True), countdown=0.2) + + try: + with transaction.atomic(): + msg = Message.objects.select_for_update().get(pk=msgid) + if msg.sent_at: return - else: - print(f"message {msgid} not found in retry, giving up") - return - msg = msgs[0] - if msg.sent_at: - return - send(msg) - msg.sent_at = timezone.now() - msg.save() + send(msg) + msg.sent_at = timezone.now() + msg.save() + except Message.DoesNotExist: + if not is_retry: + 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 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(): - for reg in ShiftRegistration.objects.select_for_update().filter( - reminder_sent=False, - shift__start_at__lte=timezone.now() - + global_preferences["helper__reminder_time"], - ): - reg.send_reminder() + for reg in regs: + if reg.reminder_sent: + continue + try: + reg.send_reminder() + except: + pass