Broker unterstützt jetzt mehrere WSGI Worker
This commit is contained in:
1 parent
1e2a58e563
commit
f5b0c9509c
13 files changed
+1252
-327
No files matched your search
@@ -11,8 +11,16 @@ def get_db():
|
||||
db_path = current_app.config['DATABASE_PATH']
|
||||
# Stelle sicher, dass das instance Verzeichnis existiert
|
||||
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
g.db = sqlite3.connect(db_path)
|
||||
g.db = sqlite3.connect(db_path, timeout=30)
|
||||
g.db.row_factory = sqlite3.Row
|
||||
# Mehrere WSGI-Worker schreiben gleichzeitig in dieselbe SQLite-Datei.
|
||||
# busy_timeout zuerst setzen, damit auch das Umschalten auf WAL bei
|
||||
# parallelen Verbindungen warten kann statt sofort zu scheitern.
|
||||
# WAL erlaubt parallele Leser und reduziert die Blockierzeit bei
|
||||
# Schreibern.
|
||||
g.db.execute('PRAGMA busy_timeout = 30000')
|
||||
g.db.execute('PRAGMA journal_mode = WAL')
|
||||
g.db.execute('PRAGMA synchronous = NORMAL')
|
||||
return g.db
|
||||
|
||||
|
||||
@@ -23,6 +31,65 @@ def close_db(e=None):
|
||||
db.close()
|
||||
|
||||
|
||||
def ensure_ws_schema(db):
|
||||
"""Legt die Tabellen für den prozessübergreifenden Broker-Zustand an."""
|
||||
db.executescript('''
|
||||
CREATE TABLE IF NOT EXISTS ws_clients (
|
||||
tenant_id INTEGER NOT NULL,
|
||||
client_id TEXT NOT NULL,
|
||||
account_id INTEGER NOT NULL,
|
||||
last_seq INTEGER NOT NULL DEFAULT 0,
|
||||
worker_id TEXT,
|
||||
authenticated INTEGER NOT NULL DEFAULT 0,
|
||||
connected_at REAL,
|
||||
disconnected_at REAL,
|
||||
queue_started_at REAL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (tenant_id, client_id),
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
||||
FOREIGN KEY (account_id) REFERENCES accounts(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ws_client_queue (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
tenant_id INTEGER NOT NULL,
|
||||
client_id TEXT NOT NULL,
|
||||
code INTEGER NOT NULL,
|
||||
payload TEXT NOT NULL,
|
||||
code99_only INTEGER NOT NULL DEFAULT 0,
|
||||
created_at REAL NOT NULL,
|
||||
expires_at REAL,
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ws_group_presence (
|
||||
tenant_id INTEGER NOT NULL,
|
||||
group_id INTEGER NOT NULL,
|
||||
last_pong_at REAL,
|
||||
online_since REAL,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (tenant_id, group_id),
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
||||
FOREIGN KEY (group_id) REFERENCES groups(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS cache_state (
|
||||
key TEXT PRIMARY KEY,
|
||||
revision INTEGER NOT NULL DEFAULT 0,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_ws_clients_account
|
||||
ON ws_clients(tenant_id, account_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_ws_client_queue_client
|
||||
ON ws_client_queue(tenant_id, client_id, id);
|
||||
CREATE INDEX IF NOT EXISTS idx_ws_client_queue_expires
|
||||
ON ws_client_queue(expires_at);
|
||||
''')
|
||||
db.commit()
|
||||
|
||||
|
||||
def ensure_schema():
|
||||
"""Ergänzt bestehende Datenbanken um das Gruppen-Account-Zuordnungsmodell.
|
||||
|
||||
@@ -32,6 +99,7 @@ def ensure_schema():
|
||||
und die alte Spalte durch einen Tabellen-Neubau entfernt.
|
||||
"""
|
||||
db = get_db()
|
||||
ensure_ws_schema(db)
|
||||
groups_table = db.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'groups'"
|
||||
).fetchone()
|
||||
|
||||
Reference in new issue
Block a user