2026-07-23 23:41:34 +02:00
|
|
|
"""ARQ reminder job for due tasks — sends notifications for overdue/due tasks."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
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 sqlalchemy import select
|
|
|
|
|
|
2026-08-16 01:17:18 +02:00
|
|
|
from app.core.notifications import create_notification
|
|
|
|
|
from app.models.tenant import Tenant
|
|
|
|
|
from app.plugins.builtins.tasks.services import get_due_tasks
|
|
|
|
|
|
2026-07-23 23:41:34 +02:00
|
|
|
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
|
2026-07-25 03:22:55 +02:00
|
|
|
|
|
|
|
|
# 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'],
|
|
|
|
|
})
|
2026-07-23 23:41:34 +02:00
|
|
|
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()
|
2026-07-25 09:19:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Register all job functions with the job registry
|
2026-08-16 01:17:18 +02:00
|
|
|
from app.core.job_registry import register_job # noqa: E402
|
2026-07-25 09:19:32 +02:00
|
|
|
|
|
|
|
|
register_job("tasks_due_reminder", tasks_due_reminder)
|