Files
leocrm/app/plugins/builtins/tasks/jobs.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

68 lines
2.5 KiB
Python

"""ARQ reminder job for due tasks — sends notifications for overdue/due tasks."""
from __future__ import annotations
import logging
from datetime import datetime, timezone
from app.core.db import get_session_factory
logger = logging.getLogger(__name__)
async def tasks_due_reminder(ctx: dict) -> None:
"""Check for due tasks and send notifications to assigned users.
Runs daily at 8:00 via cron. Finds all non-done tasks with due_date <= now
and creates a notification for the assigned user.
"""
from app.plugins.builtins.tasks.services import get_due_tasks
from app.models.tenant import Tenant
from app.core.notifications import create_notification
from sqlalchemy import select
factory = get_session_factory()
async with factory() as db:
result = await db.execute(select(Tenant))
tenants = result.scalars().all()
total_notified = 0
for tenant in tenants:
try:
due_tasks = await get_due_tasks(db, tenant.id)
for task in due_tasks:
if not task.get("assigned_to"):
continue
await create_notification(
db,
user_id=task["assigned_to"],
tenant_id=str(tenant.id),
type="task_due",
title=f"Task due: {task['title']}",
body=f"Task '{task['title']}' is due. Priority: {task['priority']}",
data={"task_id": task["id"], "due_date": task.get("due_date")},
)
total_notified += 1
# Publish task.overdue event
from app.core.event_bus import get_event_bus
event_bus = get_event_bus()
await event_bus.publish('task.overdue', {
'event_id': task['id'],
'tenant_id': str(tenant.id),
'user_id': str(task.get('assigned_to', '')),
'title': task['title'],
})
except Exception:
logger.warning(f"Failed to process due tasks for tenant {tenant.id}", exc_info=True)
if total_notified > 0:
logger.info(f"Tasks reminder: sent {total_notified} notifications")
await db.commit()
# Register all job functions with the job registry
from app.core.job_registry import register_job
register_job("tasks_due_reminder", tasks_due_reminder)