Skip to content

Commit

Permalink
Update simulate_alerts.py
Browse files Browse the repository at this point in the history
1. Configurable Sleep Interval: Added a SLEEP_INTERVAL variable to make the sleep duration configurable.

2. Improved Error Handling: Used response.raise_for_status() to raise an HTTPError for bad responses.

3. More Descriptive Logging: Enhanced the error logging to include more details about the exception.

Signed-off-by: Dhruv Kadam <[email protected]>
  • Loading branch information
DhruvKadam-git authored Oct 2, 2024
1 parent 6cbfeca commit f249dd2
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions scripts/simulate_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

def main():
GENERATE_DEDUPLICATIONS = True
SLEEP_INTERVAL = 0.2 # Configurable sleep interval
keep_api_key = os.environ.get("KEEP_API_KEY")
keep_api_url = os.environ.get("KEEP_API_URL") or "http://localhost:8080"
if keep_api_key is None or keep_api_url is None:
Expand Down Expand Up @@ -50,17 +51,18 @@ def main():
headers={"x-api-key": keep_api_key},
json=alert,
)
except Exception as e:
response.raise_for_status() # Raise an HTTPError for bad responses
except requests.exceptions.RequestException as e:
logger.error("Failed to send alert: {}".format(e))
time.sleep(0.2)
time.sleep(SLEEP_INTERVAL)
continue

if response.status_code != 202:
logger.error("Failed to send alert: {}".format(response.text))
else:
logger.info("Alert sent successfully")

time.sleep(0.2) # Wait for 0.2 seconds before sending the next alert
time.sleep(SLEEP_INTERVAL) # Wait for the configured interval before sending the next alert


if __name__ == "__main__":
Expand Down

0 comments on commit f249dd2

Please sign in to comment.