Skip to content

Commit 78c264d

Browse files
authored
Fix Slack alerting not working if using a DB (#10370)
* Try to add prints * more print * fix print * batch debugging * Change batch size * Revert "Change batch size" This reverts commit af16d86. * Look into periodic task problems * Fix missing periodic in slack init * Initialize periodic flush on startup * Log update_values * more logging * Fix startup * Cleanup change * Add a unit test for the change * Renamed and moved to standard * Merging in with new test * comment change * Extend the timeout because normal runs are over 5 min
1 parent b8b78f1 commit 78c264d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

litellm/integrations/SlackAlerting/slack_alerting.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def __init__(
8585
self.alerting_args = SlackAlertingArgs(**alerting_args)
8686
self.default_webhook_url = default_webhook_url
8787
self.flush_lock = asyncio.Lock()
88+
self.periodic_started = False
8889
super().__init__(**kwargs, flush_lock=self.flush_lock)
8990

9091
def update_values(
@@ -99,12 +100,17 @@ def update_values(
99100
if alerting is not None:
100101
self.alerting = alerting
101102
asyncio.create_task(self.periodic_flush())
103+
self.periodic_started = True
102104
if alerting_threshold is not None:
103105
self.alerting_threshold = alerting_threshold
104106
if alert_types is not None:
105107
self.alert_types = alert_types
106108
if alerting_args is not None:
107109
self.alerting_args = SlackAlertingArgs(**alerting_args)
110+
if not self.periodic_started:
111+
asyncio.create_task(self.periodic_flush())
112+
self.periodic_started = True
113+
108114
if alert_to_webhook_url is not None:
109115
# update the dict
110116
if self.alert_to_webhook_url is None:

tests/litellm/integrations/SlackAlerting/test_slack_alerting.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import unittest
66
from typing import List, Optional, Tuple
7-
from unittest.mock import ANY, MagicMock, Mock, patch
7+
from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch
88

99
sys.path.insert(
1010
0, os.path.abspath("../../..")
@@ -161,3 +161,14 @@ def test_get_event_and_event_message_both_budgets(self):
161161
)
162162
self.assertEqual(event, "soft_budget_crossed")
163163
self.assertTrue("Total Soft Budget" in event_message)
164+
165+
# Calling update_values with alerting args should try to start the periodic task
166+
@patch("asyncio.create_task")
167+
def test_update_values_starts_periodic_task(self, mock_create_task):
168+
# Make it do nothing (or return a dummy future)
169+
mock_create_task.return_value = AsyncMock() # prevents awaiting errors
170+
171+
assert(self.slack_alerting.periodic_started == False)
172+
173+
self.slack_alerting.update_values(alerting_args={"slack_alerting": "True"})
174+
assert(self.slack_alerting.periodic_started == True)

0 commit comments

Comments
 (0)