Firebase Cloud Messaging 1. Versuch
This commit is contained in:
1 parent
f1fb926c2f
commit
4c8b11c4d5
25 files changed
+2111
-31
No files matched your search
@@ -0,0 +1,103 @@
|
||||
"""Periodischer Worker für FCM-Aufwachsignale.
|
||||
|
||||
Der Worker läuft in jedem WSGI-Prozess. Die eigentliche Kandidatenauswahl
|
||||
wird über ``claim_offline_mobile_wakes`` in SQLite transaktional reserviert,
|
||||
damit mehrere Worker nicht dasselbe Signal senden.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
|
||||
from cmbnet.db import get_db
|
||||
from cmbnet.services.fcm_service import fcm_service
|
||||
from cmbnet.ws import store
|
||||
|
||||
|
||||
class PushWakeWorker:
|
||||
def __init__(self, interval=30):
|
||||
self.interval = max(5.0, float(interval or 30))
|
||||
self.app = None
|
||||
self.running = False
|
||||
self._thread = None
|
||||
self._stop = threading.Event()
|
||||
|
||||
def start(self, app=None, interval=None):
|
||||
if app is not None:
|
||||
self.app = app
|
||||
if interval is not None:
|
||||
self.interval = max(5.0, float(interval))
|
||||
if self.running or self.app is None:
|
||||
return
|
||||
|
||||
self.running = True
|
||||
self._stop.clear()
|
||||
self._thread = threading.Thread(
|
||||
target=self._run,
|
||||
name='cmbnet-fcm-wake',
|
||||
daemon=True,
|
||||
)
|
||||
self._thread.start()
|
||||
|
||||
def stop(self):
|
||||
self.running = False
|
||||
self._stop.set()
|
||||
if self._thread is not None:
|
||||
self._thread.join(timeout=2.0)
|
||||
self._thread = None
|
||||
|
||||
def _run(self):
|
||||
with self.app.app_context():
|
||||
while not self._stop.is_set() and self.running:
|
||||
try:
|
||||
self._run_once()
|
||||
except Exception as exc:
|
||||
try:
|
||||
self.app.logger.warning(
|
||||
'FCM-Offline-Worker fehlgeschlagen: %s', exc
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
self._stop.wait(self.interval)
|
||||
|
||||
def _run_once(self):
|
||||
config = self.app.config
|
||||
if not fcm_service.server_ready():
|
||||
return
|
||||
|
||||
try:
|
||||
offline_seconds = max(
|
||||
0.0,
|
||||
float(config.get('FCM_OFFLINE_WAKE_MINUTES', 5)) * 60
|
||||
)
|
||||
except (TypeError, ValueError):
|
||||
offline_seconds = 5 * 60
|
||||
|
||||
current = store.now()
|
||||
cutoff = current - offline_seconds
|
||||
db = get_db()
|
||||
tenant_rows = db.execute('SELECT id FROM tenants').fetchall()
|
||||
for tenant_row in tenant_rows:
|
||||
tenant_id = tenant_row['id']
|
||||
candidates = store.claim_offline_mobile_wakes(
|
||||
tenant_id,
|
||||
disconnected_before=cutoff,
|
||||
re_wake_before=cutoff,
|
||||
auto_position_only=True,
|
||||
reason='offline',
|
||||
limit=500,
|
||||
)
|
||||
if not candidates:
|
||||
continue
|
||||
|
||||
tokens = [row['token'] for row in candidates]
|
||||
result = fcm_service.send_wake(tokens, reason='offline')
|
||||
for token in result.get('invalid_tokens') or []:
|
||||
store.mark_push_token_invalid(token)
|
||||
if result.get('failed'):
|
||||
self.app.logger.warning(
|
||||
'FCM-Offline-Wake: %s von %s Signalen fehlgeschlagen '
|
||||
'(tenant=%s)', result.get('failed'), len(tokens), tenant_id
|
||||
)
|
||||
|
||||
|
||||
push_wake_worker = PushWakeWorker()
|
||||
Reference in new issue
Block a user