Files
orion/app/modules/marketplace/tasks/sync_tasks.py
Samir Boulahtit f20266167d
Some checks failed
CI / ruff (push) Failing after 7s
CI / pytest (push) Failing after 1s
CI / architecture (push) Failing after 9s
CI / dependency-scanning (push) Successful in 27s
CI / audit (push) Successful in 8s
CI / docs (push) Has been skipped
fix(lint): auto-fix ruff violations and tune lint rules
- Auto-fixed 4,496 lint issues (import sorting, modern syntax, etc.)
- Added ignore rules for patterns intentional in this codebase:
  E402 (late imports), E712 (SQLAlchemy filters), B904 (raise from),
  SIM108/SIM105/SIM117 (readability preferences)
- Added per-file ignores for tests and scripts
- Excluded broken scripts/rename_terminology.py (has curly quotes)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 23:10:42 +01:00

87 lines
2.8 KiB
Python

# app/modules/marketplace/tasks/sync_tasks.py
"""
Celery tasks for Letzshop store directory synchronization.
Periodically syncs store information from Letzshop's public GraphQL API.
"""
import logging
from typing import Any
from app.core.celery_config import celery_app
from app.modules.marketplace.services.letzshop import LetzshopStoreSyncService
from app.modules.messaging.services.admin_notification_service import (
admin_notification_service,
)
from app.modules.task_base import ModuleTask
logger = logging.getLogger(__name__)
@celery_app.task(
bind=True,
base=ModuleTask,
name="app.modules.marketplace.tasks.sync_tasks.sync_store_directory",
max_retries=2,
default_retry_delay=300,
autoretry_for=(Exception,),
retry_backoff=True,
)
def sync_store_directory(self) -> dict[str, Any]:
"""
Celery task to sync Letzshop store directory.
Fetches all stores from Letzshop's public GraphQL API and updates
the local letzshop_store_cache table.
This task is scheduled to run daily via the module's scheduled_tasks
definition.
Returns:
dict: Sync statistics including created, updated, and error counts.
"""
with self.get_db() as db:
try:
logger.info("Starting Letzshop store directory sync...")
sync_service = LetzshopStoreSyncService(db)
def progress_callback(page: int, fetched: int, total: int):
"""Log progress during sync."""
logger.info(f"Store sync progress: page {page}, {fetched}/{total} stores")
stats = sync_service.sync_all_stores(progress_callback=progress_callback)
logger.info(
f"Store directory sync completed: "
f"{stats.get('created', 0)} created, "
f"{stats.get('updated', 0)} updated, "
f"{stats.get('errors', 0)} errors"
)
# Send admin notification if there were errors
if stats.get("errors", 0) > 0:
admin_notification_service.notify_system_info(
db=db,
title="Letzshop Store Sync Completed with Errors",
message=(
f"Synced {stats.get('total_fetched', 0)} stores. "
f"Errors: {stats.get('errors', 0)}"
),
details=stats,
)
return stats
except Exception as e:
logger.error(f"Store directory sync failed: {e}", exc_info=True)
# Notify admins of failure
admin_notification_service.notify_critical_error(
db=db,
error_type="Store Directory Sync",
error_message=f"Failed to sync Letzshop store directory: {str(e)[:200]}",
details={"error": str(e)},
)
raise # Re-raise for Celery retry