Skip to content

Commit 10e0652

Browse files
committed
Wip changes
1 parent 4ada123 commit 10e0652

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/apify/storage_clients/_apify/_storage_client.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from ._dataset_client import ApifyDatasetClient
1010
from ._key_value_store_client import ApifyKeyValueStoreClient
11+
from ._request_queue_client_full import ApifyRequestQueueClientFull
1112
from ._request_queue_client_simple import ApifyRequestQueueClientSimple
1213
from apify._utils import docs_group
1314

@@ -21,6 +22,17 @@
2122
class ApifyStorageClient(StorageClient):
2223
"""Apify storage client."""
2324

25+
def __init__(self, simple_request_queue: bool = True) -> None:
26+
"""Initialize the Apify storage client.
27+
28+
Args:
29+
simple_request_queue: If True, the `create_rq_client` will always return `ApifyRequestQueueClientSimple`,
30+
if false it will return `ApifyRequestQueueClientFull`. Simple client is suitable for single consumer
31+
scenarios and makes less API calls. Full client is suitable for multiple consumers scenarios at the
32+
cost of higher API usage
33+
"""
34+
self._simple_request_queue = simple_request_queue
35+
2436
@override
2537
async def create_dataset_client(
2638
self,
@@ -74,7 +86,10 @@ async def create_rq_client(
7486

7587
configuration = configuration or ApifyConfiguration.get_global_configuration()
7688
if isinstance(configuration, ApifyConfiguration):
77-
return await ApifyRequestQueueClientSimple.open(id=id, name=name, configuration=configuration)
89+
if not self._simple_request_queue:
90+
return await ApifyRequestQueueClientSimple.open(id=id, name=name, configuration=configuration)
91+
else:
92+
return await ApifyRequestQueueClientFull.open(id=id, name=name, configuration=configuration)
7893

7994
raise TypeError(
8095
f'Expected "configuration" to be an instance of "apify.Configuration", '

tests/integration/test_request_queue.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
from typing import TYPE_CHECKING
55

66
import pytest
7+
from apify_shared.consts import ApifyEnvVars
78

89
from crawlee import Request
910

1011
from apify import Actor
12+
from ._utils import generate_unique_resource_name
1113

1214
if TYPE_CHECKING:
1315
from apify_client import ApifyClientAsync
@@ -1068,3 +1070,31 @@ async def test_request_queue_not_had_multiple_clients(
10681070
api_response = await api_client.get()
10691071
assert api_response
10701072
assert api_response['hadMultipleClients'] is False
1073+
1074+
1075+
async def test_cache_initialization(
1076+
apify_token: str, monkeypatch: pytest.MonkeyPatch, apify_client_async: ApifyClientAsync
1077+
) -> None:
1078+
"""Test that same `RequestQueue` created from Actor does not act as multiple clients."""
1079+
1080+
"""Create an instance of the Apify request queue on the platform and drop it when the test is finished."""
1081+
request_queue_name = generate_unique_resource_name('request_queue')
1082+
monkeypatch.setenv(ApifyEnvVars.TOKEN, apify_token)
1083+
1084+
async with Actor:
1085+
rq = await Actor.open_request_queue(name=request_queue_name, force_cloud=True)
1086+
yield rq
1087+
await rq.drop()
1088+
1089+
1090+
await request_queue_force_cloud.fetch_next_request()
1091+
await request_queue_force_cloud.fetch_next_request()
1092+
1093+
# Check that it is correctly in the RequestQueueClient metadata
1094+
assert (await request_queue_force_cloud.get_metadata()).had_multiple_clients is False
1095+
1096+
# Check that it is correctly in the API
1097+
api_client = apify_client_async.request_queue(request_queue_id=request_queue_force_cloud.id)
1098+
api_response = await api_client.get()
1099+
assert api_response
1100+
assert api_response['hadMultipleClients'] is False

0 commit comments

Comments
 (0)