Fix: Fixed database is locked error when enabling WAL in SQLite

This commit is contained in:
2026-04-21 00:29:39 +02:00
parent f259e0a774
commit ba241f6a45
2 changed files with 10 additions and 3 deletions
+8 -3
View File
@@ -13,7 +13,7 @@ from logging.handlers import RotatingFileHandler
# --- CONFIGURAZIONE LOGGING ---
logging.basicConfig(
handlers=[
RotatingFileHandler('/opt/web-control/fleet_console.log', maxBytes=2000000, backupCount=3),
RotatingFileHandler('/opt/web-control/fleet_console.log', maxBytes=10000000, backupCount=3),
logging.StreamHandler()
],
level=logging.INFO,
@@ -21,6 +21,8 @@ logging.basicConfig(
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger("FleetHub")
# Silenzia lo spam delle richieste HTTP (GET /api/states 200 OK)
logging.getLogger('werkzeug').setLevel(logging.ERROR)
# --- PERCORSI ---
DB_PATH = '/opt/web-control/monitor.db'
@@ -33,6 +35,9 @@ CLIENTS_PATH = '/opt/web-control/clients.json'
def init_db():
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('PRAGMA journal_mode=WAL;') # <-- MAGIA: Abilita letture/scritture simultanee!
c.execute('''CREATE TABLE IF NOT EXISTS radio_logs
(id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp DATETIME, client_id TEXT,
source_id TEXT, target TEXT, slot INTEGER, duration REAL, ber REAL, loss REAL)''')
@@ -158,7 +163,7 @@ def on_message(client, userdata, msg):
device_configs[cid_conf] = {}
try:
device_configs[cid_conf][svc_name] = json.loads(payload)
logger.info(f"Configurazione salvata per {cid_conf} -> {svc_name}")
logger.debug(f"Configurazione salvata per {cid_conf} -> {svc_name}")
except Exception as e:
logger.error(f"Errore parsing config JSON: {e}")
@@ -313,7 +318,7 @@ def get_clients():
@app.route('/api/logs')
def get_logs():
conn = sqlite3.connect(DB_PATH)
conn = sqlite3.connect(DB_PATH, timeout=10)
c = conn.cursor()
c.execute("SELECT timestamp, client_id, protocol, source_id, target, slot, duration, ber FROM radio_logs ORDER BY id DESC LIMIT 60")
logs = c.fetchall()