Fix: Handle matches with NULL start_time in cleanup logic

- Updated _cleanup_old_incomplete_matches() to also check matches with NULL start_time using created_at as fallback
- Updated _get_yesterday_incomplete_matches() to also check matches with NULL start_time using created_at as fallback
- Added detailed debug logging to help diagnose date/time issues
- This fixes the issue where matches from old fixtures were not being detected because their start_time was NULL or recently synced
parent e4010d4e
...@@ -320,6 +320,10 @@ class GamesThread(ThreadedComponent): ...@@ -320,6 +320,10 @@ class GamesThread(ThreadedComponent):
This method should be called during initialization to handle matches This method should be called during initialization to handle matches
that were never completed from 2 or more days ago. that were never completed from 2 or more days ago.
It uses multiple strategies to identify old matches:
1. Matches with start_time before yesterday (2+ days ago)
2. Matches with NULL start_time but created_at before yesterday (fallback)
""" """
try: try:
session = self.db_manager.get_session() session = self.db_manager.get_session()
...@@ -337,23 +341,42 @@ class GamesThread(ThreadedComponent): ...@@ -337,23 +341,42 @@ class GamesThread(ThreadedComponent):
# Find all incomplete matches from 2+ days ago # Find all incomplete matches from 2+ days ago
# These are matches that: # These are matches that:
# - Have start_time before yesterday_start (2+ days ago) # - Have start_time before yesterday_start (2+ days ago) OR
# - Have NULL start_time but created_at before yesterday_start
# - Are NOT in terminal states (done, cancelled, failed, paused) # - Are NOT in terminal states (done, cancelled, failed, paused)
# - Have active_status = True # - Have active_status = True
incomplete_statuses = ['pending', 'scheduled', 'bet', 'ingame'] incomplete_statuses = ['pending', 'scheduled', 'bet', 'ingame']
old_incomplete_matches = session.query(MatchModel).filter( # Strategy 1: Matches with start_time set and before yesterday
old_matches_with_start_time = session.query(MatchModel).filter(
MatchModel.start_time.isnot(None), MatchModel.start_time.isnot(None),
MatchModel.start_time < yesterday_start, MatchModel.start_time < yesterday_start,
MatchModel.status.in_(incomplete_statuses), MatchModel.status.in_(incomplete_statuses),
MatchModel.active_status == True MatchModel.active_status == True
).all() ).all()
# Strategy 2: Matches with NULL start_time but created_at before yesterday
# This catches matches that were synced recently but belong to old fixtures
old_matches_without_start_time = session.query(MatchModel).filter(
MatchModel.start_time.is_(None),
MatchModel.created_at < yesterday_start,
MatchModel.status.in_(incomplete_statuses),
MatchModel.active_status == True
).all()
# Combine both sets
old_incomplete_matches = old_matches_with_start_time + old_matches_without_start_time
# Log detailed information for debugging
logger.info(f"🔍 Cleanup check: yesterday_start (UTC) = {yesterday_start}")
logger.info(f"🔍 Found {len(old_matches_with_start_time)} old matches with start_time < yesterday")
logger.info(f"🔍 Found {len(old_matches_without_start_time)} old matches with NULL start_time but old created_at")
if old_incomplete_matches: if old_incomplete_matches:
logger.info(f"⚠️ Found {len(old_incomplete_matches)} incomplete matches from 2+ days ago - marking as failed") logger.info(f"⚠️ Found {len(old_incomplete_matches)} incomplete matches from 2+ days ago - marking as failed")
for match in old_incomplete_matches: for match in old_incomplete_matches:
logger.info(f"Marking old incomplete match {match.match_number} (fixture: {match.fixture_id}) as failed: {match.fighter1_township} vs {match.fighter2_township}") logger.info(f"Marking old incomplete match {match.match_number} (fixture: {match.fixture_id}, start_time: {match.start_time}, created_at: {match.created_at}) as failed: {match.fighter1_township} vs {match.fighter2_township}")
match.status = 'failed' match.status = 'failed'
# Cancel/refund associated bets # Cancel/refund associated bets
...@@ -374,6 +397,10 @@ class GamesThread(ThreadedComponent): ...@@ -374,6 +397,10 @@ class GamesThread(ThreadedComponent):
"""Get all incomplete matches from yesterday. """Get all incomplete matches from yesterday.
Returns a list of matches from yesterday that are not in terminal states. Returns a list of matches from yesterday that are not in terminal states.
Uses multiple strategies to identify yesterday's matches:
1. Matches with start_time in yesterday's date range
2. Matches with NULL start_time but created_at in yesterday's date range (fallback)
""" """
try: try:
session = self.db_manager.get_session() session = self.db_manager.get_session()
...@@ -395,13 +422,31 @@ class GamesThread(ThreadedComponent): ...@@ -395,13 +422,31 @@ class GamesThread(ThreadedComponent):
# Find all incomplete matches from yesterday # Find all incomplete matches from yesterday
incomplete_statuses = ['pending', 'scheduled', 'bet', 'ingame'] incomplete_statuses = ['pending', 'scheduled', 'bet', 'ingame']
yesterday_matches = session.query(MatchModel).filter( # Strategy 1: Matches with start_time in yesterday's range
yesterday_matches_with_start_time = session.query(MatchModel).filter(
MatchModel.start_time.isnot(None), MatchModel.start_time.isnot(None),
MatchModel.start_time >= yesterday_start, MatchModel.start_time >= yesterday_start,
MatchModel.start_time < yesterday_end, MatchModel.start_time < yesterday_end,
MatchModel.status.in_(incomplete_statuses), MatchModel.status.in_(incomplete_statuses),
MatchModel.active_status == True MatchModel.active_status == True
).order_by(MatchModel.match_number.asc()).all() ).all()
# Strategy 2: Matches with NULL start_time but created_at in yesterday's range
yesterday_matches_without_start_time = session.query(MatchModel).filter(
MatchModel.start_time.is_(None),
MatchModel.created_at >= yesterday_start,
MatchModel.created_at < yesterday_end,
MatchModel.status.in_(incomplete_statuses),
MatchModel.active_status == True
).all()
# Combine both sets
yesterday_matches = yesterday_matches_with_start_time + yesterday_matches_without_start_time
# Log detailed information for debugging
logger.info(f"🔍 Yesterday check: yesterday_start (UTC) = {yesterday_start}, yesterday_end (UTC) = {yesterday_end}")
logger.info(f"🔍 Found {len(yesterday_matches_with_start_time)} yesterday matches with start_time in range")
logger.info(f"🔍 Found {len(yesterday_matches_without_start_time)} yesterday matches with NULL start_time but created_at in range")
if yesterday_matches: if yesterday_matches:
logger.info(f"Found {len(yesterday_matches)} incomplete matches from yesterday") logger.info(f"Found {len(yesterday_matches)} incomplete matches from yesterday")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment