1313from apify_client import ApifyClientAsync
1414from apify_shared .consts import ActorEnvVars , ActorExitCodes , ApifyEnvVars
1515from apify_shared .utils import ignore_docs , maybe_extract_enum_member_value
16- from crawlee import service_container
16+ from crawlee import service_locator
1717from crawlee .events ._types import Event , EventMigratingData , EventPersistStateData
18+ from crawlee .memory_storage_client import MemoryStorageClient
1819
1920from apify ._configuration import Configuration
2021from apify ._consts import EVENT_LISTENERS_TIMEOUT
@@ -71,17 +72,22 @@ def __init__(
7172 self ._configure_logging = configure_logging
7273 self ._apify_client = self .new_client ()
7374
74- self ._event_manager : EventManager
75- if self ._configuration .is_at_home :
76- self ._event_manager = PlatformEventManager (
77- config = self ._configuration ,
78- persist_state_interval = self ._configuration .persist_state_interval ,
75+ # We need to keep both local & cloud storage clients because of the `force_cloud` option.
76+ self ._local_storage_client = MemoryStorageClient .from_config (config = self .config )
77+ self ._cloud_storage_client = ApifyStorageClient .from_config (config = self .config )
78+
79+ # Set the event manager based on whether the Actor is running on the platform or locally.
80+ self ._event_manager = (
81+ PlatformEventManager (
82+ config = self .config ,
83+ persist_state_interval = self .config .persist_state_interval ,
7984 )
80- else :
81- self . _event_manager = LocalEventManager (
82- system_info_interval = self ._configuration .system_info_interval ,
83- persist_state_interval = self ._configuration .persist_state_interval ,
85+ if self . is_at_home ()
86+ else LocalEventManager (
87+ system_info_interval = self .config .system_info_interval ,
88+ persist_state_interval = self .config .persist_state_interval ,
8489 )
90+ )
8591
8692 self ._is_initialized = False
8793
@@ -95,7 +101,7 @@ async def __aenter__(self) -> Self:
95101 executing the block code, the `Actor.fail` method is called.
96102 """
97103 if self ._configure_logging :
98- _configure_logging (self . _configuration )
104+ _configure_logging ()
99105
100106 await self .init ()
101107 return self
@@ -184,18 +190,17 @@ async def init(self) -> None:
184190 if self ._is_initialized :
185191 raise RuntimeError ('The Actor was already initialized!' )
186192
187- if self ._configuration . token :
188- service_container . set_cloud_storage_client ( ApifyStorageClient ( configuration = self ._configuration ))
193+ self ._is_exiting = False
194+ self ._was_final_persist_state_emitted = False
189195
190- if self ._configuration .is_at_home :
191- service_container .set_default_storage_client_type ('cloud' )
196+ # Register services in the service locator.
197+ if self .is_at_home ():
198+ service_locator .set_storage_client (self ._cloud_storage_client )
192199 else :
193- service_container . set_default_storage_client_type ( 'local' )
200+ service_locator . set_storage_client ( self . _local_storage_client )
194201
195- service_container .set_event_manager (self ._event_manager )
196-
197- self ._is_exiting = False
198- self ._was_final_persist_state_emitted = False
202+ service_locator .set_event_manager (self .event_manager )
203+ service_locator .set_configuration (self .configuration )
199204
200205 self .log .info ('Initializing Actor...' )
201206 self .log .info ('System info' , extra = get_system_info ())
@@ -245,7 +250,6 @@ async def finalize() -> None:
245250 await self ._event_manager .wait_for_all_listeners_to_complete (timeout = event_listeners_timeout )
246251
247252 await self ._event_manager .__aexit__ (None , None , None )
248- cast (dict , service_container ._services ).clear () # noqa: SLF001
249253
250254 await asyncio .wait_for (finalize (), cleanup_timeout .total_seconds ())
251255 self ._is_initialized = False
@@ -349,11 +353,13 @@ async def open_dataset(
349353 self ._raise_if_not_initialized ()
350354 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
351355
356+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
357+
352358 return await Dataset .open (
353359 id = id ,
354360 name = name ,
355361 configuration = self ._configuration ,
356- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
362+ storage_client = storage_client ,
357363 )
358364
359365 async def open_key_value_store (
@@ -381,12 +387,13 @@ async def open_key_value_store(
381387 """
382388 self ._raise_if_not_initialized ()
383389 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
390+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
384391
385392 return await KeyValueStore .open (
386393 id = id ,
387394 name = name ,
388395 configuration = self ._configuration ,
389- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
396+ storage_client = storage_client ,
390397 )
391398
392399 async def open_request_queue (
@@ -417,11 +424,13 @@ async def open_request_queue(
417424 self ._raise_if_not_initialized ()
418425 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
419426
427+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
428+
420429 return await RequestQueue .open (
421430 id = id ,
422431 name = name ,
423432 configuration = self ._configuration ,
424- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
433+ storage_client = storage_client ,
425434 )
426435
427436 async def push_data (self , data : dict | list [dict ]) -> None :
@@ -963,7 +972,7 @@ async def create_proxy_configuration(
963972 password : str | None = None ,
964973 groups : list [str ] | None = None ,
965974 country_code : str | None = None ,
966- proxy_urls : list [str ] | None = None ,
975+ proxy_urls : list [str | None ] | None = None ,
967976 new_url_function : _NewUrlFunction | None = None ,
968977 ) -> ProxyConfiguration | None :
969978 """Create a ProxyConfiguration object with the passed proxy configuration.
0 commit comments