Spaces:
Running

File size: 1,946 Bytes
cd61bf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
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())