|
4 | 4 | import traceback |
5 | 5 | from typing import Any |
6 | 6 |
|
| 7 | +import plugins |
7 | 8 | import registry as registry |
8 | 9 | from base_exception import BaseSentinelaException |
9 | 10 | from configs import configs |
10 | | -from models import Alert, Issue, Notification, NotificationStatus |
11 | | -from services.slack import clear_slack_notification |
12 | | -from utils.async_tools import do_concurrently |
| 11 | +from models import Alert, Issue |
13 | 12 |
|
14 | 13 | _logger = logging.getLogger("request_handler") |
15 | 14 |
|
@@ -58,48 +57,45 @@ async def issue_drop(message_payload: dict[Any, Any]): |
58 | 57 | await issue.drop() |
59 | 58 |
|
60 | 59 |
|
61 | | -async def resend_slack_notifications(message_payload: dict[Any, Any]): |
62 | | - """Clear all the notifications for the channel and update all active alerts to queue events to |
63 | | - send the notifications again""" |
64 | | - # Get all active notifications for the channel |
65 | | - notifications = await Notification.get_all( |
66 | | - Notification.status == NotificationStatus.active, |
67 | | - Notification.target == "slack", |
68 | | - Notification.data["channel"].astext == message_payload["slack_channel"], |
69 | | - ) |
70 | | - |
71 | | - if len(notifications) == 0: |
72 | | - return |
73 | | - |
74 | | - monitors_ids = {notification.monitor_id for notification in notifications} |
75 | | - for monitor_id in monitors_ids: |
76 | | - await registry.wait_monitor_loaded(monitor_id) |
77 | | - |
78 | | - await do_concurrently(*[ |
79 | | - clear_slack_notification(notification) |
80 | | - for notification in notifications |
81 | | - ]) |
82 | | - |
83 | | - alert_ids = list({notification.alert_id for notification in notifications}) |
84 | | - alerts = await Alert.get_all(Alert.id.in_(alert_ids)) |
85 | | - await do_concurrently(*[alert.update() for alert in alerts]) |
86 | | - |
87 | | - |
88 | 60 | actions = { |
89 | 61 | "alert_acknowledge": alert_acknowledge, |
90 | 62 | "alert_lock": alert_lock, |
91 | 63 | "alert_solve": alert_solve, |
92 | 64 | "issue_drop": issue_drop, |
93 | | - "resend_slack_notifications": resend_slack_notifications, |
94 | 65 | } |
95 | 66 |
|
96 | 67 |
|
| 68 | +def get_action(action_name: str): |
| 69 | + """Get the action function by its name, checking if it is a plugin action""" |
| 70 | + if action_name.startswith("plugin."): |
| 71 | + plugin_name, action_name = action_name.split(".")[1:3] |
| 72 | + |
| 73 | + plugin = plugins.loaded_plugins.get(plugin_name) |
| 74 | + if plugin is None: |
| 75 | + _logger.warning(f"Plugin '{plugin_name}' unknown") |
| 76 | + return None |
| 77 | + |
| 78 | + plugin_actions = getattr(plugin, "actions", None) |
| 79 | + if plugin_actions is None: |
| 80 | + _logger.warning(f"Plugin '{plugin_name}' doesn't have actions") |
| 81 | + return None |
| 82 | + |
| 83 | + action = getattr(plugin_actions, action_name, None) |
| 84 | + if action is None: |
| 85 | + _logger.warning(f"Action '{plugin_name}.{action_name}' unknown") |
| 86 | + return None |
| 87 | + |
| 88 | + return action |
| 89 | + |
| 90 | + return actions.get(action_name) |
| 91 | + |
| 92 | + |
97 | 93 | async def run(message: dict[Any, Any]): |
98 | 94 | """Process a received request""" |
99 | 95 | message_payload = message["payload"] |
100 | 96 | action_name = message_payload["action"] |
101 | 97 |
|
102 | | - action = actions.get(action_name) |
| 98 | + action = get_action(action_name) |
103 | 99 |
|
104 | 100 | if action is None: |
105 | 101 | _logger.warning(f"Got request with unknown action '{json.dumps(message_payload)}'") |
|
0 commit comments