From e314b714d44d250d345f6d932e91149704080928 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Tue, 15 Oct 2024 09:02:44 -0300 Subject: [PATCH] Reschedule rate limited telegram task instead of retry --- engine/apps/alerts/tasks/notify_user.py | 6 ++++-- engine/apps/alerts/tests/test_notify_user.py | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/engine/apps/alerts/tasks/notify_user.py b/engine/apps/alerts/tasks/notify_user.py index 7ad2bb498..7d7e0dbf9 100644 --- a/engine/apps/alerts/tasks/notify_user.py +++ b/engine/apps/alerts/tasks/notify_user.py @@ -445,10 +445,12 @@ def perform_notification(log_record_pk, use_default_notification_policy_fallback try: TelegramToUserConnector.notify_user(user, alert_group, notification_policy) except RetryAfter as e: + task_logger.exception(f"Telegram API rate limit exceeded. Retry after {e.retry_after} seconds.") countdown = getattr(e, "retry_after", 3) - raise perform_notification.retry( - (log_record_pk, use_default_notification_policy_fallback), countdown=countdown, exc=e + perform_notification.apply_async( + (log_record_pk, use_default_notification_policy_fallback), countdown=countdown ) + return elif notification_channel == UserNotificationPolicy.NotificationChannel.SLACK: # TODO: refactor checking the possibility of sending a notification in slack diff --git a/engine/apps/alerts/tests/test_notify_user.py b/engine/apps/alerts/tests/test_notify_user.py index 7124f957d..114c339a5 100644 --- a/engine/apps/alerts/tests/test_notify_user.py +++ b/engine/apps/alerts/tests/test_notify_user.py @@ -360,10 +360,12 @@ def test_perform_notification_telegram_retryafter_error( countdown = 15 exc = RetryAfter(countdown) with patch.object(TelegramToUserConnector, "notify_user", side_effect=exc) as mock_notify_user: - with pytest.raises(RetryAfter): + with patch.object(perform_notification, "apply_async") as mock_apply_async: perform_notification(log_record.pk, False) mock_notify_user.assert_called_once_with(user, alert_group, user_notification_policy) + # task is rescheduled using the countdown value from the exception + mock_apply_async.assert_called_once_with((log_record.pk, False), countdown=countdown) assert alert_group.personal_log_records.last() == log_record