Skip to content

Commit

Permalink
Log when not all emails could be sent
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Apr 18, 2024
1 parent 1f22a16 commit a5c8c70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
26 changes: 20 additions & 6 deletions src/argus/notificationprofile/media/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from collections.abc import Iterable

from types import NoneType
from typing import List, Union
from typing import Union, Set
from django.db.models.query import QuerySet
from argus.auth.models import User
from ..serializers import RequestDestinationConfigSerializer
Expand All @@ -41,7 +41,7 @@ def modelinstance_to_dict(obj):
return dict_


def send_email_safely(function, additional_error=None, *args, **kwargs):
def send_email_safely(function, additional_error=None, *args, **kwargs) -> int:
try:
result = function(*args, **kwargs)
return result
Expand Down Expand Up @@ -143,15 +143,15 @@ def has_duplicate(cls, queryset: QuerySet, settings: dict) -> bool:
return queryset.filter(settings__email_address=settings["email_address"]).exists()

@classmethod
def get_relevant_addresses(cls, destinations: Iterable[DestinationConfig]) -> List[DestinationConfig]:
def get_relevant_addresses(cls, destinations: Iterable[DestinationConfig]) -> Set[DestinationConfig]:
"""Returns a list of email addresses the message should be sent to"""
email_addresses = [
destination.settings["email_address"]
for destination in destinations
if destination.media_id == cls.MEDIA_SLUG
]

return email_addresses
return set(email_addresses)

@staticmethod
def create_message_context(event: Event):
Expand Down Expand Up @@ -183,17 +183,31 @@ def send(cls, event: Event, destinations: Iterable[DestinationConfig], **_) -> b
email_addresses = cls.get_relevant_addresses(destinations=destinations)
if not email_addresses:
return False
num_emails = len(email_addresses)

subject, message, html_message = cls.create_message_context(event=event)

failed = set()
for email_address in email_addresses:
send_email_safely(
sent = send_email_safely(
send_mail,
subject=subject,
message=message,
from_email=None,
recipient_list=[email_address],
html_message=html_message,
)

if not sent: # 0 for failure otherwise 1
failed.add(email_address)

if failed:
if num_emails == len(failed):
LOG.error("Email: Failed to send to any addresses")
return False
LOG.warn(
"Email: Failed to send to %i of %i addresses",
len(failed),
num_emails,
)
LOG.debug("Email: Failed to send to:", " ".join(failed))
return True
11 changes: 8 additions & 3 deletions src/argus/notificationprofile/media/sms_as_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,22 @@ def send(cls, event: Event, destinations: Iterable[DestinationConfig], **_) -> b
return

phone_numbers = cls.get_relevant_addresses(destinations=destinations)

if not phone_numbers:
return False

# there is only one recipient, so failing to send a single message
# means something is wrong on the email server
sent = True
for phone_number in phone_numbers:
send_email_safely(
sent = send_email_safely(
send_mail,
subject=f"sms {phone_number}",
message=f"{event.description}",
from_email=None,
recipient_list=[recipient],
)
if not sent:
LOG.error("SMS: Failed to send")
break

return True
return sent

0 comments on commit a5c8c70

Please sign in to comment.