1212from apify_client import ApifyClientAsync
1313from apify_shared .consts import ActorEnvVars , ActorExitCodes , ApifyEnvVars
1414from apify_shared .utils import ignore_docs , maybe_extract_enum_member_value
15- from crawlee import service_container
15+ from crawlee import service_locator
1616from crawlee .events ._types import Event , EventPersistStateData
17+ from crawlee .memory_storage_client import MemoryStorageClient
1718
1819from apify ._configuration import Configuration
1920from apify ._consts import EVENT_LISTENERS_TIMEOUT
@@ -69,17 +70,31 @@ def __init__(
6970 self ._configure_logging = configure_logging
7071 self ._apify_client = self .new_client ()
7172
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 (
7580 config = self ._configuration ,
7681 persist_state_interval = self ._configuration .persist_state_interval ,
7782 )
78- else :
79- self . _event_manager = LocalEventManager (
83+ if self . is_at_home ()
84+ else LocalEventManager (
8085 system_info_interval = self ._configuration .system_info_interval ,
8186 persist_state_interval = self ._configuration .persist_state_interval ,
8287 )
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 )
8398
8499 self ._is_initialized = False
85100
@@ -93,7 +108,7 @@ async def __aenter__(self) -> Self:
93108 executing the block code, the `Actor.fail` method is called.
94109 """
95110 if self ._configure_logging :
96- _configure_logging (self . _configuration )
111+ _configure_logging ()
97112
98113 await self .init ()
99114 return self
@@ -182,16 +197,6 @@ async def init(self) -> None:
182197 if self ._is_initialized :
183198 raise RuntimeError ('The Actor was already initialized!' )
184199
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-
195200 self ._is_exiting = False
196201 self ._was_final_persist_state_emitted = False
197202
@@ -243,7 +248,6 @@ async def finalize() -> None:
243248 await self ._event_manager .wait_for_all_listeners_to_complete (timeout = event_listeners_timeout )
244249
245250 await self ._event_manager .__aexit__ (None , None , None )
246- cast (dict , service_container ._services ).clear () # noqa: SLF001
247251
248252 await asyncio .wait_for (finalize (), cleanup_timeout .total_seconds ())
249253 self ._is_initialized = False
@@ -347,11 +351,13 @@ async def open_dataset(
347351 self ._raise_if_not_initialized ()
348352 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
349353
354+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
355+
350356 return await Dataset .open (
351357 id = id ,
352358 name = name ,
353359 configuration = self ._configuration ,
354- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
360+ storage_client = storage_client ,
355361 )
356362
357363 async def open_key_value_store (
@@ -379,12 +385,13 @@ async def open_key_value_store(
379385 """
380386 self ._raise_if_not_initialized ()
381387 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 ()
382389
383390 return await KeyValueStore .open (
384391 id = id ,
385392 name = name ,
386393 configuration = self ._configuration ,
387- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
394+ storage_client = storage_client ,
388395 )
389396
390397 async def open_request_queue (
@@ -415,11 +422,13 @@ async def open_request_queue(
415422 self ._raise_if_not_initialized ()
416423 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
417424
425+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
426+
418427 return await RequestQueue .open (
419428 id = id ,
420429 name = name ,
421430 configuration = self ._configuration ,
422- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
431+ storage_client = storage_client ,
423432 )
424433
425434 async def push_data (self , data : dict | list [dict ]) -> None :
@@ -941,7 +950,7 @@ async def create_proxy_configuration(
941950 password : str | None = None ,
942951 groups : list [str ] | None = None ,
943952 country_code : str | None = None ,
944- proxy_urls : list [str ] | None = None ,
953+ proxy_urls : list [str | None ] | None = None ,
945954 new_url_function : _NewUrlFunction | None = None ,
946955 ) -> ProxyConfiguration | None :
947956 """Create a ProxyConfiguration object with the passed proxy configuration.
0 commit comments