mirror of
https://github.com/bcye/structured-wikivoyage-exports.git
synced 2025-11-02 08:02:44 +00:00
Merge branch 'main' into feature/docker-multi-stage-builds
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Reference handler for output handlers."""
|
||||
from abc import ABC, abstractmethod
|
||||
import logging
|
||||
import asyncio
|
||||
|
||||
|
||||
|
||||
@@ -14,15 +15,20 @@ class BaseHandler(ABC):
|
||||
_successful_writes = 0
|
||||
_failed_writes = 0
|
||||
|
||||
def __init__(self, fail_on_error: bool = True, **kwargs):
|
||||
def __init__(self, fail_on_error: bool = True, max_concurrent=0, **kwargs):
|
||||
"""
|
||||
Initializes the BaseHandler with optional parameters.
|
||||
|
||||
Args:
|
||||
fail_on_error (bool): If True, the handler will raise an exception on error. Defaults to True.
|
||||
max_concurrent: Maximum number of concurrent write operations.
|
||||
0 means unlimited concurrency.
|
||||
**kwargs: Additional keyword arguments for specific handler implementations.
|
||||
"""
|
||||
self.fail_on_error = fail_on_error
|
||||
self.semaphore = None
|
||||
if max_concurrent > 0:
|
||||
self.semaphore = asyncio.Semaphore(max_concurrent)
|
||||
|
||||
|
||||
@abstractmethod
|
||||
@@ -47,7 +53,11 @@ class BaseHandler(ABC):
|
||||
entry (dict): The entry to write (will be JSON-encoded).
|
||||
uid (str): The unique identifier for the entry. The default id provided by wikivoyage is recommended.
|
||||
"""
|
||||
success = await self._write_entry(entry, uid)
|
||||
if self.semaphore:
|
||||
async with self.semaphore:
|
||||
success = await self._write_entry(entry, uid)
|
||||
else:
|
||||
success = await self._write_entry(entry, uid)
|
||||
if success:
|
||||
self.logger.debug(f"Successfully wrote entry with UID {uid}")
|
||||
self._successful_writes += 1
|
||||
|
||||
@@ -10,8 +10,9 @@ class BunnyStorageHandler(BaseHandler):
|
||||
api_key: str,
|
||||
fail_on_error: bool = True,
|
||||
keepalive_timeout: int = 75,
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__(fail_on_error=fail_on_error)
|
||||
super().__init__(fail_on_error=fail_on_error, **kwargs)
|
||||
self.base_url = f"https://{region}.bunnycdn.com/{base_path}"
|
||||
self.headers = {
|
||||
"AccessKey": api_key,
|
||||
|
||||
Reference in New Issue
Block a user