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()
|
ha_mqtt = HomeAssistantMQTT()
|
||||||
|
|
||||||
# Constants
|
# 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
|
# Set up MQTT event logging
|
||||||
async def log_mqtt_event(action: str, success: bool = True):
|
async def log_mqtt_event(action: str, success: bool = True):
|
||||||
|
|
@ -467,17 +474,6 @@ async def update_settings(settings: Settings):
|
||||||
# Serve static files
|
# Serve static files
|
||||||
app.mount("/", StaticFiles(directory="../public", html=True), name="static")
|
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
|
# GPIO Setup
|
||||||
def setup_gpio():
|
def setup_gpio():
|
||||||
"""Initialize GPIO pins based on settings"""
|
"""Initialize GPIO pins based on settings"""
|
||||||
|
|
@ -665,10 +661,8 @@ async def shutdown_event():
|
||||||
|
|
||||||
# 4. Close database connection
|
# 4. Close database connection
|
||||||
logger.info("Closing database connection...")
|
logger.info("Closing database connection...")
|
||||||
global db_pool
|
# No need to explicitly close as we're using new connections for each request
|
||||||
if db_pool:
|
logger.info("Database connections will be closed automatically")
|
||||||
await db_pool.close()
|
|
||||||
logger.info("Database connection closed")
|
|
||||||
|
|
||||||
logger.info("Shutdown completed successfully")
|
logger.info("Shutdown completed successfully")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@ function App() {
|
||||||
const [hasMoreEvents, setHasMoreEvents] = useState(false);
|
const [hasMoreEvents, setHasMoreEvents] = useState(false);
|
||||||
const [totalEvents, setTotalEvents] = useState(0);
|
const [totalEvents, setTotalEvents] = useState(0);
|
||||||
const [settings, setSettings] = useState<Settings>({
|
const [settings, setSettings] = useState<Settings>({
|
||||||
maxOpenTimeSeconds: '300',
|
maxOpenTimeSeconds: 300,
|
||||||
triggerDuration: '500',
|
triggerDuration: 500,
|
||||||
mqtt: {
|
mqtt: {
|
||||||
broker: 'localhost',
|
broker: 'localhost',
|
||||||
port: '1883',
|
port: '1883',
|
||||||
|
|
@ -84,7 +84,7 @@ function App() {
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
const newEvents = await api.getEvents();
|
const newEvents = await api.getEvents();
|
||||||
setEvents(newEvents.events);
|
setEvents(newEvents.events);
|
||||||
setGateStatus(prev => ({ ...prev, isOpen: result.currentStatus }));
|
setGateStatus(prev => ({ ...prev, isOpen: result.isOpen }));
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError('Failed to trigger gate');
|
setError('Failed to trigger gate');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue