88Order matches the original behavior exactly — see plan
99main-py-is-too-long-wiggly-knuth.md.
1010"""
11+
1112import asyncio
1213import os
1314
1617from config .settings import clients , get_openrag_config
1718from services .startup_orchestrator import startup_tasks
1819from utils .logging_config import get_logger
19- from utils .telemetry import TelemetryClient , Category , MessageId
20+ from utils .telemetry import Category , MessageId , TelemetryClient
2021
2122logger = get_logger (__name__ )
2223
@@ -31,9 +32,7 @@ async def cleanup_subscriptions_proper(services):
3132
3233 all_connections = await connector_service .connection_manager .list_connections ()
3334 active_connections = [
34- c
35- for c in all_connections
36- if c .is_active and c .config .get ("webhook_channel_id" )
35+ c for c in all_connections if c .is_active and c .config .get ("webhook_channel_id" )
3736 ]
3837
3938 for connection in active_connections :
@@ -42,15 +41,11 @@ async def cleanup_subscriptions_proper(services):
4241 "Cancelling subscription for connection" ,
4342 connection_id = connection .connection_id ,
4443 )
45- connector = await connector_service .get_connector (
46- connection .connection_id
47- )
44+ connector = await connector_service .get_connector (connection .connection_id )
4845 if connector :
4946 subscription_id = connection .config .get ("webhook_channel_id" )
5047 await connector .cleanup_subscription (subscription_id )
51- logger .info (
52- "Cancelled subscription" , subscription_id = subscription_id
53- )
48+ logger .info ("Cancelled subscription" , subscription_id = subscription_id )
5449 except Exception as e :
5550 logger .error (
5651 "Failed to cancel subscription" ,
@@ -75,17 +70,13 @@ async def _periodic_backup(services):
7570
7671 config = get_openrag_config ()
7772 if not config .edited :
78- logger .debug (
79- "Onboarding not completed yet, skipping periodic backup"
80- )
73+ logger .debug ("Onboarding not completed yet, skipping periodic backup" )
8174 continue
8275
8376 flows_service = services .get ("flows_service" )
8477 if flows_service :
8578 logger .info ("Running periodic flow backup" )
86- backup_results = await flows_service .backup_all_flows (
87- only_if_changed = True
88- )
79+ backup_results = await flows_service .backup_all_flows (only_if_changed = True )
8980 if backup_results ["backed_up" ]:
9081 logger .info (
9182 "Periodic backup completed" ,
@@ -147,6 +138,7 @@ async def run_startup(app: FastAPI):
147138 # the operator owns the trade-off.
148139 from services .rbac_service import is_rbac_enforced
149140 from utils .run_mode_utils import get_run_mode
141+
150142 if is_rbac_enforced ():
151143 logger .info ("RBAC enforcement is ON" , run_mode = get_run_mode ())
152144 else :
@@ -163,6 +155,7 @@ async def run_startup(app: FastAPI):
163155 # at call time, so it will pick up the binding we set here.
164156 try :
165157 from db .engine import init_engine
158+
166159 init_engine ()
167160 except Exception as e :
168161 logger .error ("DB engine init failed at startup" , error = str (e ))
@@ -173,6 +166,7 @@ async def run_startup(app: FastAPI):
173166 try :
174167 from db .engine import SessionLocal as _SL
175168 from db .migrations_runtime import run as run_runtime_migration
169+
176170 if _SL is not None :
177171 async with _SL () as _session :
178172 await run_runtime_migration (_session )
@@ -181,9 +175,7 @@ async def run_startup(app: FastAPI):
181175 logger .error ("Runtime DB migration failed" , error = str (e ))
182176 raise
183177
184- await TelemetryClient .send_event (
185- Category .APPLICATION_STARTUP , MessageId .ORB_APP_STARTED
186- )
178+ await TelemetryClient .send_event (Category .APPLICATION_STARTUP , MessageId .ORB_APP_STARTED )
187179
188180 # FastMCP requires its own lifespan to be entered before requests
189181 # arrive so the StreamableHTTPSessionManager task group exists.
@@ -212,9 +204,7 @@ async def run_shutdown(app: FastAPI):
212204
213205 logger .info ("Application shutdown initiated" )
214206
215- await TelemetryClient .send_event (
216- Category .APPLICATION_SHUTDOWN , MessageId .ORB_APP_SHUTDOWN
217- )
207+ await TelemetryClient .send_event (Category .APPLICATION_SHUTDOWN , MessageId .ORB_APP_SHUTDOWN )
218208
219209 # Drain any pending workspace_config DB-mirror tasks before we
220210 # tear down the engine. Without this, a save_config triggered
@@ -238,6 +228,7 @@ async def run_shutdown(app: FastAPI):
238228 # Gracefully shutdown OpenSearch connection
239229 try :
240230 from utils .opensearch_utils import graceful_opensearch_shutdown
231+
241232 await graceful_opensearch_shutdown (clients .opensearch )
242233 except Exception as e :
243234 logger .error ("Error during graceful OpenSearch shutdown" , error = str (e ))
@@ -252,11 +243,13 @@ async def run_shutdown(app: FastAPI):
252243 # path did this; the live shutdown_event leaked the engine.
253244 try :
254245 from db .engine import dispose_engine
246+
255247 await dispose_engine ()
256248 except Exception as e :
257249 logger .error ("Error disposing DB engine" , error = str (e ))
258250
259251 # Cleanup telemetry client
260252 from utils .telemetry .client import cleanup_telemetry_client
253+
261254 await cleanup_telemetry_client ()
262255 logger .info ("Application shutdown completed" )
0 commit comments