fix: parallelize WA sync and surface scheduler errors in historial

- whatsapp_sync: send patients concurrently (asyncio.gather + semaphore=10) instead of sequentially — eliminates long waits with many patients
- envios_erp: auto-refresh historial every 60s when scheduler tab is active
- main: wrap scheduler job to catch exceptions and log them to sync_wa_log so failures are visible in historial

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-23 11:34:30 -05:00
co-authored by Claude Sonnet 4.6
parent 5ca97159b4
commit cffeae7e94
3 changed files with 61 additions and 21 deletions
+22 -2
View File
@@ -13,6 +13,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
from app.database import init_db
from app.auth import decode_token
from app.services.scheduler import sync_recientes
from app.services.whatsapp_sync import guardar_sync_log
app = FastAPI(title="RIPS Manager", version="1.0.0")
_scheduler = AsyncIOScheduler()
@@ -63,8 +64,27 @@ async def startup():
config_defaults()
query_defaults()
contratos_defaults()
_scheduler.add_job(sync_recientes, "interval", minutes=1, id="wa_sync_recientes",
kwargs={"ventana_min": 10}, max_instances=1, coalesce=True)
async def _job_sync():
try:
resultado = await sync_recientes(ventana_min=10)
if not resultado.get("ok") and resultado.get("error"):
guardar_sync_log(
{"total": 0, "created": 0, "skipped": 0, "updated": 0, "errores": 1, "detalle": [
{"ok": False, "action": "error", "message": resultado["error"], "doc": "", "nombre": "scheduler-error"}
]},
None, origen="scheduler", modo="upsert"
)
except Exception as exc:
import traceback
guardar_sync_log(
{"total": 0, "created": 0, "skipped": 0, "updated": 0, "errores": 1, "detalle": [
{"ok": False, "action": "error", "message": str(exc), "doc": "", "nombre": "scheduler-exception"}
]},
None, origen="scheduler", modo="upsert"
)
_scheduler.add_job(_job_sync, "interval", minutes=1, id="wa_sync_recientes",
max_instances=1, coalesce=True)
_scheduler.start()