fixed event shutdown
This commit is contained in:
parent
ac85c8e3dc
commit
1053d0c715
2 changed files with 28 additions and 23 deletions
50
app.py
50
app.py
|
|
@ -426,32 +426,36 @@ async def lifespan(app: FastAPI):
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
# --- Stop Managers ---
|
# --- Shutdown sequence (protected against CancelledError) ---
|
||||||
eventManager.stop()
|
|
||||||
|
|
||||||
# --- Stop Feature Containers (Plug&Play) ---
|
|
||||||
try:
|
try:
|
||||||
mainModules = loadFeatureMainModules()
|
# 1. Stop scheduler first (removes all pending cron/interval jobs)
|
||||||
for featureName, module in mainModules.items():
|
eventManager.stop()
|
||||||
if hasattr(module, "onStop"):
|
|
||||||
try:
|
|
||||||
await module.onStop(eventUser)
|
|
||||||
logger.info(f"Feature '{featureName}' stopped")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Feature '{featureName}' failed to stop: {e}")
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"Could not shutdown feature containers: {e}")
|
|
||||||
|
|
||||||
# --- Close all PostgreSQL connection pools ---
|
# 2. Stop Feature Containers (Plug&Play)
|
||||||
# Must run LAST: feature `onStop` hooks may still issue DB calls during
|
try:
|
||||||
# shutdown. Once we tear down the pools, no more borrows are possible.
|
mainModules = loadFeatureMainModules()
|
||||||
try:
|
for featureName, module in mainModules.items():
|
||||||
from modules.connectors.connectorDbPostgre import closeAllPools
|
if hasattr(module, "onStop"):
|
||||||
closeAllPools()
|
try:
|
||||||
except Exception as e:
|
await module.onStop(eventUser)
|
||||||
logger.warning(f"Closing DB connection pools failed: {e}")
|
logger.info(f"Feature '{featureName}' stopped")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Feature '{featureName}' failed to stop: {e}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Could not shutdown feature containers: {e}")
|
||||||
|
|
||||||
logger.info("Application has been shut down")
|
# 3. Close all PostgreSQL connection pools (LAST -- features may still
|
||||||
|
# issue DB calls during their onStop hooks)
|
||||||
|
try:
|
||||||
|
from modules.connectors.connectorDbPostgre import closeAllPools
|
||||||
|
closeAllPools()
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Closing DB connection pools failed: {e}")
|
||||||
|
|
||||||
|
logger.info("Application has been shut down")
|
||||||
|
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logger.info("Shutdown interrupted (CancelledError) -- resources released")
|
||||||
|
|
||||||
|
|
||||||
# Custom function to generate readable operation IDs for Swagger UI
|
# Custom function to generate readable operation IDs for Swagger UI
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,7 @@ class EventManagement:
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
if self._scheduler and self._scheduler.running:
|
if self._scheduler and self._scheduler.running:
|
||||||
try:
|
try:
|
||||||
|
self._scheduler.remove_all_jobs()
|
||||||
self._scheduler.shutdown(wait=False)
|
self._scheduler.shutdown(wait=False)
|
||||||
logger.info("EventManagement scheduler stopped")
|
logger.info("EventManagement scheduler stopped")
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue