From d5ce6a27fb854c839c7e83207fe1279dd4f21807 Mon Sep 17 00:00:00 2001 From: Jimmy Royer Date: Tue, 24 Oct 2023 09:07:10 -0400 Subject: [PATCH] Overriding retry policy for SMS high priority --- app/notifications/__init__.py | 26 ++++++++++++++++++++++ app/notifications/process_notifications.py | 21 ++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/notifications/__init__.py b/app/notifications/__init__.py index e69de29bb2..bfc56cafbd 100644 --- a/app/notifications/__init__.py +++ b/app/notifications/__init__.py @@ -0,0 +1,26 @@ +from app.models import BULK, NORMAL, PRIORITY + +# Default retry policy for all notifications. +RETRY_POLICY_DEFAULT = { + "max_retries": 48, + "interval_start": 300, + "interval_step": 300, + "interval_max": 300, + "retry_errors": None, +} + +# Retry policy for high priority notifications. +RETRY_POLICY_HIGH = { + "max_retries": 48, + "interval_start": 25, + "interval_step": 25, + "interval_max": 25, + "retry_errors": None, +} + +# Retry policies for each notification priority lanes. +RETRY_POLICIES = { + BULK: RETRY_POLICY_DEFAULT, + NORMAL: RETRY_POLICY_DEFAULT, + PRIORITY: RETRY_POLICY_HIGH, +} diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index ab37b57dd2..c689b7b4a8 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -35,6 +35,7 @@ ScheduledNotification, Service, ) +from app.notifications import RETRY_POLICY_DEFAULT, RETRY_POLICIES from app.types import VerifiedNotification from app.utils import get_delivery_queue_for_template, get_template_instance from app.v2.errors import BadRequestError @@ -210,7 +211,7 @@ def db_save_and_send_notification(notification: Notification): deliver_task = choose_deliver_task(notification) try: - deliver_task.apply_async([str(notification.id)], queue=notification.queue_name) + deliver_task.apply_async([str(notification.id)], queue=notification.queue_name, **build_task_params(notification)) except Exception: dao_delete_notifications_by_id(notification.id) raise @@ -219,6 +220,24 @@ def db_save_and_send_notification(notification: Notification): ) +def build_task_params(notification: Notification): + """ + Build task params for the sending parameter tasks. + + If the notification is a high priority SMS, set the retry policy to retry every 25 seconds + else fall back to the default retry policy of retrying every 5 minutes. + """ + params = {} + params['retry'] = True + # Overring the retry policy is only supported for SMS for now; + # email support coming later. + if notification.notification_type == SMS_TYPE: + params['retry_policy'] = RETRY_POLICIES[notification.template.process_type] + else: + params['retry_policy'] = RETRY_POLICY_DEFAULT + return params + + def choose_queue(notification, research_mode, queue=None) -> QueueNames: if research_mode or notification.key_type == KEY_TYPE_TEST: queue = QueueNames.RESEARCH_MODE