""" Subscription Renewal CRON Job Run this daily to process recurring subscription payments. Usage: python subscription_cron.py Or schedule via system cron: 0 2 * * * cd /path/to/backend && python subscription_cron.py """ import asyncio import sys from pathlib import Path # Add parent directory to path sys.path.insert(0, str(Path(__file__).parent)) import httpx from datetime import datetime async def trigger_renewal_job(): """Trigger the renewal API endpoint.""" print(f"\n{'='*80}") print(f"šŸ• CRON JOB STARTED: {datetime.utcnow().isoformat()}") print(f"{'='*80}\n") api_url = "http://localhost:8000/api/v1/billing/renewals/process" try: async with httpx.AsyncClient(timeout=300.0) as client: # 5 min timeout print(f"šŸ“” Calling API: {api_url}") response = await client.post(api_url) if response.status_code == 200: data = response.json() print(f"\nāœ… RENEWAL JOB COMPLETED") print(f" Renewed: {data.get('renewed_count', 0)} tenants") print(f" Revenue: ${data.get('total_revenue', 0):.2f}") print(f" Partner Commissions: ${data.get('total_commission', 0):.2f}") else: print(f"\nāŒ ERROR: {response.status_code}") print(f" Response: {response.text}") sys.exit(1) except httpx.ConnectError: print("\nāŒ ERROR: Cannot connect to API server") print(" Make sure the backend is running on http://localhost:8000") sys.exit(1) except Exception as e: print(f"\nāŒ UNEXPECTED ERROR: {e}") sys.exit(1) print(f"\n{'='*80}") print(f"šŸ• CRON JOB FINISHED: {datetime.utcnow().isoformat()}") print(f"{'='*80}\n") if __name__ == "__main__": asyncio.run(trigger_renewal_job())