Spaces:
Running
Running
cronjob first commit
Browse files- cronjob_service.py +58 -13
- requirements.txt +2 -1
cronjob_service.py
CHANGED
|
@@ -1,24 +1,32 @@
|
|
| 1 |
import logging
|
| 2 |
import requests
|
| 3 |
import pytz
|
| 4 |
-
from datetime import
|
| 5 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 6 |
from apscheduler.triggers.cron import CronTrigger
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
import os
|
|
|
|
|
|
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
-
# --- Configuration ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
DAILY_CHAT_RATE_LIMIT_RESET_URL = os.getenv("DAILY_CHAT_RATE_LIMIT_RESET_URL")
|
| 14 |
DAILY_CHAT_RATE_LIMIT_RESET_TOKEN = os.getenv("DAILY_CHAT_RATE_LIMIT_RESET_TOKEN")
|
| 15 |
-
|
| 16 |
API_ONLY_USER_CHAT_LIMIT_RESET_URL = os.getenv("API_ONLY_USER_CHAT_LIMIT_RESET_URL")
|
| 17 |
API_ONLY_USER_CHAT_LIMIT_RESET_TOKEN = os.getenv("API_ONLY_USER_CHAT_LIMIT_RESET_TOKEN")
|
| 18 |
US_TIMEZONE = 'US/Eastern'
|
| 19 |
LOG_FILE = 'cronjob_log.log'
|
| 20 |
|
| 21 |
-
# --- Logging Setup ---
|
| 22 |
logging.basicConfig(
|
| 23 |
level=logging.INFO,
|
| 24 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
@@ -27,24 +35,56 @@ logging.basicConfig(
|
|
| 27 |
logging.StreamHandler()
|
| 28 |
]
|
| 29 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def call_service(url: str, token: str, service_name: str):
|
| 32 |
-
"""
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
"Authorization": f"Bearer {token}",
|
| 36 |
-
"Content-Type": "application/json"
|
| 37 |
-
}
|
| 38 |
|
| 39 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
response = requests.post(url, headers=headers)
|
| 41 |
response.raise_for_status()
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def init_scheduler():
|
| 47 |
-
"""
|
| 48 |
scheduler = BackgroundScheduler(timezone=pytz.timezone(US_TIMEZONE))
|
| 49 |
|
| 50 |
scheduler.add_job(
|
|
@@ -62,4 +102,9 @@ def init_scheduler():
|
|
| 62 |
)
|
| 63 |
|
| 64 |
scheduler.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
return scheduler
|
|
|
|
| 1 |
import logging
|
| 2 |
import requests
|
| 3 |
import pytz
|
| 4 |
+
from datetime import datetime
|
| 5 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 6 |
from apscheduler.triggers.cron import CronTrigger
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
import os
|
| 9 |
+
from twilio.rest import Client
|
| 10 |
+
from twilio.base.exceptions import TwilioRestException
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
+
# --- Twilio Configuration (New Additions) ---
|
| 15 |
+
TWILIO_ENABLED = os.getenv("TWILIO_ENABLED", "false").lower() == "true"
|
| 16 |
+
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
|
| 17 |
+
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
|
| 18 |
+
TWILIO_WHATSAPP_FROM = os.getenv("TWILIO_WHATSAPP_FROM", "whatsapp:+14155238886")
|
| 19 |
+
ADMIN_WHATSAPP_NUMBER = os.getenv("ADMIN_WHATSAPP_NUMBER")
|
| 20 |
+
|
| 21 |
+
# --- Existing Configuration (Unchanged) ---
|
| 22 |
DAILY_CHAT_RATE_LIMIT_RESET_URL = os.getenv("DAILY_CHAT_RATE_LIMIT_RESET_URL")
|
| 23 |
DAILY_CHAT_RATE_LIMIT_RESET_TOKEN = os.getenv("DAILY_CHAT_RATE_LIMIT_RESET_TOKEN")
|
|
|
|
| 24 |
API_ONLY_USER_CHAT_LIMIT_RESET_URL = os.getenv("API_ONLY_USER_CHAT_LIMIT_RESET_URL")
|
| 25 |
API_ONLY_USER_CHAT_LIMIT_RESET_TOKEN = os.getenv("API_ONLY_USER_CHAT_LIMIT_RESET_TOKEN")
|
| 26 |
US_TIMEZONE = 'US/Eastern'
|
| 27 |
LOG_FILE = 'cronjob_log.log'
|
| 28 |
|
| 29 |
+
# --- Existing Logging Setup (Unchanged) ---
|
| 30 |
logging.basicConfig(
|
| 31 |
level=logging.INFO,
|
| 32 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
|
|
| 35 |
logging.StreamHandler()
|
| 36 |
]
|
| 37 |
)
|
| 38 |
+
logger = logging.getLogger(__name__)
|
| 39 |
+
|
| 40 |
+
def send_whatsapp_notification(message: str):
|
| 41 |
+
"""New: Safely send WhatsApp notification without disrupting main logic"""
|
| 42 |
+
if not TWILIO_ENABLED:
|
| 43 |
+
return False
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
|
| 47 |
+
client.messages.create(
|
| 48 |
+
body=f"[CRONJOB] {message}",
|
| 49 |
+
from_=TWILIO_WHATSAPP_FROM,
|
| 50 |
+
to=ADMIN_WHATSAPP_NUMBER
|
| 51 |
+
)
|
| 52 |
+
return True
|
| 53 |
+
except Exception as e:
|
| 54 |
+
logger.warning(f"Notification failed (non-critical): {str(e)}")
|
| 55 |
+
return False
|
| 56 |
|
| 57 |
def call_service(url: str, token: str, service_name: str):
|
| 58 |
+
"""Modified: Existing logic with added notifications"""
|
| 59 |
+
start_time = datetime.now()
|
| 60 |
+
logger.info(f"Starting {service_name} at {start_time}")
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
# Original unchanged logic
|
| 64 |
+
headers = {
|
| 65 |
+
"Authorization": f"Bearer {token}",
|
| 66 |
+
"Content-Type": "application/json"
|
| 67 |
+
}
|
| 68 |
response = requests.post(url, headers=headers)
|
| 69 |
response.raise_for_status()
|
| 70 |
+
|
| 71 |
+
# New success notification
|
| 72 |
+
duration = (datetime.now() - start_time).total_seconds()
|
| 73 |
+
success_msg = f"{service_name} succeeded in {duration:.2f}s (Status: {response.status_code})"
|
| 74 |
+
logger.info(success_msg)
|
| 75 |
+
send_whatsapp_notification(f"✅ {success_msg}")
|
| 76 |
+
|
| 77 |
except Exception as e:
|
| 78 |
+
# Original error handling preserved
|
| 79 |
+
error_msg = f"{service_name} failed: {str(e)}"
|
| 80 |
+
logger.error(error_msg)
|
| 81 |
+
|
| 82 |
+
# New failure notification
|
| 83 |
+
send_whatsapp_notification(f"❌ {error_msg[:200]}") # Truncate long errors
|
| 84 |
+
raise # Maintains existing error propagation
|
| 85 |
|
| 86 |
def init_scheduler():
|
| 87 |
+
"""Unchanged scheduler initialization"""
|
| 88 |
scheduler = BackgroundScheduler(timezone=pytz.timezone(US_TIMEZONE))
|
| 89 |
|
| 90 |
scheduler.add_job(
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
scheduler.start()
|
| 105 |
+
|
| 106 |
+
# New: Optional startup notification
|
| 107 |
+
if TWILIO_ENABLED:
|
| 108 |
+
send_whatsapp_notification("🟢 Scheduler started successfully")
|
| 109 |
+
|
| 110 |
return scheduler
|
requirements.txt
CHANGED
|
@@ -3,4 +3,5 @@ pytz
|
|
| 3 |
fastapi
|
| 4 |
uvicorn
|
| 5 |
requests
|
| 6 |
-
dotenv
|
|
|
|
|
|
| 3 |
fastapi
|
| 4 |
uvicorn
|
| 5 |
requests
|
| 6 |
+
dotenv
|
| 7 |
+
twilio
|