ajaybolloju commited on
Commit
a50f8bd
·
verified ·
1 Parent(s): 70d88fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -16
app.py CHANGED
@@ -9,7 +9,6 @@ import os
9
  import logging
10
  from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
11
  from dotenv import load_dotenv
12
- from contextlib import asynccontextmanager
13
 
14
  # Load environment variables from .env file (for local development)
15
  load_dotenv()
@@ -48,7 +47,7 @@ def init_db():
48
  logger.error(f"Error initializing SQLite database: {e}")
49
  raise
50
 
51
- # Run database initialization
52
  init_db()
53
 
54
  # Salesforce connection (using environment variables)
@@ -113,23 +112,17 @@ async def sync_to_salesforce():
113
 
114
  # Set up scheduler
115
  scheduler = AsyncIOScheduler()
 
116
 
117
- # Define lifespan handler for startup and shutdown
118
- @asynccontextmanager
119
- async def lifespan(app: FastAPI):
120
- # Startup
121
- scheduler.add_job(sync_to_salesforce, "interval", hours=4)
122
  scheduler.start()
123
  logger.debug("Scheduler started successfully.")
124
- try:
125
- yield
126
- finally:
127
- # Shutdown
128
- scheduler.shutdown()
129
- logger.debug("Scheduler shut down successfully.")
130
-
131
- # Attach the lifespan handler to the app
132
- app.lifespan = lifespan
133
 
134
  @app.get("/", response_class=HTMLResponse)
135
  async def index(request: Request):
 
9
  import logging
10
  from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
11
  from dotenv import load_dotenv
 
12
 
13
  # Load environment variables from .env file (for local development)
14
  load_dotenv()
 
47
  logger.error(f"Error initializing SQLite database: {e}")
48
  raise
49
 
50
+ # Run database initialization on startup
51
  init_db()
52
 
53
  # Salesforce connection (using environment variables)
 
112
 
113
  # Set up scheduler
114
  scheduler = AsyncIOScheduler()
115
+ scheduler.add_job(sync_to_salesforce, "interval", hours=4)
116
 
117
+ @app.on_event("startup")
118
+ async def startup_event():
 
 
 
119
  scheduler.start()
120
  logger.debug("Scheduler started successfully.")
121
+
122
+ @app.on_event("shutdown")
123
+ async def shutdown_event():
124
+ scheduler.shutdown()
125
+ logger.debug("Scheduler shut down successfully.")
 
 
 
 
126
 
127
  @app.get("/", response_class=HTMLResponse)
128
  async def index(request: Request):