fix: database connection handling
- Create new connections for each request instead of reusing one - Remove global connection pool to fix thread reuse error - Simplify shutdown handler
This commit is contained in:
parent
0277e3d6ec
commit
a0533e1d78
|
|
@ -102,7 +102,14 @@ app = FastAPI()
|
|||
ha_mqtt = HomeAssistantMQTT()
|
||||
|
||||
# Constants
|
||||
DB_PATH = "gate.db" # Database file path
|
||||
DB_PATH = "gatekeeper.db" # Database file path
|
||||
|
||||
# Database connection handling
|
||||
async def get_db():
|
||||
"""Get a new database connection"""
|
||||
db = await aiosqlite.connect(DB_PATH)
|
||||
db.row_factory = aiosqlite.Row
|
||||
return db
|
||||
|
||||
# Set up MQTT event logging
|
||||
async def log_mqtt_event(action: str, success: bool = True):
|
||||
|
|
@ -467,17 +474,6 @@ async def update_settings(settings: Settings):
|
|||
# Serve static files
|
||||
app.mount("/", StaticFiles(directory="../public", html=True), name="static")
|
||||
|
||||
# Database connection pool
|
||||
db_pool = None
|
||||
|
||||
async def get_db():
|
||||
"""Get a database connection from the pool"""
|
||||
global db_pool
|
||||
if db_pool is None:
|
||||
db_pool = await aiosqlite.connect(DB_PATH)
|
||||
db_pool.row_factory = aiosqlite.Row
|
||||
return db_pool
|
||||
|
||||
# GPIO Setup
|
||||
def setup_gpio():
|
||||
"""Initialize GPIO pins based on settings"""
|
||||
|
|
@ -665,10 +661,8 @@ async def shutdown_event():
|
|||
|
||||
# 4. Close database connection
|
||||
logger.info("Closing database connection...")
|
||||
global db_pool
|
||||
if db_pool:
|
||||
await db_pool.close()
|
||||
logger.info("Database connection closed")
|
||||
# No need to explicitly close as we're using new connections for each request
|
||||
logger.info("Database connections will be closed automatically")
|
||||
|
||||
logger.info("Shutdown completed successfully")
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ function App() {
|
|||
const [hasMoreEvents, setHasMoreEvents] = useState(false);
|
||||
const [totalEvents, setTotalEvents] = useState(0);
|
||||
const [settings, setSettings] = useState<Settings>({
|
||||
maxOpenTimeSeconds: '300',
|
||||
triggerDuration: '500',
|
||||
maxOpenTimeSeconds: 300,
|
||||
triggerDuration: 500,
|
||||
mqtt: {
|
||||
broker: 'localhost',
|
||||
port: '1883',
|
||||
|
|
@ -84,7 +84,7 @@ function App() {
|
|||
if (result.success) {
|
||||
const newEvents = await api.getEvents();
|
||||
setEvents(newEvents.events);
|
||||
setGateStatus(prev => ({ ...prev, isOpen: result.currentStatus }));
|
||||
setGateStatus(prev => ({ ...prev, isOpen: result.isOpen }));
|
||||
}
|
||||
} catch (err) {
|
||||
setError('Failed to trigger gate');
|
||||
|
|
|
|||
Loading…
Reference in New Issue