perf: Fix all 7 code analysis issues
HIGH (Performance):
- Replace 8 sync file operations with aiofiles in async context (storage, mail,
report_generator, dms_bridge, ai_assistant)
- Frontend bundle splitting: manualChunks for react-vendor, ui-components, tanstack,
markdown, icons, utils, i18n (ui chunk 936K → ~19K)
MEDIUM (Architecture):
- Worker circular deps: Replace direct plugin imports with job_registry.py pattern
(register_job/get_all_jobs, importlib-based lazy loading)
- App-wide ErrorBoundary: New ErrorBoundary.tsx component, wrapped in AppShell
and all standalone routes
LOW (Code Quality):
- N+1 query fix: selectinload(Contact.contact_persons) in list_contacts()
- O(n²) dedup fix: SQL GROUP BY for email/phone duplicates, Dict-based name grouping
- Response format standardization: 7 routes converted from plain arrays to
{items: [...], total: N} format
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
Job Registry — decouples ARQ worker from direct plugin imports.
|
||||
|
||||
Plugins register their job functions via register_job() at import time.
|
||||
The worker retrieves all registered jobs via get_all_jobs() instead of
|
||||
importing from plugin modules directly.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any, Callable, Coroutine
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Type alias for an async job function
|
||||
JobFunc = Callable[..., Coroutine[Any, Any, Any]]
|
||||
|
||||
# Internal registry: name -> job function
|
||||
_registry: dict[str, JobFunc] = {}
|
||||
|
||||
|
||||
def register_job(name: str, func: JobFunc) -> None:
|
||||
"""Register a job function under the given name.
|
||||
|
||||
Args:
|
||||
name: Unique job name (e.g. 'index_mails').
|
||||
func: The async callable to register.
|
||||
"""
|
||||
if name in _registry:
|
||||
logger.warning("Job '%s' is being re-registered — overwriting", name)
|
||||
_registry[name] = func
|
||||
logger.debug("Registered job: %s", name)
|
||||
|
||||
|
||||
def get_job(name: str) -> JobFunc | None:
|
||||
"""Retrieve a registered job function by name.
|
||||
|
||||
Args:
|
||||
name: The job name to look up.
|
||||
|
||||
Returns:
|
||||
The registered callable, or None if not found.
|
||||
"""
|
||||
return _registry.get(name)
|
||||
|
||||
|
||||
def get_all_jobs() -> list[JobFunc]:
|
||||
"""Return all registered job functions (order is insertion order).
|
||||
|
||||
Returns:
|
||||
List of all registered async callables.
|
||||
"""
|
||||
return list(_registry.values())
|
||||
|
||||
|
||||
def clear_registry() -> None:
|
||||
"""Clear all registered jobs. Useful for testing."""
|
||||
_registry.clear()
|
||||
logger.debug("Job registry cleared")
|
||||
Reference in New Issue
Block a user