fix: add timeout for playwright to load page and debug response saving
This commit is contained in:
@@ -3,17 +3,17 @@ import logging
|
||||
import redis
|
||||
|
||||
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
|
||||
|
||||
# Configure logging
|
||||
# Logger
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Load configuration
|
||||
# Load environment variables
|
||||
WEBINAR_URL = os.getenv('WEBINAR_URL', 'https://edu.edu.vn.ua/webinar/useractive')
|
||||
WEBINAR_CHECK_INTERVAL = int(os.getenv('WEBINAR_CHECK_INTERVAL', 60))
|
||||
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)
|
||||
if not phpsessid:
|
||||
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
|
||||
|
||||
webinar_details = []
|
||||
content = ""
|
||||
|
||||
try:
|
||||
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
|
||||
await page.wait_for_selector('#meetings table', timeout=10000)
|
||||
await page.wait_for_timeout(2000)
|
||||
|
||||
# Get 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)")
|
||||
|
||||
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
|
||||
finally:
|
||||
await page.close()
|
||||
@@ -270,6 +286,16 @@ async def check_webinars_job(context: ContextTypes.DEFAULT_TYPE):
|
||||
logger.error(f"Playwright error: {e}")
|
||||
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
|
||||
if webinar_details:
|
||||
message = "🎓 <b>Найден вебинар!</b>\n\n" + "\n\n".join([f"📌 {d}" for d in webinar_details])
|
||||
|
||||
Reference in New Issue
Block a user