Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/apify/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ async def reboot(
The system stops the current container and starts a new one, with the same run ID and default storages.

Args:
event_listeners_timeout: How long should the Actor wait for Actor event listeners to finish before exiting
event_listeners_timeout: How long should the Actor wait for Actor event listeners to finish before exiting.
custom_after_sleep: How long to sleep for after the reboot, to wait for the container to be stopped.
"""
self._raise_if_not_initialized()
Expand Down
7 changes: 6 additions & 1 deletion src/apify/scrapy/middlewares/apify_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def from_crawler(cls: type[ApifyHttpProxyMiddleware], crawler: Crawler) -> Apify
if use_apify_proxy is not True:
Actor.log.warning(
'ApifyHttpProxyMiddleware is not going to be used. Actor input field '
'"proxyConfiguration.useApifyProxy" is probably set to False.'
'"proxyConfiguration.useApifyProxy" is set to False.'
)
raise NotConfigured

Expand All @@ -78,6 +78,11 @@ async def process_request(self, request: Request, spider: Spider) -> None:
Raises:
ValueError: If username and password are not provided in the proxy URL.
"""
# Do not use proxy for robots.txt, as it causes 403 Forbidden.
Copy link
Contributor

Choose a reason for hiding this comment

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

Like... universally, everywhere? I don't mind it, it just seems weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe it is a problem of Apify proxies, I don't know, but it results in the following:

[scrapy.downloadermiddlewares.robotstxt] ERROR Error downloading <GET https://console.apify.com/robots.txt>: Could not open CONNECT tunnel with proxy proxy.apify.com:8000 [{'status': 403, 'reason': b'Forbidden'}] ({"spider": "<TitleSpider 'title_spider' at 0x7f2bc3aee660>"})
      Traceback (most recent call last):
        File "/home/vdusek/Projects/apify-sdk-python/.venv/lib/python3.13/site-packages/twisted/internet/defer.py", line 2013, in _inlineCallbacks
          result = context.run(
              cast(Failure, result).throwExceptionIntoGenerator, gen
          )
        File "/home/vdusek/Projects/apify-sdk-python/.venv/lib/python3.13/site-packages/twisted/python/failure.py", line 467, in throwExceptionIntoGenerator
          return g.throw(self.value.with_traceback(self.tb))
                 ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/vdusek/Projects/apify-sdk-python/.venv/lib/python3.13/site-packages/scrapy/core/downloader/middleware.py", line 68, in process_request
          return (yield download_func(request, spider))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      scrapy.core.downloader.handlers.http11.TunnelError: Could not open CONNECT tunnel with proxy proxy.apify.com:8000 [{'status': 403, 'reason': b'Forbidden'}]

Copy link
Contributor

Choose a reason for hiding this comment

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

Humph. But the connect call should happen way before the path part of the URL matters, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, that's strange. I'm not sure why we can't connect when it comes to robots.txt, while other URLs works. I've reverted the changes and kept only the storage client fix.

if request.url.endswith('/robots.txt'):
request.meta.pop('proxy', None)
return

Actor.log.debug(f'ApifyHttpProxyMiddleware.process_request: request={request}, spider={spider}')
url = await self._get_new_proxy_url()

Expand Down
13 changes: 11 additions & 2 deletions src/apify/scrapy/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import traceback
from typing import TYPE_CHECKING

from crawlee.storage_clients import MemoryStorageClient

from apify._configuration import Configuration
from apify.apify_storage_client import ApifyStorageClient

Expand Down Expand Up @@ -52,8 +54,15 @@ def open(self, spider: Spider) -> None: # this has to be named "open"
self.spider = spider

async def open_queue() -> RequestQueue:
custom_loop_apify_client = ApifyStorageClient(configuration=Configuration.get_global_configuration())
return await RequestQueue.open(storage_client=custom_loop_apify_client)
config = Configuration.get_global_configuration()

# Use the ApifyStorageClient if the Actor is running on the Apify platform,
# otherwise use the MemoryStorageClient.
storage_client = (
ApifyStorageClient.from_config(config) if config.is_at_home else MemoryStorageClient.from_config(config)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is supposed to happen in Actor.init, right? Why duplicate it here?

Copy link
Contributor Author

@vdusek vdusek Jan 29, 2025

Choose a reason for hiding this comment

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

Because of the nested event loop, otherwise, it will result in:

RuntimeError: <asyncio.locks.Event object at 0x7c2d640c8fc0 [unset]> is bound to a different event loop

when using Apify client.

)

return await RequestQueue.open(storage_client=storage_client)

try:
self._rq = nested_event_loop.run_until_complete(open_queue())
Expand Down