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 .storage_clients 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
@@ -94,9 +100,6 @@ async def __aenter__(self) -> Self:
94100 When you exit the `async with` block, the `Actor.exit()` method is called, and if any exception happens while
95101 executing the block code, the `Actor.fail` method is called.
96102 """
97- if self ._configure_logging :
98- _configure_logging (self ._configuration )
99-
100103 await self .init ()
101104 return self
102105
@@ -184,18 +187,21 @@ async def init(self) -> None:
184187 if self ._is_initialized :
185188 raise RuntimeError ('The Actor was already initialized!' )
186189
187- if self ._configuration . token :
188- service_container . set_cloud_storage_client ( ApifyStorageClient ( configuration = self ._configuration ))
190+ self ._is_exiting = False
191+ self ._was_final_persist_state_emitted = False
189192
190- if self ._configuration .is_at_home :
191- service_container .set_default_storage_client_type ('cloud' )
193+ # Register services in the service locator.
194+ if self .is_at_home ():
195+ service_locator .set_storage_client (self ._cloud_storage_client )
192196 else :
193- service_container . set_default_storage_client_type ( 'local' )
197+ service_locator . set_storage_client ( self . _local_storage_client )
194198
195- service_container .set_event_manager (self ._event_manager )
199+ service_locator .set_event_manager (self .event_manager )
200+ service_locator .set_configuration (self .configuration )
196201
197- self ._is_exiting = False
198- self ._was_final_persist_state_emitted = False
202+ # The logging configuration has to be called after all service_locator set methods.
203+ if self ._configure_logging :
204+ _configure_logging ()
199205
200206 self .log .info ('Initializing Actor...' )
201207 self .log .info ('System info' , extra = get_system_info ())
@@ -245,7 +251,6 @@ async def finalize() -> None:
245251 await self ._event_manager .wait_for_all_listeners_to_complete (timeout = event_listeners_timeout )
246252
247253 await self ._event_manager .__aexit__ (None , None , None )
248- cast (dict , service_container ._services ).clear () # noqa: SLF001
249254
250255 await asyncio .wait_for (finalize (), cleanup_timeout .total_seconds ())
251256 self ._is_initialized = False
@@ -349,11 +354,13 @@ async def open_dataset(
349354 self ._raise_if_not_initialized ()
350355 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
351356
357+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
358+
352359 return await Dataset .open (
353360 id = id ,
354361 name = name ,
355362 configuration = self ._configuration ,
356- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
363+ storage_client = storage_client ,
357364 )
358365
359366 async def open_key_value_store (
@@ -381,12 +388,13 @@ async def open_key_value_store(
381388 """
382389 self ._raise_if_not_initialized ()
383390 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
391+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
384392
385393 return await KeyValueStore .open (
386394 id = id ,
387395 name = name ,
388396 configuration = self ._configuration ,
389- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
397+ storage_client = storage_client ,
390398 )
391399
392400 async def open_request_queue (
@@ -417,11 +425,13 @@ async def open_request_queue(
417425 self ._raise_if_not_initialized ()
418426 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
419427
428+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
429+
420430 return await RequestQueue .open (
421431 id = id ,
422432 name = name ,
423433 configuration = self ._configuration ,
424- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
434+ storage_client = storage_client ,
425435 )
426436
427437 async def push_data (self , data : dict | list [dict ]) -> None :
@@ -963,7 +973,7 @@ async def create_proxy_configuration(
963973 password : str | None = None ,
964974 groups : list [str ] | None = None ,
965975 country_code : str | None = None ,
966- proxy_urls : list [str ] | None = None ,
976+ proxy_urls : list [str | None ] | None = None ,
967977 new_url_function : _NewUrlFunction | None = None ,
968978 ) -> ProxyConfiguration | None :
969979 """Create a ProxyConfiguration object with the passed proxy configuration.
0 commit comments