refactor: parallelize tariff worker panel calls and unblock startup sync
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional, Union
|
||||
@@ -16,6 +17,11 @@ from db.models import Subscription
|
||||
|
||||
router = Router(name="admin_sync_router")
|
||||
|
||||
# Single-flight guard: panel sync runs concurrently with the bot, but only one
|
||||
# sync at a time. Overlapping callers (startup, /sync, admin API) return early
|
||||
# instead of queueing behind the running sync.
|
||||
_sync_lock = asyncio.Lock()
|
||||
|
||||
|
||||
def _normalize_panel_email(value: Optional[str]) -> Optional[str]:
|
||||
email = (value or "").strip().lower()
|
||||
@@ -122,6 +128,31 @@ async def perform_sync(
|
||||
session: AsyncSession,
|
||||
settings: Settings,
|
||||
i18n_instance: JsonI18n,
|
||||
) -> dict:
|
||||
"""Single-flight entry point — skips when another sync is already running."""
|
||||
if _sync_lock.locked():
|
||||
logging.info("perform_sync: skipped because another sync is already in progress")
|
||||
return {
|
||||
"status": "skipped",
|
||||
"details": "Another sync run is already in progress.",
|
||||
"errors": [],
|
||||
"users_processed": 0,
|
||||
"subs_synced": 0,
|
||||
}
|
||||
async with _sync_lock:
|
||||
return await _perform_sync_impl(
|
||||
panel_service=panel_service,
|
||||
session=session,
|
||||
settings=settings,
|
||||
i18n_instance=i18n_instance,
|
||||
)
|
||||
|
||||
|
||||
async def _perform_sync_impl(
|
||||
panel_service: PanelApiService,
|
||||
session: AsyncSession,
|
||||
settings: Settings,
|
||||
i18n_instance: JsonI18n,
|
||||
) -> dict:
|
||||
"""
|
||||
Perform panel synchronization and return results
|
||||
|
||||
Reference in New Issue
Block a user