@@ -140,19 +140,8 @@ def periodic_update():
140
140
# Run initial update
141
141
periodic_update ()
142
142
143
- # Set up periodic updates every 30 seconds
144
- def start_periodic_updates ():
145
- loop = tornado .ioloop .IOLoop .current ()
146
-
147
- def update ():
148
- periodic_update ()
149
- loop .call_later (30 , update )
150
-
151
- loop .call_later (30 , update )
152
-
153
- # Start periodic updates in the main server's IOLoop
154
- if hasattr (self .server_app , "io_loop" ) and self .server_app .io_loop :
155
- self .server_app .io_loop .add_callback (start_periodic_updates )
143
+ # Store the periodic update function to be called from the metrics server thread
144
+ self ._periodic_update = periodic_update
156
145
157
146
def start (self , port : int ) -> None :
158
147
"""Start the metrics server on the specified port.
@@ -220,6 +209,17 @@ def start(self, port: int) -> None:
220
209
def start_metrics_loop ():
221
210
loop = tornado .ioloop .IOLoop ()
222
211
loop .make_current ()
212
+
213
+ # Set up periodic updates in this IOLoop
214
+ def periodic_update_wrapper ():
215
+ if hasattr (self , '_periodic_update' ):
216
+ self ._periodic_update ()
217
+ # Schedule next update in 30 seconds
218
+ loop .call_later (30 , periodic_update_wrapper )
219
+
220
+ # Start periodic updates
221
+ loop .call_later (30 , periodic_update_wrapper )
222
+
223
223
loop .start ()
224
224
225
225
self .thread = threading .Thread (target = start_metrics_loop , daemon = True )
0 commit comments