21
21
TENANT = os .environ .get ('TENANT' )
22
22
PROJECT = os .environ .get ('PROJECT' )
23
23
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
26
26
DEBOUNCE_INTERVAL = 180 # 3 minutes in seconds
27
27
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
+
28
38
def trigger_github_workflow (gh_token : str , event_type : str , service_key : str ) -> bool :
29
39
"""
30
40
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) ->
46
56
return False
47
57
48
58
current_time = time .time ()
49
- last_trigger_time = last_trigger_global
59
+ last_trigger_time = get_last_trigger ()
50
60
51
61
# 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)" )
54
65
return False
55
66
56
67
try :
@@ -91,7 +102,7 @@ def trigger_github_workflow(gh_token: str, event_type: str, service_key: str) ->
91
102
inputs = inputs
92
103
)
93
104
# Update the last trigger time only on successful dispatch
94
- last_trigger_global = current_time
105
+ set_last_trigger ( current_time )
95
106
logger .info (f"Successfully triggered workflow { workflow .path } for event: { event_type } (service: { service_key } )" )
96
107
return True
97
108
0 commit comments