Skip to content

Commit b511011

Browse files
committed
Finalize change and add few more tests
1 parent ce090c0 commit b511011

File tree

5 files changed

+153
-89
lines changed

5 files changed

+153
-89
lines changed

docs/04_upgrading/upgrading_to_v3.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,15 @@ async def main():
5858
- The default client for working with Apify platform based `RequestQueue` is now optimized and simplified client which has significantly lower amount of API calls, but does not support multiple consumers working on the same queue. It is cheaper and faster and is suitable for the majority of the use cases.
5959
- The full client is still available, but it has to be explicitly requested via `simple_request_queue=False` argument when using the `ApifyStorageClient`.
6060

61-
**Before (v2.x):**
62-
63-
```python
64-
from apify import Actor
65-
66-
async def main():
67-
async with Actor:
68-
...
69-
```
70-
7161
**Now (v3.0):**
7262

7363
```python
74-
from apify import Actor
75-
from crawlee import service_locator
64+
from apify.storages import RequestQueue
7665
from apify.storage_clients import ApifyStorageClient
7766

7867
async def main():
79-
# Use the full-featured RequestQueue client only if you really need it.
80-
service_locator.set_storage_client(ApifyStorageClient(simple_request_queue=False))
81-
async with Actor:
82-
...
68+
# Full client
69+
rq_full = await RequestQueue.open(storage_client=ApifyStorageClient(simple_request_queue=False))
70+
# Default optimized client
71+
rq_simple = await RequestQueue.open(storage_client=ApifyStorageClient())
8372
```

src/apify/storage_clients/_apify/_storage_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ._request_queue_client_full import ApifyRequestQueueClientFull
1212
from ._request_queue_client_simple import ApifyRequestQueueClientSimple
1313
from ._utils import hash_api_base_url_and_token
14-
from apify import Configuration as ApifyConfiguration
14+
from apify._configuration import Configuration as ApifyConfiguration
1515
from apify._utils import docs_group
1616

1717
if TYPE_CHECKING:
@@ -45,6 +45,8 @@ def __init__(self, *, simple_request_queue: bool = True) -> None:
4545
@override
4646
def get_additional_cache_key(self, configuration: CrawleeConfiguration) -> Hashable:
4747
if isinstance(configuration, ApifyConfiguration):
48+
# Current design does not support opening exactly same queue with full and simple client at the same time,
49+
# due to default and unnamed storages. Whichever client variation gets used first, wins.
4850
return hash_api_base_url_and_token(configuration)
4951

5052
config_class = type(configuration)
@@ -94,7 +96,7 @@ async def create_rq_client(
9496
configuration = configuration or ApifyConfiguration.get_global_configuration()
9597
if isinstance(configuration, ApifyConfiguration):
9698
client: type[ApifyRequestQueueClient] = (
97-
ApifyRequestQueueClientSimple if (self._simple_request_queue) else ApifyRequestQueueClientFull
99+
ApifyRequestQueueClientSimple if self._simple_request_queue else ApifyRequestQueueClientFull
98100
)
99101
return await client.open(id=id, name=name, alias=alias, configuration=configuration)
100102

tests/integration/conftest.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
from ._utils import generate_unique_resource_name
2121
from apify import Actor
2222
from apify._models import ActorRun
23+
from apify.storage_clients import ApifyStorageClient
2324
from apify.storage_clients._apify._utils import AliasResolver
25+
from apify.storages import RequestQueue
2426

2527
if TYPE_CHECKING:
2628
from collections.abc import AsyncGenerator, Awaitable, Callable, Coroutine, Iterator, Mapping
2729
from decimal import Decimal
2830

2931
from apify_client.clients.resource_clients import ActorClientAsync
30-
from crawlee.storages import RequestQueue
3132

3233
_TOKEN_ENV_VAR = 'APIFY_TEST_USER_API_TOKEN'
3334
_API_URL_ENV_VAR = 'APIFY_INTEGRATION_TESTS_API_URL'
@@ -50,6 +51,9 @@ def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callabl
5051
"""
5152

5253
def _prepare_test_env() -> None:
54+
# Reset the Actor class state.
55+
apify._actor.Actor.__wrapped__.__class__._is_any_instance_initialized = False # type: ignore[attr-defined]
56+
apify._actor.Actor.__wrapped__.__class__._is_rebooting = False # type: ignore[attr-defined]
5357
delattr(apify._actor.Actor, '__wrapped__')
5458

5559
# Set the environment variable for the local storage directory to the temporary path.
@@ -103,14 +107,15 @@ def apify_client_async(apify_token: str) -> ApifyClientAsync:
103107
return ApifyClientAsync(apify_token, api_url=api_url)
104108

105109

106-
@pytest.fixture
107-
async def request_queue_force_cloud(apify_token: str, monkeypatch: pytest.MonkeyPatch) -> AsyncGenerator[RequestQueue]:
110+
@pytest.fixture(params=[False, True])
111+
async def default_request_queue_apify(
112+
apify_token: str, monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest
113+
) -> AsyncGenerator[RequestQueue]:
108114
"""Create an instance of the Apify request queue on the platform and drop it when the test is finished."""
109-
request_queue_name = generate_unique_resource_name('request_queue')
110115
monkeypatch.setenv(ApifyEnvVars.TOKEN, apify_token)
111116

112117
async with Actor:
113-
rq = await Actor.open_request_queue(name=request_queue_name, force_cloud=True)
118+
rq = await RequestQueue.open(storage_client=ApifyStorageClient(simple_request_queue=request.param))
114119
yield rq
115120
await rq.drop()
116121

tests/integration/test_actor_request_queue.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,29 @@
1212
from ._utils import generate_unique_resource_name
1313
from apify import Actor, Request
1414
from apify._models import ActorRun
15+
from apify.storage_clients import ApifyStorageClient
16+
from apify.storages import RequestQueue
1517

1618
if TYPE_CHECKING:
1719
from collections.abc import AsyncGenerator
1820

1921
from apify_client import ApifyClientAsync
20-
from crawlee.storages import RequestQueue
2122

2223
from .conftest import MakeActorFunction, RunActorFunction
2324

2425

25-
@pytest.fixture
26+
@pytest.fixture(params=[False, True])
2627
async def apify_named_rq(
27-
apify_client_async: ApifyClientAsync, monkeypatch: pytest.MonkeyPatch
28+
apify_client_async: ApifyClientAsync, monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest
2829
) -> AsyncGenerator[RequestQueue]:
2930
assert apify_client_async.token
3031
monkeypatch.setenv(ApifyEnvVars.TOKEN, apify_client_async.token)
3132
request_queue_name = generate_unique_resource_name('request_queue')
3233

3334
async with Actor:
34-
request_queue = await Actor.open_request_queue(name=request_queue_name, force_cloud=True)
35+
request_queue = await RequestQueue.open(
36+
name=request_queue_name, storage_client=ApifyStorageClient(simple_request_queue=request.param)
37+
)
3538
yield request_queue
3639
await request_queue.drop()
3740

0 commit comments

Comments
 (0)