Skip to content

Commit bf6d7aa

Browse files
fix debounce
1 parent c030c0e commit bf6d7aa

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

service_monitor.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,20 @@
2121
TENANT = os.environ.get('TENANT')
2222
PROJECT = os.environ.get('PROJECT')
2323

24-
# Store the last trigger time for each service
25-
global last_trigger_global = 0
24+
# Store the last trigger time globally
25+
last_trigger_global = 0
2626
DEBOUNCE_INTERVAL = 180 # 3 minutes in seconds
2727

28+
def set_last_trigger(timestamp: float) -> None:
29+
"""Update the last trigger timestamp"""
30+
global last_trigger_global
31+
last_trigger_global = timestamp
32+
33+
def get_last_trigger() -> float:
34+
"""Get the last trigger timestamp"""
35+
global last_trigger_global
36+
return last_trigger_global
37+
2838
def trigger_github_workflow(gh_token: str, event_type: str, service_key: str) -> bool:
2939
"""
3040
Trigger GitHub Actions workflow with debouncing and detailed error logging
@@ -46,11 +56,12 @@ def trigger_github_workflow(gh_token: str, event_type: str, service_key: str) ->
4656
return False
4757

4858
current_time = time.time()
49-
last_trigger_time = last_trigger_global
59+
last_trigger_time = get_last_trigger()
5060

5161
# Check if enough time has passed since the last trigger
52-
if current_time - last_trigger_time < DEBOUNCE_INTERVAL:
53-
logger.info(f"Skipping workflow trigger for {service_key} due to debouncing (last trigger was {int(current_time - last_trigger_time)} seconds ago)")
62+
time_since_last = current_time - last_trigger_time
63+
if time_since_last < DEBOUNCE_INTERVAL:
64+
logger.info(f"Skipping workflow trigger for {service_key} due to debouncing (last trigger was {int(time_since_last)} seconds ago)")
5465
return False
5566

5667
try:
@@ -91,7 +102,7 @@ def trigger_github_workflow(gh_token: str, event_type: str, service_key: str) ->
91102
inputs=inputs
92103
)
93104
# Update the last trigger time only on successful dispatch
94-
last_trigger_global = current_time
105+
set_last_trigger(current_time)
95106
logger.info(f"Successfully triggered workflow {workflow.path} for event: {event_type} (service: {service_key})")
96107
return True
97108

0 commit comments

Comments
 (0)