fix batch pool leak and add page cap
This commit is contained in:
@@ -919,6 +919,41 @@ class EncarScraper:
|
||||
excluded_brands: set[str] | None = None,
|
||||
runtime_filters: FiltersConfig | None = None,
|
||||
probe_all_photos: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Публичная обёртка: гарантирует закрытие HTTP pool даже при ошибках."""
|
||||
try:
|
||||
return self._sync_listing_impl(
|
||||
limit=limit,
|
||||
filters=filters,
|
||||
lane=lane,
|
||||
only_new=only_new,
|
||||
batch_size=batch_size,
|
||||
redis_client=redis_client,
|
||||
allowed_brands=allowed_brands,
|
||||
excluded_brands=excluded_brands,
|
||||
runtime_filters=runtime_filters,
|
||||
probe_all_photos=probe_all_photos,
|
||||
)
|
||||
finally:
|
||||
if self._batch_pool is not None:
|
||||
try:
|
||||
self._batch_pool.close()
|
||||
except Exception:
|
||||
logger.debug("Failed to close batch pool", exc_info=True)
|
||||
self._batch_pool = None
|
||||
|
||||
def _sync_listing_impl(
|
||||
self,
|
||||
limit: int | None = None,
|
||||
filters: EncarFilters | None = None,
|
||||
lane: str = "encar",
|
||||
only_new: bool = False,
|
||||
batch_size: int = 1000,
|
||||
redis_client: Any | None = None,
|
||||
allowed_brands: set[str] | None = None,
|
||||
excluded_brands: set[str] | None = None,
|
||||
runtime_filters: FiltersConfig | None = None,
|
||||
probe_all_photos: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Полная синхронизация листинга Encar.
|
||||
|
||||
@@ -988,11 +1023,21 @@ class EncarScraper:
|
||||
max_consecutive_errors = 5
|
||||
consecutive_zero_new = 0
|
||||
max_consecutive_zero_new = 5
|
||||
# Защита от бесконечной пагинации: Encar API отдаёт максимум
|
||||
# ~10k записей на query, шарды строятся ≤9500 → не более ~10
|
||||
# страниц при page_size=1000. Жёсткий cap в 50 — паранойя.
|
||||
max_pages_per_shard = 50
|
||||
|
||||
shard_query = shard_filters.build_query()
|
||||
shard_collected = 0
|
||||
|
||||
while True:
|
||||
if page >= max_pages_per_shard:
|
||||
logger.warning(
|
||||
"Shard %d hit hard page cap (%d), moving to next",
|
||||
shard_idx, max_pages_per_shard,
|
||||
)
|
||||
break
|
||||
offset = page * page_size
|
||||
try:
|
||||
response = self._fetch_listing_page(
|
||||
@@ -1152,11 +1197,6 @@ class EncarScraper:
|
||||
|
||||
self._clear_checkpoint(redis_client)
|
||||
|
||||
# Закрываем pool batch API
|
||||
if self._batch_pool is not None:
|
||||
self._batch_pool.close()
|
||||
self._batch_pool = None
|
||||
|
||||
logger.info(
|
||||
"Full sync complete: %d shards, %d collected, %d synced, %d failed, %d marked sold",
|
||||
len(shards), items_collected, synced, failed, marked_sold,
|
||||
|
||||
Reference in New Issue
Block a user