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:
kbe
2025-08-12 02:02:20 +02:00
parent cfbb857cfb
commit fa9d73a9a9
3 changed files with 72 additions and 1 deletions

View File

@@ -213,6 +213,8 @@ class Booker:
# Wait for a short time before next check # Wait for a short time before next check
time.sleep(60) time.sleep(60)
else: 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 # Check again in 5 minutes if outside booking window
time.sleep(300) time.sleep(300)
except Exception as e: except Exception as e:

View File

@@ -84,7 +84,10 @@ class CrossFitBooker:
This method initiates the booking process by running the Booker's main execution loop. This method initiates the booking process by running the Booker's main execution loop.
""" """
import asyncio import asyncio
asyncio.run(self.booker.run()) 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]: def get_auth_headers(self) -> Dict[str, str]:
""" """

View 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()