04/17 Update: fixed email ingestion ticket error 2
This commit is contained in:
@@ -89,14 +89,36 @@ def create_notification(user_id, notif_type, title, message, ticket_id=None, lin
|
|||||||
'link' : link,
|
'link' : link,
|
||||||
'created_at': notif.created_at.isoformat(),
|
'created_at': notif.created_at.isoformat(),
|
||||||
}
|
}
|
||||||
|
# Emit strategy depends on whether we have a live request context:
|
||||||
|
#
|
||||||
|
# - Inside a web request → use start_background_task() so the emit
|
||||||
|
# runs in eventlet's green-thread pool and does not block the WSGI
|
||||||
|
# response or race with a polling->WebSocket upgrade handshake.
|
||||||
|
#
|
||||||
|
# - Outside a request context (e.g. APScheduler background job) ->
|
||||||
|
# call socketio.emit() directly. start_background_task() causes
|
||||||
|
# Flask-SocketIO to invoke the on_connect handler internally, which
|
||||||
|
# references current_user -- but there is no session to resolve it
|
||||||
|
# from, so Flask-Login returns None and raises AttributeError.
|
||||||
|
# A direct emit bypasses that handler entirely and is safe from a
|
||||||
|
# background thread that already has an app context active.
|
||||||
|
from flask import has_request_context
|
||||||
|
if has_request_context():
|
||||||
def _emit():
|
def _emit():
|
||||||
socketio.emit(
|
socketio.emit(
|
||||||
'new_notification',
|
'new_notification',
|
||||||
payload,
|
payload,
|
||||||
to=f'user_{user_id}', # 'to' is the modern alias for 'room'
|
to=f'user_{user_id}',
|
||||||
namespace='/', # explicit default namespace — avoids
|
namespace='/',
|
||||||
) # ambiguity under reverse-proxy setups
|
)
|
||||||
socketio.start_background_task(_emit)
|
socketio.start_background_task(_emit)
|
||||||
|
else:
|
||||||
|
socketio.emit(
|
||||||
|
'new_notification',
|
||||||
|
payload,
|
||||||
|
to=f'user_{user_id}',
|
||||||
|
namespace='/',
|
||||||
|
)
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
|
|||||||
Reference in New Issue
Block a user