fix: SQLite database locked — timeout=10 on get_db, direct connect in HA sync thread
Deploy via Portainer / deploy (push) Successful in 0s
Deploy via Portainer / deploy (push) Successful in 0s
This commit is contained in:
@@ -75,7 +75,7 @@ def require_login_global():
|
|||||||
return redirect(url_for('login'))
|
return redirect(url_for('login'))
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
conn = sqlite3.connect(DATABASE)
|
conn = sqlite3.connect(DATABASE, timeout=10)
|
||||||
conn.row_factory = sqlite3.Row
|
conn.row_factory = sqlite3.Row
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
@@ -1973,7 +1973,14 @@ def _ha_fetch_all_states(ha_url, ha_token):
|
|||||||
def _ha_sync_once():
|
def _ha_sync_once():
|
||||||
"""Poll HA et met a jour les print_slots actifs."""
|
"""Poll HA et met a jour les print_slots actifs."""
|
||||||
try:
|
try:
|
||||||
s = get_settings()
|
# Use direct connection with timeout — avoids locking conflicts with Flask request threads
|
||||||
|
_conn = sqlite3.connect(DATABASE, timeout=15)
|
||||||
|
_conn.row_factory = sqlite3.Row
|
||||||
|
try:
|
||||||
|
_s = _conn.execute("SELECT key, value FROM settings").fetchall()
|
||||||
|
finally:
|
||||||
|
_conn.close()
|
||||||
|
s = {r['key']: r['value'] for r in _s}
|
||||||
ha_url = str(s.get('ha_url', '')).rstrip('/')
|
ha_url = str(s.get('ha_url', '')).rstrip('/')
|
||||||
ha_token = str(s.get('ha_token', ''))
|
ha_token = str(s.get('ha_token', ''))
|
||||||
if not ha_url or not ha_token:
|
if not ha_url or not ha_token:
|
||||||
@@ -1981,7 +1988,8 @@ def _ha_sync_once():
|
|||||||
states = _ha_fetch_all_states(ha_url, ha_token)
|
states = _ha_fetch_all_states(ha_url, ha_token)
|
||||||
if not states:
|
if not states:
|
||||||
return
|
return
|
||||||
conn = get_db()
|
conn = sqlite3.connect(DATABASE, timeout=15)
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
printers = conn.execute(
|
printers = conn.execute(
|
||||||
"SELECT id, name, ha_entity_prefix FROM printers "
|
"SELECT id, name, ha_entity_prefix FROM printers "
|
||||||
"WHERE ha_entity_prefix IS NOT NULL AND ha_entity_prefix != ''"
|
"WHERE ha_entity_prefix IS NOT NULL AND ha_entity_prefix != ''"
|
||||||
@@ -2014,7 +2022,9 @@ def _ha_sync_once():
|
|||||||
if ha_ignored > 0:
|
if ha_ignored > 0:
|
||||||
conn.execute('UPDATE print_slots SET pieces_ignored=? WHERE id=?',
|
conn.execute('UPDATE print_slots SET pieces_ignored=? WHERE id=?',
|
||||||
(ha_ignored, slot['id']))
|
(ha_ignored, slot['id']))
|
||||||
|
try:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'[HA sync] {e}')
|
print(f'[HA sync] {e}')
|
||||||
|
|||||||
Reference in New Issue
Block a user