Files
leocrm/app/core/worker.py
T
Agent Zero aaa7406929 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
2026-07-25 09:19:32 +02:00

86 lines
3.0 KiB
Python

"""ARQ worker configuration and entrypoint."""
from __future__ import annotations
import logging
from typing import Any
from arq.connections import RedisSettings
from arq import cron
from app.config import get_settings
from app.core.job_registry import get_all_jobs, get_job, register_job
logger = logging.getLogger(__name__)
def _get_redis_settings() -> RedisSettings:
"""Get Redis settings from app config."""
settings = get_settings()
return RedisSettings.from_dsn(settings.redis_url)
async def on_startup(ctx: dict[str, Any]) -> None:
"""Called when worker starts."""
logger.info("ARQ worker starting...")
# Register search providers (normally done by app startup)
try:
from app.core.db import get_session_factory
from app.plugins.builtins.unified_search.provider_registry import auto_register_providers
factory = get_session_factory()
async with factory() as db:
await auto_register_providers(db)
logger.info("Search providers registered for worker")
except Exception:
logger.warning("Failed to register search providers in worker", exc_info=True)
async def on_shutdown(ctx: dict[str, Any]) -> None:
"""Called when worker shuts down."""
logger.info("ARQ worker shutting down...")
# ---------------------------------------------------------------------------
# Lazy-load plugin jobs via importlib so they register themselves with the
# job_registry. This avoids circular imports and keeps the worker decoupled
# from plugin internals.
# ---------------------------------------------------------------------------
def _lazy_register_plugin_jobs() -> None:
"""Import each plugin job module so its register_job() call fires."""
plugin_job_modules = [
"app.plugins.builtins.unified_search.jobs",
"app.plugins.builtins.ai_proactive.jobs",
"app.plugins.builtins.automation.scheduler",
"app.plugins.builtins.automation.workflow_timeout",
"app.plugins.builtins.automation.agent_runner",
"app.plugins.builtins.automation.execution_engine",
"app.plugins.builtins.tasks.jobs",
]
for mod_name in plugin_job_modules:
try:
import importlib
importlib.import_module(mod_name)
logger.debug("Lazy-loaded plugin jobs from %s", mod_name)
except Exception:
logger.warning("Failed to lazy-load plugin jobs from %s", mod_name, exc_info=True)
# Trigger lazy registration at module level so jobs are available when
# WorkerSettings.functions is evaluated.
_lazy_register_plugin_jobs()
class WorkerSettings:
"""ARQ worker settings."""
functions = get_all_jobs()
redis_settings = _get_redis_settings()
on_startup = on_startup
on_shutdown = on_shutdown
max_jobs = 10
job_timeout = 300
queue_name = "arq:queue"
cron_jobs = [
cron(get_job("scheduler_tick"), minute={0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55}),
cron(get_job("tasks_due_reminder"), hour=8, minute=0),
]