Watch
1
0
Fork
You've already forked Seyyed_arc
0
forked from hesabix/arc
Seyyed_arc/hesabixAPI/app/services/crm_background_jobs.py
2026-07-31 13:33:00 +00:00

45 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# noqa: D100
"""Jobهای پس‌زمینه CRM: یادآوری پیگیری، وظایف، SLA، فرصت‌های راکد و پردازش توالی‌ها."""
from __future__ import annotations
import logging
from typing import Dict
logger = logging.getLogger(__name__)
def tick_once() -> Dict[str, Dict]:
"""یک بار اجرای همه Jobهای CRM (برای زمان‌بند یا تست)."""
from adapters.db.session import get_db_session
from app.services import crm_automation_service as automation
out: Dict[str, Dict] = {}
with get_db_session() as db:
try:
out["follow_up_reminders"] = automation.process_follow_up_reminders(db)
except Exception as e:
logger.error("crm follow-up reminders tick failed: %s", e, exc_info=True)
out["follow_up_reminders"] = {"error": str(e)}
try:
out["sequences"] = automation.tick_sequences(db)
except Exception as e:
logger.error("crm sequences tick failed: %s", e, exc_info=True)
out["sequences"] = {"error": str(e)}
try:
out["stale_deals"] = automation.flag_stale_deals(db)
except Exception as e:
logger.error("crm stale deals tick failed: %s", e, exc_info=True)
out["stale_deals"] = {"error": str(e)}
return out
async def crm_automation_background_loop(interval_seconds: int = 60) -> None:
"""حلقه پس‌زمینه: هر ۶۰ ثانیه Jobهای CRM را اجرا می‌کند."""
import asyncio
while True:
try:
await asyncio.to_thread(tick_once)
except Exception:
logger.exception("crm_automation_background_loop tick error")
await asyncio.sleep(max(30, int(interval_seconds)))