-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotification_dispatcher.py
96 lines (79 loc) · 3.24 KB
/
notification_dispatcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
This program is free software: you can redistribute it under the terms
of the GNU General Public License, v. 3.0. If a copy of the GNU General
Public License was not distributed with this file, see <https://www.gnu.org/licenses/>.
"""
import concurrent.futures
import threading
import sentry_sdk
from logutils import get_logger
from sms_outbound import send_with_twilio
from db_models import Publications
logger = get_logger(__name__)
def send_sms_notification(phone_number: str, message: str):
"""Send an SMS notification.
Args:
phone_number (str): Recipient's phone number.
message (str): Message content.
Returns:
bool: True if sent successfully, False otherwise.
"""
send_with_twilio(phone_number, message)
def send_event(
event_type: str,
details: dict = None,
message: str = None,
exception: Exception = None,
) -> None:
"""Store an event in the database.
Args:
event_type (str): The type of event (e.g., "publication")
details (dict, optional): Additional parameters.
message (str, optional): The message content.
exception (Exception, optional): The exception object.
"""
match event_type:
case "publication":
Publications.create_publication(**details)
case "sentry":
level = details.get("level")
capture_type = details.get("capture_type")
sentry = getattr(sentry_sdk, f"capture_{capture_type}")
data = message if capture_type == "message" else exception
sentry(data, level=level)
case _:
logger.error("Invalid event type: %s", event_type)
def dispatch_notifications(notifications: list):
"""Dispatch multiple notifications concurrently using ThreadPoolExecutor.
Args:
notifications (list): A list of notification dictionaries, each containing:
- notification_type (str): Type of notification ("sms", "event").
- target (str): The recipient (phone number for SMS, or event type for event storage).
- message (str, optional): The message content.
- exception (Exception, optional): The exception object.
- details (dict, optional): Additional parameters.
"""
def _dispatch(notification):
notification_type = notification.get("notification_type")
target = notification.get("target")
message = notification.get("message")
details = notification.get("details")
exception = notification.get("exception")
match notification_type:
case "sms":
send_sms_notification(phone_number=target, message=message)
case "event":
send_event(
event_type=target,
details=details,
message=message,
exception=exception,
)
case _:
logger.error("Invalid notification type: %s", notification_type)
def _run_in_background():
with concurrent.futures.ThreadPoolExecutor() as executor:
for notification in notifications:
executor.submit(_dispatch, notification)
thread = threading.Thread(target=_run_in_background, daemon=True)
thread.start()