Skip to content
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

add request count to max requests close reason #154

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion scrapy_zyte_api/_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def process_request(self, request, spider):
slot.delay = 0

if self._max_requests_reached(downloader):
self._crawler.engine.close_spider(spider, "closespider_max_zapi_requests")
self._crawler.engine.close_spider(
spider, f"closespider_max_{self._max_requests}_zapi_requests"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to have the close reasons static, so that they are not unique each run. If we need any additional information, then stats look like a better place.

)
raise IgnoreRequest(
f"The request {request} is skipped as {self._max_requests} max "
f"Zyte API requests have been reached."
Expand Down
5 changes: 4 additions & 1 deletion tests/test_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ def parse(self, response):
assert crawler.stats.get_value("scrapy-zyte-api/success") <= zapi_max_requests
assert crawler.stats.get_value("scrapy-zyte-api/processed") == zapi_max_requests
assert crawler.stats.get_value("item_scraped_count") == zapi_max_requests + 6
assert crawler.stats.get_value("finish_reason") == "closespider_max_zapi_requests"
assert (
crawler.stats.get_value("finish_reason")
== f"closespider_max_{zapi_max_requests}_zapi_requests"
)
assert (
crawler.stats.get_value(
"downloader/exception_type_count/scrapy.exceptions.IgnoreRequest"
Expand Down