Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,15 @@ from fastapi.responses import HTMLResponse, FileResponse
|
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from fastapi.templating import Jinja2Templates
|
| 5 |
from simple_salesforce import Salesforce
|
| 6 |
-
from
|
|
|
|
| 7 |
import os
|
| 8 |
import logging
|
| 9 |
-
import sqlite3
|
| 10 |
-
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
| 11 |
|
| 12 |
# Set up logging
|
| 13 |
logging.basicConfig(level=logging.DEBUG)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
-
# FastAPI app setup
|
| 17 |
app = FastAPI()
|
| 18 |
|
| 19 |
# Mount static files
|
|
@@ -42,11 +40,12 @@ def init_db():
|
|
| 42 |
logger.debug("SQLite database initialized successfully.")
|
| 43 |
except Exception as e:
|
| 44 |
logger.error(f"Error initializing SQLite database: {e}")
|
|
|
|
| 45 |
|
| 46 |
# Run database initialization on startup
|
| 47 |
init_db()
|
| 48 |
|
| 49 |
-
# Salesforce connection
|
| 50 |
def get_salesforce_connection():
|
| 51 |
try:
|
| 52 |
username = os.getenv("SFDC_USERNAME")
|
|
@@ -101,14 +100,15 @@ async def sync_to_salesforce():
|
|
| 101 |
conn.close()
|
| 102 |
except Exception as e:
|
| 103 |
logger.error(f"Error syncing to Salesforce: {str(e)}")
|
|
|
|
| 104 |
|
| 105 |
-
# Set up scheduler
|
| 106 |
scheduler = AsyncIOScheduler()
|
| 107 |
-
scheduler.add_job(sync_to_salesforce,
|
| 108 |
-
scheduler.start()
|
| 109 |
|
| 110 |
@app.on_event("startup")
|
| 111 |
async def startup_event():
|
|
|
|
| 112 |
logger.debug("Scheduler started successfully.")
|
| 113 |
|
| 114 |
@app.on_event("shutdown")
|
|
@@ -166,6 +166,19 @@ async def submit_case(request: Request):
|
|
| 166 |
logger.error(f"Error processing request: {str(e)}")
|
| 167 |
raise HTTPException(status_code=500, detail=str(e))
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
if __name__ == "__main__":
|
| 170 |
import uvicorn
|
| 171 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from fastapi.templating import Jinja2Templates
|
| 5 |
from simple_salesforce import Salesforce
|
| 6 |
+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
| 7 |
+
import sqlite3
|
| 8 |
import os
|
| 9 |
import logging
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
logging.basicConfig(level=logging.DEBUG)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
|
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
# Mount static files
|
|
|
|
| 40 |
logger.debug("SQLite database initialized successfully.")
|
| 41 |
except Exception as e:
|
| 42 |
logger.error(f"Error initializing SQLite database: {e}")
|
| 43 |
+
raise
|
| 44 |
|
| 45 |
# Run database initialization on startup
|
| 46 |
init_db()
|
| 47 |
|
| 48 |
+
# Salesforce connection (using Space Secrets)
|
| 49 |
def get_salesforce_connection():
|
| 50 |
try:
|
| 51 |
username = os.getenv("SFDC_USERNAME")
|
|
|
|
| 100 |
conn.close()
|
| 101 |
except Exception as e:
|
| 102 |
logger.error(f"Error syncing to Salesforce: {str(e)}")
|
| 103 |
+
raise # Raise the exception to see the error in the response
|
| 104 |
|
| 105 |
+
# Set up scheduler
|
| 106 |
scheduler = AsyncIOScheduler()
|
| 107 |
+
scheduler.add_job(sync_to_salesforce, "interval", hours=4)
|
|
|
|
| 108 |
|
| 109 |
@app.on_event("startup")
|
| 110 |
async def startup_event():
|
| 111 |
+
scheduler.start()
|
| 112 |
logger.debug("Scheduler started successfully.")
|
| 113 |
|
| 114 |
@app.on_event("shutdown")
|
|
|
|
| 166 |
logger.error(f"Error processing request: {str(e)}")
|
| 167 |
raise HTTPException(status_code=500, detail=str(e))
|
| 168 |
|
| 169 |
+
@app.get("/debug-cases")
|
| 170 |
+
async def debug_cases():
|
| 171 |
+
try:
|
| 172 |
+
conn = sqlite3.connect("cases.db")
|
| 173 |
+
cursor = conn.cursor()
|
| 174 |
+
cursor.execute("SELECT * FROM cases")
|
| 175 |
+
cases = cursor.fetchall()
|
| 176 |
+
conn.close()
|
| 177 |
+
return {"cases": cases}
|
| 178 |
+
except Exception as e:
|
| 179 |
+
logger.error(f"Error retrieving cases: {str(e)}")
|
| 180 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 181 |
+
|
| 182 |
if __name__ == "__main__":
|
| 183 |
import uvicorn
|
| 184 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|