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,22 @@ 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 (
75- config = self ._configuration ,
76- persist_state_interval = self ._configuration .persist_state_interval ,
73+ # We need to keep both local & cloud storage clients because of the `force_cloud` option.
74+ self ._local_storage_client = MemoryStorageClient .from_config (config = self .config )
75+ self ._cloud_storage_client = ApifyStorageClient .from_config (config = self .config )
76+
77+ # Set the event manager based on whether the Actor is running on the platform or locally.
78+ self ._event_manager = (
79+ PlatformEventManager (
80+ config = self .config ,
81+ persist_state_interval = self .config .persist_state_interval ,
7782 )
78- else :
79- self . _event_manager = LocalEventManager (
80- system_info_interval = self ._configuration .system_info_interval ,
81- persist_state_interval = self ._configuration .persist_state_interval ,
83+ if self . is_at_home ()
84+ else LocalEventManager (
85+ system_info_interval = self .config .system_info_interval ,
86+ persist_state_interval = self .config .persist_state_interval ,
8287 )
88+ )
8389
8490 self ._is_initialized = False
8591
@@ -93,7 +99,7 @@ async def __aenter__(self) -> Self:
9399 executing the block code, the `Actor.fail` method is called.
94100 """
95101 if self ._configure_logging :
96- _configure_logging (self . _configuration )
102+ _configure_logging ()
97103
98104 await self .init ()
99105 return self
@@ -182,18 +188,17 @@ async def init(self) -> None:
182188 if self ._is_initialized :
183189 raise RuntimeError ('The Actor was already initialized!' )
184190
185- if self ._configuration . token :
186- service_container . set_cloud_storage_client ( ApifyStorageClient ( configuration = self ._configuration ))
191+ self ._is_exiting = False
192+ self ._was_final_persist_state_emitted = False
187193
188- if self ._configuration .is_at_home :
189- service_container .set_default_storage_client_type ('cloud' )
194+ # Register services in the service locator.
195+ if self .is_at_home ():
196+ service_locator .set_storage_client (self ._cloud_storage_client )
190197 else :
191- service_container . set_default_storage_client_type ( 'local' )
198+ service_locator . set_storage_client ( self . _local_storage_client )
192199
193- service_container .set_event_manager (self ._event_manager )
194-
195- self ._is_exiting = False
196- self ._was_final_persist_state_emitted = False
200+ service_locator .set_event_manager (self .event_manager )
201+ service_locator .set_configuration (self .configuration )
197202
198203 self .log .info ('Initializing Actor...' )
199204 self .log .info ('System info' , extra = get_system_info ())
@@ -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