3. Versuch bug fix FCM mit mehr Info im UI
This commit is contained in:
1 parent
df97539f27
commit
84aa7f9631
10 files changed
+178
-36
No files matched your search
@@ -58,7 +58,7 @@ def register_push_token(**kwargs):
|
||||
return jsonify({'ok': False, 'error': 'Client-ID fehlt.'}), 400
|
||||
client_id = broker.normalize_client_id(requested_client_id, account_id)
|
||||
|
||||
store.upsert_push_token(
|
||||
stored = store.upsert_push_token(
|
||||
g.tenant_id,
|
||||
client_id,
|
||||
account_id,
|
||||
@@ -67,6 +67,11 @@ def register_push_token(**kwargs):
|
||||
platform=_platform_name(data.get('platform')),
|
||||
auto_position=data.get('auto_position') is True,
|
||||
)
|
||||
if not stored:
|
||||
return jsonify({
|
||||
'ok': False,
|
||||
'error': 'Client-ID gehört bereits zu einem anderen Account.'
|
||||
}), 409
|
||||
current_app.logger.info(
|
||||
'FCM-Token registriert: tenant=%s client=%s type=%s',
|
||||
g.tenant_id, client_id, client_type
|
||||
@@ -118,13 +123,36 @@ def wake_mobile_clients(**kwargs):
|
||||
|
||||
current = store.now()
|
||||
stats = store.mobile_push_stats(g.tenant_id)
|
||||
cooldown = max(0, int(current_app.config.get(
|
||||
'FCM_MANUAL_WAKE_COOLDOWN_SECONDS', 15
|
||||
)))
|
||||
|
||||
# Die öffentliche Web-Konfiguration kann vollständig sein, während der
|
||||
# Server selbst nicht senden kann (fehlendes firebase-admin, fehlender
|
||||
# oder ungültiger Service-Account). Ohne diese Prüfung würde der Button
|
||||
# fälschlich „keine Clients gefunden“ melden, obwohl Tokens vorhanden
|
||||
# sind.
|
||||
server = fcm_service.server_status()
|
||||
if not server['ready']:
|
||||
message = server['error'] or 'FCM-Server ist nicht sendebereit.'
|
||||
current_app.logger.warning(
|
||||
'FCM-Wake nicht möglich: tenant=%s tokens=%s grund=%s',
|
||||
g.tenant_id, stats['valid_tokens'], message
|
||||
)
|
||||
return jsonify({
|
||||
'ok': False,
|
||||
'configured': False,
|
||||
'error': message,
|
||||
'valid_tokens': stats['valid_tokens'],
|
||||
'connected': stats['connected'],
|
||||
}), 503
|
||||
|
||||
candidates = store.offline_mobile_wake_candidates(
|
||||
g.tenant_id,
|
||||
disconnected_before=current,
|
||||
re_wake_before=current - cooldown if cooldown else None,
|
||||
# Ein ausdrücklicher Klick soll immer senden. Die automatische
|
||||
# Drosselung über ``last_wake_at`` gilt nur für Nachrichten-Wakes.
|
||||
# Andernfalls könnte ein kurz zuvor automatisch versuchtes Signal den
|
||||
# Button für 15 Sekunden blockieren – der Nutzer sähe dann trotz
|
||||
# registrierter Tokens „kein passender Client“.
|
||||
re_wake_before=None,
|
||||
group_id=group_id,
|
||||
auto_position_only=False,
|
||||
# Der Button soll alle registrierten mobilen Clients erreichen – auch
|
||||
|
||||
@@ -139,6 +139,26 @@ def ensure_ws_schema(db):
|
||||
'CREATE INDEX IF NOT EXISTS idx_ws_clients_type '
|
||||
'ON ws_clients(tenant_id, client_type, disconnected_at)'
|
||||
)
|
||||
|
||||
# Tokens, die vor Einführung der Platzhalter-Clients registriert wurden,
|
||||
# erhalten nachträglich einen ws_clients-Eintrag. Ohne diesen Eintrag
|
||||
# wären sie zwar als „registriert“ gezählt, würden aber nie als
|
||||
# Wake-Kandidat gefunden werden.
|
||||
db.execute(
|
||||
'''INSERT INTO ws_clients
|
||||
(tenant_id, client_id, account_id, client_type, worker_id,
|
||||
connected_at, disconnected_at, authenticated, last_seq)
|
||||
SELECT p.tenant_id, p.client_id, p.account_id, p.client_type,
|
||||
NULL, p.updated_at, p.updated_at, 0, 0
|
||||
FROM ws_push_tokens p
|
||||
WHERE p.invalid = 0
|
||||
AND p.client_type IN ('group_view', 'dispatcher_mobile')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM ws_clients c
|
||||
WHERE c.tenant_id = p.tenant_id
|
||||
AND c.client_id = p.client_id
|
||||
)'''
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
|
||||
@@ -118,6 +118,20 @@ class FcmService:
|
||||
_config, error = self._server_config()
|
||||
return error is None
|
||||
|
||||
def server_status(self):
|
||||
"""Prüft die tatsächliche Sendefähigkeit inklusive firebase-admin.
|
||||
|
||||
``server_ready`` prüft nur die Konfiguration. Für eine verständliche
|
||||
Fehlermeldung im Wake-Endpunkt muss auch die Initialisierung von
|
||||
``firebase-admin`` berücksichtigt werden (fehlendes Paket, ungültiger
|
||||
Service-Account usw.).
|
||||
"""
|
||||
_config, error = self._server_config()
|
||||
if error:
|
||||
return {'ready': False, 'error': error}
|
||||
app, error = self._ensure_app()
|
||||
return {'ready': app is not None, 'error': error}
|
||||
|
||||
def _ensure_app(self):
|
||||
"""Initialisiert firebase-admin genau einmal pro Workerprozess."""
|
||||
config, error = self._server_config()
|
||||
|
||||
+52
-13
@@ -233,9 +233,35 @@ def upsert_push_token(tenant_id, client_id, account_id, token, client_type,
|
||||
verschoben, statt ein zweites Token anzulegen. Ältere Tokens derselben
|
||||
Client-ID werden entfernt, damit nach einer Token-Rotation keine doppelten
|
||||
Wake-Signale entstehen.
|
||||
|
||||
Fehlt zur Client-ID noch ein ``ws_clients``-Eintrag (z. B. weil die
|
||||
Registrierung erfolgte, bevor die WebSocket-Verbindung stand), wird ein
|
||||
Platzhalter angelegt. Nur so erscheint das Token später als Wake-Kandidat
|
||||
und die Drosselung über ``last_wake_at`` greift. Der Platzhalter gilt als
|
||||
getrennt; eine echte WebSocket-Verbindung setzt den Status zurück.
|
||||
"""
|
||||
db = get_db()
|
||||
current = now()
|
||||
|
||||
client_row = get_client(tenant_id, client_id)
|
||||
if client_row is None:
|
||||
db.execute(
|
||||
'''INSERT OR IGNORE INTO ws_clients
|
||||
(tenant_id, client_id, account_id, client_type, worker_id,
|
||||
connected_at, disconnected_at, authenticated, last_seq)
|
||||
VALUES (?, ?, ?, ?, NULL, ?, ?, 0, 0)''',
|
||||
(tenant_id, client_id, account_id, client_type, current, current)
|
||||
)
|
||||
elif client_row['account_id'] != account_id:
|
||||
# Ein Token darf keine fremde Client-ID übernehmen.
|
||||
return False
|
||||
elif not client_row['client_type'] and client_type:
|
||||
db.execute(
|
||||
'UPDATE ws_clients SET client_type = ? '
|
||||
'WHERE tenant_id = ? AND client_id = ? AND client_type IS NULL',
|
||||
(client_type, tenant_id, client_id)
|
||||
)
|
||||
|
||||
db.execute(
|
||||
'DELETE FROM ws_push_tokens '
|
||||
'WHERE tenant_id = ? AND client_id = ? AND token <> ?',
|
||||
@@ -259,6 +285,7 @@ def upsert_push_token(tenant_id, client_id, account_id, token, client_type,
|
||||
platform, 1 if auto_position else 0, current, current)
|
||||
)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
def delete_push_token(tenant_id, token=None, client_id=None, account_id=None):
|
||||
@@ -363,22 +390,31 @@ def _offline_mobile_wake_query(tenant_id, disconnected_before,
|
||||
re_wake_before=None, group_id=None,
|
||||
auto_position_only=False, client_id=None,
|
||||
include_connected=False, limit=500):
|
||||
# Maßgeblich ist das registrierte FCM-Token, nicht ein eventuell fehlender
|
||||
# oder veralteter ws_clients-Eintrag. Früher wurden Kandidaten über einen
|
||||
# INNER JOIN auf ws_clients ermittelt. Existierte dort kein Eintrag (z. B.
|
||||
# weil die Registrierung ohne offene WebSocket-Verbindung erfolgte), wurde
|
||||
# trotz gültigem Token kein Empfänger gefunden. Der LEFT JOIN erlaubt
|
||||
# solche Tokens ausdrücklich.
|
||||
sql = [
|
||||
'''SELECT c.tenant_id, c.client_id, c.account_id, c.client_type,
|
||||
'''SELECT p.tenant_id, p.client_id, p.account_id, p.client_type,
|
||||
c.disconnected_at, c.last_wake_at, c.last_wake_reason,
|
||||
p.token, p.platform, p.auto_position
|
||||
FROM ws_clients c
|
||||
JOIN ws_push_tokens p
|
||||
ON p.id = (
|
||||
FROM ws_push_tokens p
|
||||
LEFT JOIN ws_clients c
|
||||
ON c.tenant_id = p.tenant_id
|
||||
AND c.client_id = p.client_id
|
||||
WHERE p.tenant_id = ?
|
||||
AND p.invalid = 0
|
||||
AND p.client_type IN ('group_view', 'dispatcher_mobile')
|
||||
AND p.id = (
|
||||
SELECT p2.id FROM ws_push_tokens p2
|
||||
WHERE p2.tenant_id = c.tenant_id
|
||||
AND p2.client_id = c.client_id
|
||||
WHERE p2.tenant_id = p.tenant_id
|
||||
AND p2.client_id = p.client_id
|
||||
AND p2.invalid = 0
|
||||
ORDER BY p2.updated_at DESC, p2.id DESC
|
||||
LIMIT 1
|
||||
)
|
||||
WHERE c.tenant_id = ?
|
||||
AND p.client_type IN ('group_view', 'dispatcher_mobile')'''
|
||||
)'''
|
||||
]
|
||||
params = [tenant_id]
|
||||
|
||||
@@ -394,7 +430,7 @@ def _offline_mobile_wake_query(tenant_id, disconnected_before,
|
||||
params.append(disconnected_before)
|
||||
|
||||
if client_id is not None:
|
||||
sql.append('AND c.client_id = ?')
|
||||
sql.append('AND p.client_id = ?')
|
||||
params.append(client_id)
|
||||
|
||||
if re_wake_before is not None:
|
||||
@@ -404,14 +440,17 @@ def _offline_mobile_wake_query(tenant_id, disconnected_before,
|
||||
params.append(re_wake_before)
|
||||
|
||||
if auto_position_only:
|
||||
sql.append("AND (c.client_type = 'dispatcher_mobile' OR p.auto_position = 1)")
|
||||
sql.append(
|
||||
"AND (p.client_type = 'dispatcher_mobile' "
|
||||
'OR p.auto_position = 1)'
|
||||
)
|
||||
|
||||
if group_id is not None:
|
||||
sql.append(
|
||||
'''AND EXISTS (
|
||||
SELECT 1 FROM group_accounts ga
|
||||
WHERE ga.tenant_id = c.tenant_id
|
||||
AND ga.account_id = c.account_id
|
||||
WHERE ga.tenant_id = p.tenant_id
|
||||
AND ga.account_id = p.account_id
|
||||
AND ga.group_id = ?
|
||||
)'''
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user