diff --git a/shiftregister/app/tasks.py b/shiftregister/app/tasks.py index 275196f..95fd070 100644 --- a/shiftregister/app/tasks.py +++ b/shiftregister/app/tasks.py @@ -41,11 +41,20 @@ def send_messages(): # those messages will be picked up by the cron send later # once we have decided to continue sending messages @shared_task -def send_message(msgid): +def send_message(msgid, is_retry=False): if not global_preferences["helper__send_sms"]: return with transaction.atomic(): - msg = Message.objects.select_for_update().get(pk=msgid) + 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) + return + else: + print(f"message {msgid} not found in retry, giving up") + return + msg = msgs[0] if msg.sent_at: return send(msg)