Fix database locking during ZIP file downloads

- Release database session before ZIP downloads to prevent locking
- Process all match data first, then download ZIP files separately
- This prevents database locks during potentially slow ZIP downloads
- Allows other database operations to proceed between different ZIP downloads
- Maintains proper error handling and heartbeat updates during downloads
parent 0a16e903
......@@ -236,6 +236,7 @@ class UpdatesResponseHandler(ResponseHandler):
processed_data['message'] = "No fixtures in response"
return processed_data
# First pass: synchronize all match data to database
session = self.db_manager.get_session()
try:
for fixture_data in fixtures:
......@@ -256,12 +257,6 @@ class UpdatesResponseHandler(ResponseHandler):
self._synchronize_match(session, match_data)
processed_data['synchronized_matches'] += 1
# Download ZIP file if available (check match-level zip_download_url)
if 'zip_download_url' in match_data:
match_data['zip_url'] = match_data['zip_download_url']
if self._download_zip_file(match_data):
processed_data['downloaded_zips'] += 1
except Exception as e:
error_msg = f"Failed to process match {match_data.get('match_number', 'unknown')} in fixture {fixture_data.get('fixture_id', 'unknown')}: {e}"
logger.error(error_msg)
......@@ -274,10 +269,32 @@ class UpdatesResponseHandler(ResponseHandler):
self.api_client.heartbeat()
session.commit()
finally:
session.close()
# Second pass: download ZIP files without holding database session
# This prevents database locking during potentially slow downloads
for fixture_data in fixtures:
matches = fixture_data.get('matches', [])
for match_data in matches:
try:
# Update heartbeat before each download
if self.api_client:
self.api_client.heartbeat()
# Download ZIP file if available (check match-level zip_download_url)
if 'zip_download_url' in match_data:
match_data['zip_url'] = match_data['zip_download_url']
if self._download_zip_file(match_data):
processed_data['downloaded_zips'] += 1
except Exception as e:
error_msg = f"Failed to download ZIP for match {match_data.get('match_number', 'unknown')}: {e}"
logger.error(error_msg)
processed_data['errors'].append(error_msg)
# Continue with other downloads even if this one fails
continue
logger.info(f"Synchronized {processed_data['synchronized_matches']} matches, downloaded {processed_data['downloaded_zips']} ZIP files")
return processed_data
......
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