fix: retry transient official source failures
This commit is contained in:
@@ -17,10 +17,12 @@ from pathlib import Path
|
||||
from typing import Any, Iterable
|
||||
|
||||
import requests
|
||||
from requests.adapters import HTTPAdapter
|
||||
from pyproj import Transformer
|
||||
from shapely.geometry import GeometryCollection, MultiPolygon, Polygon, mapping, shape
|
||||
from shapely.ops import transform, unary_union
|
||||
from shapely.validation import make_valid
|
||||
from urllib3.util.retry import Retry
|
||||
|
||||
|
||||
MUNICIPALITY_NAME = "Mol"
|
||||
@@ -40,6 +42,7 @@ DEFAULT_API_URL = "http://127.0.0.1:8000"
|
||||
DEFAULT_PAGE_LIMIT = 1000
|
||||
DEFAULT_MAX_FEATURES = 100000
|
||||
GEOJSON_CRS = {"type": "name", "properties": {"name": "EPSG:4326"}}
|
||||
RETRYABLE_SOURCE_STATUS_CODES = (429, 500, 502, 503, 504)
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
@@ -116,6 +119,25 @@ def write_json(path: Path, payload: dict[str, Any], *, pretty: bool = False) ->
|
||||
)
|
||||
|
||||
|
||||
def build_source_session() -> requests.Session:
|
||||
retry = Retry(
|
||||
total=5,
|
||||
connect=5,
|
||||
read=5,
|
||||
status=5,
|
||||
backoff_factor=1.0,
|
||||
status_forcelist=RETRYABLE_SOURCE_STATUS_CODES,
|
||||
allowed_methods=frozenset({"GET"}),
|
||||
raise_on_status=True,
|
||||
)
|
||||
adapter = HTTPAdapter(max_retries=retry)
|
||||
session = requests.Session()
|
||||
session.headers.update({"User-Agent": "GeoIntel-Mol-Municipality-Operator/1.0"})
|
||||
session.mount("https://", adapter)
|
||||
session.mount("http://", adapter)
|
||||
return session
|
||||
|
||||
|
||||
def next_page_url(payload: dict[str, Any]) -> str | None:
|
||||
links = payload.get("links") or []
|
||||
for link in links:
|
||||
@@ -319,8 +341,7 @@ def prepare_artifacts(args: argparse.Namespace) -> tuple[Path, Path, dict[str, A
|
||||
if args.page_limit <= 0 or args.max_features <= 0:
|
||||
raise RuntimeError("--page-limit and --max-features must be positive integers")
|
||||
|
||||
with requests.Session() as session:
|
||||
session.headers.update({"User-Agent": "GeoIntel-Mol-Municipality-Operator/1.0"})
|
||||
with build_source_session() as session:
|
||||
boundary_feature, boundary, boundary_source_url = fetch_mol_boundary(session, args.request_timeout)
|
||||
buildings, building_summary = build_municipality_buildings(
|
||||
iter_grb_pages(
|
||||
|
||||
Reference in New Issue
Block a user