26
26
from apps .user_management .models import User
27
27
28
28
29
+ RETRY_TIMEOUT_HOURS = 1
30
+
31
+
29
32
def schedule_send_bundled_notification_task (
30
33
user_notification_bundle : "UserNotificationBundle" , alert_group : "AlertGroup"
31
34
):
@@ -445,10 +448,29 @@ def perform_notification(log_record_pk, use_default_notification_policy_fallback
445
448
try :
446
449
TelegramToUserConnector .notify_user (user , alert_group , notification_policy )
447
450
except RetryAfter as e :
448
- countdown = getattr (e , "retry_after" , 3 )
449
- raise perform_notification .retry (
450
- (log_record_pk , use_default_notification_policy_fallback ), countdown = countdown , exc = e
451
- )
451
+ task_logger .exception (f"Telegram API rate limit exceeded. Retry after { e .retry_after } seconds." )
452
+ # check how much time has passed since log record was created
453
+ # to prevent eternal loop of restarting perform_notification task
454
+ if timezone .now () < log_record .created_at + timezone .timedelta (hours = RETRY_TIMEOUT_HOURS ):
455
+ countdown = getattr (e , "retry_after" , 3 )
456
+ perform_notification .apply_async (
457
+ (log_record_pk , use_default_notification_policy_fallback ), countdown = countdown
458
+ )
459
+ else :
460
+ task_logger .debug (
461
+ f"telegram notification for alert_group { alert_group .pk } failed because of rate limit"
462
+ )
463
+ UserNotificationPolicyLogRecord (
464
+ author = user ,
465
+ type = UserNotificationPolicyLogRecord .TYPE_PERSONAL_NOTIFICATION_FAILED ,
466
+ notification_policy = notification_policy ,
467
+ reason = "Telegram rate limit exceeded" ,
468
+ alert_group = alert_group ,
469
+ notification_step = notification_policy .step ,
470
+ notification_channel = notification_channel ,
471
+ notification_error_code = UserNotificationPolicyLogRecord .ERROR_NOTIFICATION_IN_TELEGRAM_RATELIMIT ,
472
+ ).save ()
473
+ return
452
474
453
475
elif notification_channel == UserNotificationPolicy .NotificationChannel .SLACK :
454
476
# TODO: refactor checking the possibility of sending a notification in slack
@@ -516,13 +538,12 @@ def perform_notification(log_record_pk, use_default_notification_policy_fallback
516
538
).save ()
517
539
return
518
540
519
- retry_timeout_hours = 1
520
541
if alert_group .slack_message :
521
542
alert_group .slack_message .send_slack_notification (user , alert_group , notification_policy )
522
543
task_logger .debug (f"Finished send_slack_notification for alert_group { alert_group .pk } ." )
523
544
# check how much time has passed since log record was created
524
545
# to prevent eternal loop of restarting perform_notification task
525
- elif timezone .now () < log_record .created_at + timezone .timedelta (hours = retry_timeout_hours ):
546
+ elif timezone .now () < log_record .created_at + timezone .timedelta (hours = RETRY_TIMEOUT_HOURS ):
526
547
task_logger .debug (
527
548
f"send_slack_notification for alert_group { alert_group .pk } failed because slack message "
528
549
f"does not exist. Restarting perform_notification."
@@ -534,7 +555,7 @@ def perform_notification(log_record_pk, use_default_notification_policy_fallback
534
555
else :
535
556
task_logger .debug (
536
557
f"send_slack_notification for alert_group { alert_group .pk } failed because slack message "
537
- f"after { retry_timeout_hours } hours still does not exist"
558
+ f"after { RETRY_TIMEOUT_HOURS } hours still does not exist"
538
559
)
539
560
UserNotificationPolicyLogRecord (
540
561
author = user ,
0 commit comments