Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor: Move processor_v2 notification logic to notification module

LearningCircuit3ac33a0b18e7
diff --git a/src/local_deep_research/notifications/__init__.py b/src/local_deep_research/notifications/__init__.py
index d0ef598e7b..595e4dfee9 100644
--- a/src/local_deep_research/notifications/__init__.py
+++ b/src/local_deep_research/notifications/__init__.py
@@ -23,6 +23,8 @@ from .queue_helpers import (
send_queue_notification,
send_queue_failed_notification,
send_queue_failed_notification_from_session,
+ send_research_completed_notification_from_session,
+ send_research_failed_notification_from_session,
)
__all__ = [
@@ -40,4 +42,6 @@ __all__ = [
"send_queue_notification",
"send_queue_failed_notification",
"send_queue_failed_notification_from_session",
+ "send_research_completed_notification_from_session",
+ "send_research_failed_notification_from_session",
]
diff --git a/src/local_deep_research/notifications/queue_helpers.py b/src/local_deep_research/notifications/queue_helpers.py
index d649d19686..a8dac13136 100644
--- a/src/local_deep_research/notifications/queue_helpers.py
+++ b/src/local_deep_research/notifications/queue_helpers.py
@@ -159,3 +159,254 @@ def send_queue_failed_notification_from_session(
logger.exception(
f"Failed to send failure notification for {research_id}"
)
+
+
+def send_research_completed_notification_from_session(
+ username: str,
+ research_id: str,
+ db_session,
+) -> None:
+ """
+ Send research completed notification with summary and URL.
+
+ This is a convenience wrapper for the queue processor that handles
+ all notification logic for completed research, including:
+ - Research database lookup
+ - Report content retrieval
+ - URL building
+ - Context building with summary
+ - All logging and error handling
+
+ Args:
+ username: User who owns the research
+ research_id: UUID of the research
+ db_session: Database session to fetch research and settings from
+ """
+ try:
+ logger.info(
+ f"Starting completed notification process for research {research_id}, "
+ f"user {username}"
+ )
+
+ # Import here to avoid circular dependencies
+ from ...database.models import ResearchHistory
+ from ...settings import SettingsManager
+ from .manager import NotificationManager
+ from .url_builder import build_notification_url
+
+ # Get research details for notification
+ research = (
+ db_session.query(ResearchHistory).filter_by(id=research_id).first()
+ )
+
+ # Get settings snapshot for thread-safe notification sending
+ settings_manager = SettingsManager(db_session)
+ settings_snapshot = settings_manager.get_settings_snapshot()
+
+ if research:
+ logger.info(
+ f"Found research record, creating NotificationManager "
+ f"for user {username}"
+ )
+
+ # Create notification manager with settings snapshot
+ notification_manager = NotificationManager(
+ settings_snapshot=settings_snapshot, user_id=username
+ )
+
+ # Build full URL for notification
+ full_url = build_notification_url(
+ f"/research/{research_id}",
+ settings_manager=settings_manager,
+ )
+
+ # Build notification context with required fields
+ context = {
+ "query": research.query or "Unknown query",
+ "research_id": research_id,
+ "summary": "No summary available",
+ "url": full_url,
+ }
+
+ # Get report content for notification
+ from ...storage import get_report_storage
+
+ storage = get_report_storage(session=db_session)
+ report_content = storage.get_report(research_id)
+
+ if report_content:
+ # Truncate summary if too long
+ context["summary"] = (
+ report_content[:200] + "..."
+ if len(report_content) > 200
+ else report_content
+ )
+
+ logger.info(
+ f"Sending RESEARCH_COMPLETED notification for research "
+ f"{research_id} to user {username}"
+ )
+ logger.debug(f"Notification context: {context}")
+
+ # Send notification using the manager
+ result = notification_manager.send_notification(
+ event_type=EventType.RESEARCH_COMPLETED,
+ context=context,
+ )
+
+ if result:
+ logger.info(
+ f"Successfully sent completion notification for research {research_id}"
+ )
+ else:
+ logger.debug(
+ f"Completion notification not sent for {research_id} (disabled or rate limited)"
+ )
+
+ else:
+ logger.warning(
+ f"Could not find research {research_id} in database, "
+ f"sending notification with minimal details"
+ )
+
+ # Create notification manager with settings snapshot
+ notification_manager = NotificationManager(
+ settings_snapshot=settings_snapshot, user_id=username
+ )
+
+ # Build minimal context
+ context = {
+ "query": f"Research {research_id}",
+ "research_id": research_id,
+ "summary": "Research completed but details unavailable",
+ "url": f"/research/{research_id}",
+ }
+
+ notification_manager.send_notification(
+ event_type=EventType.RESEARCH_COMPLETED,
+ context=context,
+ )
+ logger.info(
+ f"Sent completion notification for research {research_id} (minimal details)"
+ )
+
+ except Exception:
+ logger.exception(
+ f"Failed to send completion notification for {research_id}"
+ )
+
+
+def send_research_failed_notification_from_session(
+ username: str,
+ research_id: str,
+ error_message: str,
+ db_session,
+) -> None:
+ """
+ Send research failed notification (research-specific version).
+
+ This is a convenience wrapper for the queue processor that handles
+ all notification logic for failed research, including:
+ - Research database lookup to get query
+ - Context building with sanitized error message
+ - All logging and error handling
+
+ Args:
+ username: User who owns the research
+ research_id: UUID of the research
+ error_message: Error message (will be sanitized for security)
+ db_session: Database session to fetch research and settings from
+ """
+ try:
+ logger.info(
+ f"Starting failed notification process for research {research_id}, "
+ f"user {username}"
+ )
+
+ # Import here to avoid circular dependencies
+ from ...database.models import ResearchHistory
+ from ...settings import SettingsManager
+ from .manager import NotificationManager
+
+ # Get research details for notification
+ research = (
+ db_session.query(ResearchHistory).filter_by(id=research_id).first()
+ )
+
+ # Get settings snapshot for thread-safe notification sending
+ settings_manager = SettingsManager(db_session)
+ settings_snapshot = settings_manager.get_settings_snapshot()
+
+ # Sanitize error message for notification to avoid exposing
+ # sensitive information (as noted by github-advanced-security)
+ safe_error = "Research failed. Check logs for details."
+
+ if research:
+ logger.info(
+ f"Found research record, creating NotificationManager "
+ f"for user {username}"
+ )
+
+ # Create notification manager with settings snapshot
+ notification_manager = NotificationManager(
+ settings_snapshot=settings_snapshot, user_id=username
+ )
+
+ # Build notification context
+ context = {
+ "query": research.query or "Unknown query",
+ "research_id": research_id,
+ "error": safe_error,
+ }
+
+ logger.info(
+ f"Sending RESEARCH_FAILED notification for research "
+ f"{research_id} to user {username}"
+ )
+ logger.debug(f"Notification context: {context}")
+
+ # Send notification using the manager
+ result = notification_manager.send_notification(
+ event_type=EventType.RESEARCH_FAILED,
+ context=context,
+ )
+
+ if result:
+ logger.info(
+ f"Successfully sent failure notification for research {research_id}"
+ )
+ else:
+ logger.debug(
+ f"Failure notification not sent for {research_id} (disabled or rate limited)"
+ )
+
+ else:
+ logger.warning(
+ f"Could not find research {research_id} in database, "
+ f"sending notification with minimal details"
+ )
+
+ # Create notification manager with settings snapshot
+ notification_manager = NotificationManager(
+ settings_snapshot=settings_snapshot, user_id=username
+ )
+
+ # Build minimal context
+ context = {
+ "query": f"Research {research_id}",
+ "research_id": research_id,
+ "error": safe_error,
+ }
+
+ notification_manager.send_notification(
+ event_type=EventType.RESEARCH_FAILED,
+ context=context,
+ )
+ logger.info(
+ f"Sent failure notification for research {research_id} (minimal details)"
+ )
+
+ except Exception:
+ logger.exception(
+ f"Failed to send failure notification for {research_id}"
+ )
diff --git a/src/local_deep_research/web/queue/processor_v2.py b/src/local_deep_research/web/queue/processor_v2.py
index 5da5c4da56..8eb689321e 100644
--- a/src/local_deep_research/web/queue/processor_v2.py
+++ b/src/local_deep_research/web/queue/processor_v2.py
@@ -18,6 +18,10 @@ from ...database.models import (
from ...database.queue_service import UserQueueService
from ...database.session_context import get_user_db_session
from ...database.session_passwords import session_password_store
+from ...notifications.queue_helpers import (
+ send_research_completed_notification_from_session,
+ send_research_failed_notification_from_session,
+)
from ..routes.globals import active_research, termination_flags
from ..services.research_service import (
run_research_process,
@@ -300,12 +304,11 @@ class QueueProcessorV2:
f"Research {research_id} completed for user {username}"
)
- # Send notification if enabled
- self._send_research_notification(
- session=session,
+ # Send notification using helper from notification module
+ send_research_completed_notification_from_session(
username=username,
research_id=research_id,
- event_type="RESEARCH_COMPLETED",
+ db_session=session,
)
except Exception:
@@ -346,170 +349,17 @@ class QueueProcessorV2:
f"{error_message}"
)
- # Send notification if enabled
- self._send_research_notification(
- session=session,
+ # Send notification using helper from notification module
+ send_research_failed_notification_from_session(
username=username,
research_id=research_id,
- event_type="RESEARCH_FAILED",
- error_message=error_message,
+ error_message=error_message or "Unknown error",
+ db_session=session,
)
except Exception:
logger.exception(f"Failed to update failure status for {username}")
- def _send_research_notification(
- self,
- session,
- username: str,
- research_id: str,
- event_type: str,
- error_message: str = None,
- ):
- """
- Helper method to send research notification with settings snapshot.
-
- Args:
- session: Database session
- username: Username
- research_id: Research ID
- event_type: Event type ("RESEARCH_COMPLETED" or "RESEARCH_FAILED")
- error_message: Optional error message for failed research
- """
- try:
- logger.info(
- f"Starting notification process for research {research_id}, "
- f"user {username}"
- )
- from ...notifications import EventType
-
- # Get research details for notification
- # No retry logic needed - research should already exist since we're
- # notifying about its completion/failure. If it's not found, something
- # is seriously wrong and retrying won't help.
- research = (
- session.query(ResearchHistory).filter_by(id=research_id).first()
- )
-
- # Get settings snapshot for thread-safe notification sending
- from ...settings import SettingsManager
-
- settings_manager = SettingsManager(session)
- settings_snapshot = settings_manager.get_settings_snapshot()
-
- if research:
- logger.info(
- f"Found research record, creating NotificationManager "
- f"for user {username}"
- )
- # Create notification manager with settings snapshot
- # (no session to avoid thread-safety issues)
- from ...notifications.manager import NotificationManager
- from ...notifications.url_builder import build_notification_url
-
- notification_manager = NotificationManager(
- settings_snapshot=settings_snapshot, user_id=username
- )
-
- # Build context based on event type
- if event_type == "RESEARCH_COMPLETED":
- # Build full URL for notification
- full_url = build_notification_url(
- f"/research/{research_id}",
- settings_manager=settings_manager,
- )
-
- # Build notification context with required fields
- context = {
- "query": research.query or "Unknown query",
- "research_id": research_id,
- "summary": "No summary available",
- "url": full_url,
- }
-
- # Get report content for notification
- from ...storage import get_report_storage
-
- storage = get_report_storage(session=session)
- report_content = storage.get_report(research_id)
-
- if report_content:
- # Truncate summary if too long
- context["summary"] = (
- report_content[:200] + "..."
- if len(report_content) > 200
- else report_content
- )
- else: # RESEARCH_FAILED
- # Sanitize error message for notification to avoid exposing
- # sensitive information (as noted by github-advanced-security)
- safe_error = "Research failed. Check logs for details."
- context = {
- "query": research.query or "Unknown query",
- "research_id": research_id,
- "error": safe_error,
- }
-
- logger.info(
- f"Sending {event_type} notification for research "
- f"{research_id} to user {username}"
- )
- logger.debug(f"Notification context: {context}")
-
- # Send notification using the manager
- result = notification_manager.send_notification(
- event_type=EventType[event_type],
- context=context,
- )
- logger.info(f"Notification sent, result: {result}")
- logger.info(
- f"Successfully sent {event_type.lower()} notification "
- f"for research {research_id}"
- )
- else:
- logger.warning(
- f"Could not find research {research_id} in database, "
- f"sending notification with minimal details"
- )
- # Create notification manager with settings snapshot
- from ...notifications.manager import NotificationManager
-
- notification_manager = NotificationManager(
- settings_snapshot=settings_snapshot, user_id=username
- )
-
- # Build minimal context
- if event_type == "RESEARCH_COMPLETED":
- context = {
- "query": f"Research {research_id}",
- "research_id": research_id,
- "summary": "Research completed but details unavailable",
- "url": f"/research/{research_id}",
- }
- else: # RESEARCH_FAILED
- # Sanitize error for notification
- safe_error = "Research failed. Check logs for details."
- context = {
- "query": f"Research {research_id}",
- "research_id": research_id,
- "error": safe_error,
- }
-
- notification_manager.send_notification(
- event_type=EventType[event_type],
- context=context,
- )
- logger.info(
- f"Sent {event_type.lower()} notification for research "
- f"{research_id} (minimal details)"
- )
-
- except Exception:
- logger.exception(
- f"Failed to send {event_type.lower()} notification for "
- f"{research_id}"
- )
-
def _process_queue_loop(self):
"""Main loop that processes the queue."""
while self.running: