Skip to content

Commit

Permalink
Reschedule rate limited telegram task instead of retry
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasb committed Oct 15, 2024
1 parent 287bfcc commit e314b71
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions engine/apps/alerts/tasks/notify_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion engine/apps/alerts/tests/test_notify_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit e314b71

Please sign in to comment.