feat(01-03): implement retry mechanism with tenacity
- retry_on_network_error decorator for automatic retry - Max 3 attempts with exponential backoff (2s, 4s, max 10s) - Only retries on NetworkError - Logs retry attempts via before_sleep_log - Reraises original exception after max attempts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""Retry mechanism for KB Pipeline
|
||||
|
||||
Provides automatic retry for network errors using tenacity library.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from tenacity import (
|
||||
retry,
|
||||
stop_after_attempt,
|
||||
wait_exponential,
|
||||
retry_if_exception_type,
|
||||
before_sleep_log,
|
||||
)
|
||||
|
||||
from .errors import NetworkError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
retry_on_network_error = retry(
|
||||
stop=stop_after_attempt(3),
|
||||
wait=wait_exponential(multiplier=1, min=2, max=10),
|
||||
retry=retry_if_exception_type(NetworkError),
|
||||
before_sleep=before_sleep_log(logger, logging.WARNING),
|
||||
reraise=True,
|
||||
)
|
||||
"""Decorator for automatic retry on network errors
|
||||
|
||||
Automatically retries functions that raise NetworkError up to 3 times
|
||||
with exponential backoff (2s, 4s, up to max 10s).
|
||||
|
||||
Example:
|
||||
@retry_on_network_error
|
||||
def fetch_data(url):
|
||||
response = requests.get(url)
|
||||
if response.status_code >= 500:
|
||||
raise NetworkError(
|
||||
step_name="fetch",
|
||||
input_summary=f"url={url}",
|
||||
status_code=response.status_code
|
||||
)
|
||||
return response.json()
|
||||
|
||||
Args:
|
||||
func: Function to wrap with retry logic
|
||||
|
||||
Returns:
|
||||
Wrapped function with retry logic
|
||||
|
||||
Raises:
|
||||
NetworkError: After 3 failed attempts
|
||||
"""
|
||||
Reference in New Issue
Block a user