From 78c264d7a5ff2aa9f60830efac1aced64d24d73e Mon Sep 17 00:00:00 2001 From: Gary Caldwell Date: Thu, 8 May 2025 08:40:05 -0700 Subject: [PATCH] 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 af16d8635f17e3928da9000bb9fec5b0af920816. * 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 --- .../integrations/SlackAlerting/slack_alerting.py | 6 ++++++ .../SlackAlerting/test_slack_alerting.py | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/litellm/integrations/SlackAlerting/slack_alerting.py b/litellm/integrations/SlackAlerting/slack_alerting.py index 7e7aa4d370e..16305061ec8 100644 --- a/litellm/integrations/SlackAlerting/slack_alerting.py +++ b/litellm/integrations/SlackAlerting/slack_alerting.py @@ -85,6 +85,7 @@ class SlackAlerting(CustomBatchLogger): self.alerting_args = SlackAlertingArgs(**alerting_args) self.default_webhook_url = default_webhook_url self.flush_lock = asyncio.Lock() + self.periodic_started = False super().__init__(**kwargs, flush_lock=self.flush_lock) def update_values( @@ -99,12 +100,17 @@ class SlackAlerting(CustomBatchLogger): if alerting is not None: self.alerting = alerting asyncio.create_task(self.periodic_flush()) + self.periodic_started = True if alerting_threshold is not None: self.alerting_threshold = alerting_threshold if alert_types is not None: self.alert_types = alert_types if alerting_args is not None: self.alerting_args = SlackAlertingArgs(**alerting_args) + if not self.periodic_started: + asyncio.create_task(self.periodic_flush()) + self.periodic_started = True + if alert_to_webhook_url is not None: # update the dict if self.alert_to_webhook_url is None: diff --git a/tests/litellm/integrations/SlackAlerting/test_slack_alerting.py b/tests/litellm/integrations/SlackAlerting/test_slack_alerting.py index 425bf8b6a58..b9d0328c5f1 100644 --- a/tests/litellm/integrations/SlackAlerting/test_slack_alerting.py +++ b/tests/litellm/integrations/SlackAlerting/test_slack_alerting.py @@ -4,7 +4,7 @@ import os import sys import unittest from typing import List, Optional, Tuple -from unittest.mock import ANY, MagicMock, Mock, patch +from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch sys.path.insert( 0, os.path.abspath("../../..") @@ -161,3 +161,14 @@ class TestSlackAlerting(unittest.TestCase): ) self.assertEqual(event, "soft_budget_crossed") self.assertTrue("Total Soft Budget" in event_message) + + # Calling update_values with alerting args should try to start the periodic task + @patch("asyncio.create_task") + def test_update_values_starts_periodic_task(self, mock_create_task): + # Make it do nothing (or return a dummy future) + mock_create_task.return_value = AsyncMock() # prevents awaiting errors + + assert(self.slack_alerting.periodic_started == False) + + self.slack_alerting.update_values(alerting_args={"slack_alerting": "True"}) + assert(self.slack_alerting.periodic_started == True)