File tree Expand file tree Collapse file tree 1 file changed +15
-8
lines changed Expand file tree Collapse file tree 1 file changed +15
-8
lines changed Original file line number Diff line number Diff line change 11import logging
2+ import asyncio
23from contextlib import asynccontextmanager
34
45import uvicorn
4950
5051prometheus_scheduler = PrometheusScheduler ()
5152
53+ @repeat_every (
54+ seconds = prometheus_scheduler .service_config .get ("metrics_schedule" , 30 ),
55+ logger = logger ,
56+ raise_exceptions = False
57+ )
58+ async def schedule_metrics_calculation ():
59+ prometheus_scheduler .calculate ()
5260
5361@asynccontextmanager
5462async def lifespan (app : FastAPI ):
55-
56- @repeat_every (
57- seconds = prometheus_scheduler .service_config .get ("metrics_schedule" , 30 )
58- )
59- async def schedule_metrics_calculation ():
60- prometheus_scheduler .calculate ()
61-
62- await schedule_metrics_calculation ()
63+ task = asyncio .create_task (schedule_metrics_calculation ())
6364
6465 yield
6566
67+ task .cancel ()
68+ try :
69+ await task
70+ except asyncio .CancelledError :
71+ logger .info ("Prometheus metrics calculation task cancelled during shutdown" )
72+
6673
6774app = FastAPI (
6875 title = "TrustyAI Service API" ,
You can’t perform that action at this time.
0 commit comments