Spaces:
Running

microfunnel-api / backend /subscription_cron.py
MicroFunnel
Production-ready: Refactored codebase with Railway + Vercel config
cd61bf7
Raw History Blame Contribute Delete
1.95 kB
"""
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())