12
12
from apify_client import ApifyClientAsync
13
13
from apify_shared .consts import ActorEnvVars , ActorExitCodes , ApifyEnvVars
14
14
from apify_shared .utils import ignore_docs , maybe_extract_enum_member_value
15
- from crawlee import service_container
15
+ from crawlee import service_locator
16
16
from crawlee .events ._types import Event , EventPersistStateData
17
+ from crawlee .memory_storage_client import MemoryStorageClient
17
18
18
19
from apify ._configuration import Configuration
19
20
from apify ._consts import EVENT_LISTENERS_TIMEOUT
@@ -69,17 +70,31 @@ def __init__(
69
70
self ._configure_logging = configure_logging
70
71
self ._apify_client = self .new_client ()
71
72
72
- self ._event_manager : EventManager
73
- if self ._configuration .is_at_home :
74
- self ._event_manager = PlatformEventManager (
73
+ # We need to keep both local & cloud storage clients because of the `force_cloud` option.
74
+ self ._local_storage_client = MemoryStorageClient .from_config ()
75
+ self ._cloud_storage_client = ApifyStorageClient (configuration = self ._configuration )
76
+
77
+ # Set the event manager based on whether the Actor is running on the platform or locally.
78
+ self ._event_manager = (
79
+ PlatformEventManager (
75
80
config = self ._configuration ,
76
81
persist_state_interval = self ._configuration .persist_state_interval ,
77
82
)
78
- else :
79
- self . _event_manager = LocalEventManager (
83
+ if self . is_at_home ()
84
+ else LocalEventManager (
80
85
system_info_interval = self ._configuration .system_info_interval ,
81
86
persist_state_interval = self ._configuration .persist_state_interval ,
82
87
)
88
+ )
89
+
90
+ # Register services in the service locator.
91
+ if self .is_at_home ():
92
+ service_locator .set_storage_client (self ._cloud_storage_client )
93
+ else :
94
+ service_locator .set_storage_client (self ._local_storage_client )
95
+
96
+ service_locator .set_event_manager (self .event_manager )
97
+ service_locator .set_configuration (self .configuration )
83
98
84
99
self ._is_initialized = False
85
100
@@ -93,7 +108,7 @@ async def __aenter__(self) -> Self:
93
108
executing the block code, the `Actor.fail` method is called.
94
109
"""
95
110
if self ._configure_logging :
96
- _configure_logging (self . _configuration )
111
+ _configure_logging ()
97
112
98
113
await self .init ()
99
114
return self
@@ -182,16 +197,6 @@ async def init(self) -> None:
182
197
if self ._is_initialized :
183
198
raise RuntimeError ('The Actor was already initialized!' )
184
199
185
- if self ._configuration .token :
186
- service_container .set_cloud_storage_client (ApifyStorageClient (configuration = self ._configuration ))
187
-
188
- if self ._configuration .is_at_home :
189
- service_container .set_default_storage_client_type ('cloud' )
190
- else :
191
- service_container .set_default_storage_client_type ('local' )
192
-
193
- service_container .set_event_manager (self ._event_manager )
194
-
195
200
self ._is_exiting = False
196
201
self ._was_final_persist_state_emitted = False
197
202
@@ -243,7 +248,6 @@ async def finalize() -> None:
243
248
await self ._event_manager .wait_for_all_listeners_to_complete (timeout = event_listeners_timeout )
244
249
245
250
await self ._event_manager .__aexit__ (None , None , None )
246
- cast (dict , service_container ._services ).clear () # noqa: SLF001
247
251
248
252
await asyncio .wait_for (finalize (), cleanup_timeout .total_seconds ())
249
253
self ._is_initialized = False
@@ -347,11 +351,13 @@ async def open_dataset(
347
351
self ._raise_if_not_initialized ()
348
352
self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
349
353
354
+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
355
+
350
356
return await Dataset .open (
351
357
id = id ,
352
358
name = name ,
353
359
configuration = self ._configuration ,
354
- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
360
+ storage_client = storage_client ,
355
361
)
356
362
357
363
async def open_key_value_store (
@@ -379,12 +385,13 @@ async def open_key_value_store(
379
385
"""
380
386
self ._raise_if_not_initialized ()
381
387
self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
388
+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
382
389
383
390
return await KeyValueStore .open (
384
391
id = id ,
385
392
name = name ,
386
393
configuration = self ._configuration ,
387
- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
394
+ storage_client = storage_client ,
388
395
)
389
396
390
397
async def open_request_queue (
@@ -415,11 +422,13 @@ async def open_request_queue(
415
422
self ._raise_if_not_initialized ()
416
423
self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
417
424
425
+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
426
+
418
427
return await RequestQueue .open (
419
428
id = id ,
420
429
name = name ,
421
430
configuration = self ._configuration ,
422
- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
431
+ storage_client = storage_client ,
423
432
)
424
433
425
434
async def push_data (self , data : dict | list [dict ]) -> None :
@@ -941,7 +950,7 @@ async def create_proxy_configuration(
941
950
password : str | None = None ,
942
951
groups : list [str ] | None = None ,
943
952
country_code : str | None = None ,
944
- proxy_urls : list [str ] | None = None ,
953
+ proxy_urls : list [str | None ] | None = None ,
945
954
new_url_function : _NewUrlFunction | None = None ,
946
955
) -> ProxyConfiguration | None :
947
956
"""Create a ProxyConfiguration object with the passed proxy configuration.
0 commit comments