From 8a5d1b37871a0196cc214511880c8fb2913f56c9 Mon Sep 17 00:00:00 2001 From: Viktor Barzin Date: Sun, 8 Feb 2026 14:50:09 +0000 Subject: [PATCH] Fix POI distance calculation: OSRM index separator and error handling - Fix OSRM client to use semicolons (not commas) for source/destination indices in /table API requests. Commas caused "Query string malformed" errors for any batch with more than one origin. - Add error handling in poi_distance_calculator for unreachable routing engines (OSRM/OTP). Connection failures now log an error and skip the mode instead of crashing the entire Celery task. --- rec/osrm_client.py | 4 ++-- services/poi_distance_calculator.py | 37 ++++++++++++++++++----------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/rec/osrm_client.py b/rec/osrm_client.py index 14359b9..d8e9801 100644 --- a/rec/osrm_client.py +++ b/rec/osrm_client.py @@ -47,8 +47,8 @@ async def osrm_table( coords_str = ";".join(f"{lng},{lat}" for lng, lat in all_coords) # Source/destination indices - source_indices = ",".join(str(i) for i in range(len(origins))) - dest_indices = ",".join(str(i) for i in range(len(origins), len(all_coords))) + source_indices = ";".join(str(i) for i in range(len(origins))) + dest_indices = ";".join(str(i) for i in range(len(origins), len(all_coords))) url = ( f"{base_url}/table/v1/{profile}/{coords_str}" diff --git a/services/poi_distance_calculator.py b/services/poi_distance_calculator.py index 6cfc669..7b1378f 100644 --- a/services/poi_distance_calculator.py +++ b/services/poi_distance_calculator.py @@ -88,20 +88,29 @@ async def calculate_poi_distances( f"(skipped {len(existing)} already computed)" ) - if mode_upper in _OSRM_PROFILES: - computed = await _compute_osrm( - pending_listings, poi, mode_upper, listing_type, - config, poi_repo, on_progress, - total_computed, len(listings) * total_modes, - ) - elif mode_upper == "TRANSIT": - computed = await _compute_transit( - pending_listings, poi, listing_type, - config, poi_repo, on_progress, - total_computed, len(listings) * total_modes, - ) - else: - logger.warning(f"Unknown travel mode: {mode_upper}") + try: + if mode_upper in _OSRM_PROFILES: + computed = await _compute_osrm( + pending_listings, poi, mode_upper, listing_type, + config, poi_repo, on_progress, + total_computed, len(listings) * total_modes, + ) + elif mode_upper == "TRANSIT": + computed = await _compute_transit( + pending_listings, poi, listing_type, + config, poi_repo, on_progress, + total_computed, len(listings) * total_modes, + ) + else: + logger.warning(f"Unknown travel mode: {mode_upper}") + continue + except (aiohttp.ClientError, OSError) as e: + logger.error(f"Routing engine unreachable for {mode_upper}: {e}") + if on_progress: + on_progress( + total_computed, len(listings) * total_modes, + f"{mode_upper}: routing engine unavailable" + ) continue total_computed += computed