feat: enhance booking process with improved error handling and logging
Added logging to display message when outside booking window in booker.py Added error handling for asyncio.run in crossfit_booker.py Added logging for errors during booking process
This commit is contained in:
@@ -213,6 +213,8 @@ class Booker:
|
||||
# Wait for a short time before next check
|
||||
time.sleep(60)
|
||||
else:
|
||||
# Display message when outside booking window
|
||||
logging.info(f"Not booking now - current time {current_time} is outside the booking window ({target_time} to {booking_window_end})")
|
||||
# Check again in 5 minutes if outside booking window
|
||||
time.sleep(300)
|
||||
except Exception as e:
|
||||
|
||||
@@ -84,7 +84,10 @@ class CrossFitBooker:
|
||||
This method initiates the booking process by running the Booker's main execution loop.
|
||||
"""
|
||||
import asyncio
|
||||
try:
|
||||
asyncio.run(self.booker.run())
|
||||
except Exception as e:
|
||||
logging.error(f"Error in booking process: {str(e)}")
|
||||
|
||||
def get_auth_headers(self) -> Dict[str, str]:
|
||||
"""
|
||||
|
||||
66
test/test_booking_window.py
Normal file
66
test/test_booking_window.py
Normal file
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify booking window functionality.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
import pytz
|
||||
|
||||
# Add the parent directory to the path
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from src.crossfit_booker import CrossFitBooker
|
||||
from src.booker import Booker
|
||||
from src.auth import AuthHandler
|
||||
|
||||
# Set up logging
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
# Mock the login method to avoid actual authentication
|
||||
def mock_login(self) -> bool:
|
||||
self.auth_token = "mock_token"
|
||||
self.user_id = "12345"
|
||||
return True
|
||||
|
||||
# Test the booking window functionality
|
||||
def test_booking_window():
|
||||
"""Test the booking window functionality."""
|
||||
# Create a booker instance
|
||||
booker = CrossFitBooker()
|
||||
|
||||
# Replace the login method with our mock
|
||||
original_login = AuthHandler.login
|
||||
AuthHandler.login = mock_login
|
||||
|
||||
# Set up timezone and target time
|
||||
tz = pytz.timezone("Europe/Paris")
|
||||
current_time = datetime.now(tz)
|
||||
|
||||
# Get the target time from the environment variable or use default
|
||||
target_time_str = os.environ.get("TARGET_RESERVATION_TIME", "20:01")
|
||||
target_hour, target_minute = map(int, target_time_str.split(":"))
|
||||
target_time = current_time.replace(hour=target_hour, minute=target_minute, second=0, microsecond=0)
|
||||
|
||||
# Calculate booking window end
|
||||
booking_window_end = target_time + timedelta(minutes=10)
|
||||
|
||||
# Display current time and booking window
|
||||
logging.info(f"Current time: {current_time}")
|
||||
logging.info(f"Target booking time: {target_time}")
|
||||
logging.info(f"Booking window end: {booking_window_end}")
|
||||
|
||||
# Check if we're in the booking window
|
||||
if target_time <= current_time <= booking_window_end:
|
||||
logging.info("We are within the booking window!")
|
||||
else:
|
||||
logging.info("We are outside the booking window.")
|
||||
time_diff = (target_time - current_time).total_seconds()
|
||||
logging.info(f"Next booking window starts in: {time_diff//60} minutes and {time_diff%60:.0f} seconds")
|
||||
|
||||
# Restore the original login method
|
||||
AuthHandler.login = original_login
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_booking_window()
|
||||
Reference in New Issue
Block a user