-
Notifications
You must be signed in to change notification settings - Fork 607
fix: Do not share state between different crawlers unless requested #1669
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
Open
Pijukatel
wants to merge
5
commits into
master
Choose a base branch
from
add-crawler-id
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−24
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
88e0fb1
Expand existing test
Pijukatel 31e16d2
Version 1: State depends on crawler_id, but stats does not.
Pijukatel 1bbb651
Draft of use_state as input argument
Pijukatel 415299f
Revert "Draft of use_state as input argument"
Pijukatel 01335f0
Rename `crawler_id` to just `id`. Polish.
Pijukatel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,6 +213,10 @@ class _BasicCrawlerOptions(TypedDict): | |
| """Allows overriding the default status message. The default status message is provided in the parameters. | ||
| Returning `None` suppresses the status message.""" | ||
|
|
||
| id: NotRequired[int] | ||
| """Id of the crawler used for state tracking. You can use same explicit id to share state between two crawlers. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please ask an LLM to improve the wording here 🙂 |
||
| By default, each crawler will use own state.""" | ||
|
|
||
|
|
||
| class _BasicCrawlerOptionsGeneric(TypedDict, Generic[TCrawlingContext, TStatisticsState]): | ||
| """Generic options the `BasicCrawler` constructor.""" | ||
|
|
@@ -266,6 +270,7 @@ class BasicCrawler(Generic[TCrawlingContext, TStatisticsState]): | |
|
|
||
| _CRAWLEE_STATE_KEY = 'CRAWLEE_STATE' | ||
| _request_handler_timeout_text = 'Request handler timed out after' | ||
| __next_id = 0 | ||
|
|
||
| def __init__( | ||
| self, | ||
|
|
@@ -297,6 +302,7 @@ def __init__( | |
| status_message_logging_interval: timedelta = timedelta(seconds=10), | ||
| status_message_callback: Callable[[StatisticsState, StatisticsState | None, str], Awaitable[str | None]] | ||
| | None = None, | ||
| id: int | None = None, | ||
| _context_pipeline: ContextPipeline[TCrawlingContext] | None = None, | ||
| _additional_context_managers: Sequence[AbstractAsyncContextManager] | None = None, | ||
| _logger: logging.Logger | None = None, | ||
|
|
@@ -349,13 +355,21 @@ def __init__( | |
| status_message_logging_interval: Interval for logging the crawler status messages. | ||
| status_message_callback: Allows overriding the default status message. The default status message is | ||
| provided in the parameters. Returning `None` suppresses the status message. | ||
| id: Id of the crawler used for state tracking. You can use same explicit id to share state and between two | ||
| crawlers. By default, each crawler will use own state. | ||
| _context_pipeline: Enables extending the request lifecycle and modifying the crawling context. | ||
| Intended for use by subclasses rather than direct instantiation of `BasicCrawler`. | ||
| _additional_context_managers: Additional context managers used throughout the crawler lifecycle. | ||
| Intended for use by subclasses rather than direct instantiation of `BasicCrawler`. | ||
| _logger: A logger instance, typically provided by a subclass, for consistent logging labels. | ||
| Intended for use by subclasses rather than direct instantiation of `BasicCrawler`. | ||
| """ | ||
| if id is None: | ||
| self._id = BasicCrawler.__next_id | ||
| BasicCrawler.__next_id += 1 | ||
| else: | ||
| self._id = id | ||
|
|
||
| implicit_event_manager_with_explicit_config = False | ||
| if not configuration: | ||
| configuration = service_locator.get_configuration() | ||
|
|
@@ -831,7 +845,7 @@ async def _use_state( | |
| default_value: dict[str, JsonSerializable] | None = None, | ||
| ) -> dict[str, JsonSerializable]: | ||
| kvs = await self.get_key_value_store() | ||
| return await kvs.get_auto_saved_value(self._CRAWLEE_STATE_KEY, default_value) | ||
| return await kvs.get_auto_saved_value(f'{self._CRAWLEE_STATE_KEY}_{self._id}', default_value) | ||
|
|
||
| async def _save_crawler_state(self) -> None: | ||
| store = await self.get_key_value_store() | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a string in JS and it makes more sense that way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done the same way as in Statistics and StatisticsState. I would like to stay consistent with those types in this repo in the first place. Changing them to a string would be a breaking change.
So keeping this int for now and creating V2 issue to change them to a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dunno, not a fan of "for now" solutions, but if V2 happens this year, we can do it that way.