#!/usr/bin/env python3 """Run MercadoLibre sync (stock/price + orders) for all enabled tenants. Called by systemd timer every 15 minutes. Runs the Celery task bodies synchronously in-process (no broker needed from the timer context). """ import os import sys sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'pos')) import psycopg2 # noqa: E402 MASTER_DB = os.environ.get('MASTER_DB_NAME', 'nexus_autoparts') def main(): conn = psycopg2.connect(dbname=MASTER_DB) cur = conn.cursor() cur.execute(""" SELECT t.id FROM tenants t JOIN tenant_schema_version v ON v.tenant_id = t.id WHERE t.is_active ORDER BY t.id """) tenant_ids = [r[0] for r in cur.fetchall()] cur.close() conn.close() from tasks import sync_meli_stock_price_task, sync_meli_orders_task for tid in tenant_ids: try: r1 = sync_meli_stock_price_task.run(tid) r2 = sync_meli_orders_task.run(tid) print(f'[tenant {tid}] stock={r1} orders={r2}', flush=True) except Exception as e: # Meli disabled / not connected for this tenant is the common case msg = str(e) if 'not configured' in msg or 'not connected' in msg: continue print(f'[tenant {tid}] ERROR: {msg[:200]}', flush=True) print('MeLi sync round complete.', flush=True) if __name__ == '__main__': main()