fix: add timeout for playwright to load page and debug response saving

This commit is contained in:
2025-11-27 15:50:12 +01:00
parent 5b1fa11b5e
commit a54b7b000f
+30 -4
View File
@@ -3,17 +3,17 @@ import logging
import redis import redis
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Application, CommandHandler, CallbackQueryHandler, ContextTypes, JobQueue from telegram.ext import Application, CommandHandler, CallbackQueryHandler, ContextTypes
from playwright.async_api import async_playwright from playwright.async_api import async_playwright
# Configure logging # Logger
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s' format='%(asctime)s - %(levelname)s - %(message)s'
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Load configuration # Load environment variables
WEBINAR_URL = os.getenv('WEBINAR_URL', 'https://edu.edu.vn.ua/webinar/useractive') WEBINAR_URL = os.getenv('WEBINAR_URL', 'https://edu.edu.vn.ua/webinar/useractive')
WEBINAR_CHECK_INTERVAL = int(os.getenv('WEBINAR_CHECK_INTERVAL', 60)) WEBINAR_CHECK_INTERVAL = int(os.getenv('WEBINAR_CHECK_INTERVAL', 60))
REDIS_HOST = os.getenv('REDIS_HOST', 'redis') REDIS_HOST = os.getenv('REDIS_HOST', 'redis')
@@ -207,9 +207,17 @@ async def check_webinars_job(context: ContextTypes.DEFAULT_TYPE):
phpsessid = redis_client.get(KEY_PHPSESSID) phpsessid = redis_client.get(KEY_PHPSESSID)
if not phpsessid: if not phpsessid:
logger.warning("PHPSESSID missing. Skipping check.") logger.warning("PHPSESSID missing. Skipping check.")
# --- DEBUG LOGGING ---
try:
with open('phpsessid_missing.log', 'a') as f:
f.write(f"[{os.getcwd()}] PHPSESSID missing at {context.job.last_run: %Y-%m-%d %H:%M:%S}\n")
except Exception as e:
logger.error(f"Failed to write PHPSESSID debug log: {e}")
# ---------------------
return None return None
webinar_details = [] webinar_details = []
content = ""
try: try:
async with async_playwright() as p: async with async_playwright() as p:
@@ -237,6 +245,7 @@ async def check_webinars_job(context: ContextTypes.DEFAULT_TYPE):
# Wait for the table to load # Wait for the table to load
await page.wait_for_selector('#meetings table', timeout=10000) await page.wait_for_selector('#meetings table', timeout=10000)
await page.wait_for_timeout(2000)
# Get page content # Get page content
content = await page.content() content = await page.content()
@@ -257,7 +266,14 @@ async def check_webinars_job(context: ContextTypes.DEFAULT_TYPE):
logger.info("No webinars found (expected message present)") logger.info("No webinars found (expected message present)")
except Exception as e: except Exception as e:
logger.error(f"Error checking page: {e}") logger.error(f"Error checking page: {e}. Saving content for debug.")
# If page content is available, save it on error
try:
if page and not content:
content = await page.content()
except Exception:
pass # Ignore error during content retrieval on check error
return None return None
finally: finally:
await page.close() await page.close()
@@ -269,6 +285,16 @@ async def check_webinars_job(context: ContextTypes.DEFAULT_TYPE):
except Exception as e: except Exception as e:
logger.error(f"Playwright error: {e}") logger.error(f"Playwright error: {e}")
return None return None
# --- DEBUG LOGGING (Saving last response content) ---
if not webinar_details and content: #if no webinars found, save the page content
try:
with open('response.html', 'w', encoding='utf-8') as f:
f.write(content)
logger.info("Saved page content to response.html for debug.")
except Exception as e:
logger.error(f"Failed to write debug HTML: {e}")
# -----------------------------------------------------
# Notify subscribers if webinars found # Notify subscribers if webinars found
if webinar_details: if webinar_details: