Coverage for src / local_deep_research / web / auth / queue_middleware.py: 86%
20 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-01-11 00:51 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2026-01-11 00:51 +0000
1"""
2Middleware to process pending queue operations when user has active session.
3"""
5from flask import g
6from loguru import logger
8from ...database.encrypted_db import db_manager
9from ...database.session_context import get_user_db_session
10from ..queue.processor_v2 import queue_processor
13def process_pending_queue_operations():
14 """
15 Process pending queue operations for the current user.
16 This runs in request context where we have access to the encrypted database.
17 """
18 # Check if user is authenticated and has a database session
19 if not hasattr(g, "current_user") or not g.current_user:
20 return
22 # g.current_user might be a string (username) or an object
23 username = (
24 g.current_user
25 if isinstance(g.current_user, str)
26 else g.current_user.username
27 )
29 # Check if user has an open database connection
30 if username not in db_manager.connections: 30 ↛ 31line 30 didn't jump to line 31 because the condition on line 30 was never true
31 return
33 try:
34 # Use the session context manager to properly handle the session
35 with get_user_db_session(username) as db_session:
36 if not db_session: 36 ↛ 37line 36 didn't jump to line 37 because the condition on line 36 was never true
37 return
39 # Process any pending operations for this user
40 started_count = queue_processor.process_pending_operations_for_user(
41 username, db_session
42 )
44 if started_count > 0:
45 logger.info(
46 f"Started {started_count} queued researches for {username}"
47 )
49 except Exception:
50 logger.exception(
51 f"Error processing pending queue operations for {username}"
52 )