-
Notifications
You must be signed in to change notification settings - Fork 421
refactor!: Introduce new storage client system #1194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
f285707
refactor!: Introduce new storage client system
vdusek dd9be6e
Cleanup
vdusek 89bfa5b
Address feedback
vdusek 4050c75
Add purge_if_needed method and improve some typing based on Pylance
vdusek 26f46e2
Address more feedback
vdusek c83a36a
RQ FS client improvements
vdusek c967fe5
Add caching to RQ FS client
vdusek 7df046f
RQ FS performance optimization in add_requests
vdusek 3555565
RQ FS performance issues in fetch_next_request
vdusek 946d1e2
RQ FS fetch performance for is_empty
vdusek 9f10b95
rm code duplication for open methods
vdusek 0864ff8
Request loaders use async getters for handled/total req cnt
vdusek af0d129
Add missing_ok when removing files
vdusek 9998a58
Improve is_empty
vdusek fdee111
Optimize RQ memory storage client
vdusek 79cdfc0
Add upgrading guide and skip problematic test
vdusek 3d2fd73
Merge branch 'master' into new-storage-clients
vdusek e818585
chore: update `docusaurus-plugin-typedoc-api`, fix failing docs build
barjin 65db9ac
fix docs
vdusek 2b786f7
add retries to atomic write
vdusek 2cb04c5
chore(deps): update dependency pytest-cov to ~=6.2.0 (#1244)
renovate[bot] 0c8c4ec
Fix atomic write on Windows
vdusek ce1eeb1
resolve write function during import time
vdusek 4c05cee
Merge branch 'master' into new-storage-clients
vdusek 8c80513
Update file utils
vdusek 70bc071
revert un-intentionally makefile changes
vdusek 78efb4d
Address Honza's comments (p1)
vdusek fa18d19
Introduce storage instance manager
vdusek c783dac
Utilize recoverable state for the FS RQ state
vdusek 437071e
Details
vdusek df4bfa7
Rm default_"storage"_id options (were not used at all)
vdusek e133fcd
Update storages guide and add storage clients guide
vdusek 76f1ffb
Docs guides - code examples
vdusek fa48644
Docs guides polishment
vdusek 5c935af
docs fix lint & type checks for py 3.9
vdusek ac259ce
Address Honza's feedback
vdusek 1cbf15e
SDK fixes
vdusek bc50990
Add KVS record_exists method
vdusek d1cf967
reduce test duplicities for storages & storage clients
vdusek aa9bfd3
Create locks in async context only
vdusek d6c9877
rm open methods from base storage clients
vdusek 3b133ce
update storage clients inits
vdusek 43b9fe9
async metadata getter
vdusek b628fbb
better typing in storage instance manager
vdusek 9dfac4b
update upgrading guide
vdusek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Callable, TypeVar, cast | ||
|
||
from crawlee._utils.docs import docs_group | ||
|
||
from ._base import Storage | ||
|
||
T = TypeVar('T', bound='Storage') | ||
|
||
|
||
@docs_group('Classes') | ||
class StorageInstanceManager: | ||
"""Manager for caching and managing storage instances. | ||
|
||
This class centralizes the caching logic for all storage types (Dataset, KeyValueStore, RequestQueue) | ||
and provides a unified interface for opening and managing storage instances. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self._cache_by_id = dict[type[Storage], dict[str, Storage]]() | ||
"""Cache for storage instances by ID, separated by storage type.""" | ||
|
||
self._cache_by_name = dict[type[Storage], dict[str, Storage]]() | ||
"""Cache for storage instances by name, separated by storage type.""" | ||
|
||
self._default_instances = dict[type[Storage], Storage]() | ||
"""Cache for default instances of each storage type.""" | ||
|
||
async def open_storage_instance( | ||
self, | ||
cls: type[T], | ||
*, | ||
id: str | None, | ||
name: str | None, | ||
configuration: Any, | ||
client_opener: Callable[..., Any], | ||
) -> T: | ||
"""Open a storage instance with caching support. | ||
|
||
Args: | ||
cls: The storage class to instantiate. | ||
id: Storage ID. | ||
name: Storage name. | ||
configuration: Configuration object. | ||
client_opener: Function to create the storage client. | ||
|
||
Returns: | ||
The storage instance. | ||
|
||
Raises: | ||
ValueError: If both id and name are specified. | ||
""" | ||
if id and name: | ||
raise ValueError('Only one of "id" or "name" can be specified, not both.') | ||
|
||
# Check for default instance | ||
if id is None and name is None and cls in self._default_instances: | ||
return cast('T', self._default_instances[cls]) | ||
|
||
# Check cache | ||
if id is not None: | ||
type_cache_by_id = self._cache_by_id.get(cls, {}) | ||
if id in type_cache_by_id: | ||
cached_instance = type_cache_by_id[id] | ||
if isinstance(cached_instance, cls): | ||
return cached_instance | ||
|
||
if name is not None: | ||
type_cache_by_name = self._cache_by_name.get(cls, {}) | ||
if name in type_cache_by_name: | ||
cached_instance = type_cache_by_name[name] | ||
if isinstance(cached_instance, cls): | ||
return cached_instance | ||
|
||
# Create new instance | ||
client = await client_opener(id=id, name=name, configuration=configuration) | ||
instance = cls(client) # type: ignore[call-arg] | ||
instance_name = getattr(instance, 'name', None) | ||
|
||
# Cache the instance | ||
if cls not in self._cache_by_id: | ||
self._cache_by_id[cls] = {} | ||
if cls not in self._cache_by_name: | ||
self._cache_by_name[cls] = {} | ||
|
||
self._cache_by_id[cls][instance.id] = instance | ||
if instance_name is not None: | ||
self._cache_by_name[cls][instance_name] = instance | ||
|
||
# Set as default if no id/name specified | ||
if id is None and name is None: | ||
self._default_instances[cls] = instance | ||
|
||
return instance | ||
|
||
def remove_from_cache(self, storage_instance: Storage) -> None: | ||
"""Remove a storage instance from the cache. | ||
|
||
Args: | ||
storage_instance: The storage instance to remove. | ||
""" | ||
storage_type = type(storage_instance) | ||
|
||
# Remove from ID cache | ||
type_cache_by_id = self._cache_by_id.get(storage_type, {}) | ||
if storage_instance.id in type_cache_by_id: | ||
del type_cache_by_id[storage_instance.id] | ||
|
||
# Remove from name cache | ||
if storage_instance.name is not None: | ||
type_cache_by_name = self._cache_by_name.get(storage_type, {}) | ||
if storage_instance.name in type_cache_by_name: | ||
del type_cache_by_name[storage_instance.name] | ||
|
||
# Remove from default instances | ||
if storage_type in self._default_instances and self._default_instances[storage_type] is storage_instance: | ||
del self._default_instances[storage_type] | ||
|
||
def clear_cache(self) -> None: | ||
"""Clear all cached storage instances.""" | ||
self._cache_by_id.clear() | ||
self._cache_by_name.clear() | ||
self._default_instances.clear() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.