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
1818
1919from apify ._configuration import Configuration
3434 from typing_extensions import Self
3535
3636 from crawlee .proxy_configuration import _NewUrlFunction
37+ from crawlee .storage_clients import BaseStorageClient
3738
3839 from apify ._models import Webhook
3940
@@ -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 (
75+ # Create an instance of the cloud storage client, the local storage client is obtained
76+ # from the service locator.
77+ self ._cloud_storage_client = ApifyStorageClient .from_config (config = self ._configuration )
78+
79+ # Set the event manager based on whether the Actor is running on the platform or locally.
80+ self ._event_manager = (
81+ PlatformEventManager (
7782 config = self ._configuration ,
7883 persist_state_interval = self ._configuration .persist_state_interval ,
7984 )
80- else :
81- self . _event_manager = LocalEventManager (
85+ if self . is_at_home ()
86+ else LocalEventManager (
8287 system_info_interval = self ._configuration .system_info_interval ,
8388 persist_state_interval = self ._configuration .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
@@ -156,6 +159,11 @@ def log(self) -> logging.Logger:
156159 """The logging.Logger instance the Actor uses."""
157160 return logger
158161
162+ @property
163+ def _local_storage_client (self ) -> BaseStorageClient :
164+ """The local storage client the Actor instance uses."""
165+ return service_locator .get_storage_client ()
166+
159167 def _raise_if_not_initialized (self ) -> None :
160168 if not self ._is_initialized :
161169 raise RuntimeError ('The Actor was not initialized!' )
@@ -184,18 +192,19 @@ async def init(self) -> None:
184192 if self ._is_initialized :
185193 raise RuntimeError ('The Actor was already initialized!' )
186194
187- if self ._configuration . token :
188- service_container . set_cloud_storage_client ( ApifyStorageClient ( configuration = self ._configuration ))
195+ self ._is_exiting = False
196+ self ._was_final_persist_state_emitted = False
189197
190- if self ._configuration .is_at_home :
191- service_container .set_default_storage_client_type ('cloud' )
192- else :
193- service_container .set_default_storage_client_type ('local' )
198+ # If the Actor is running on the Apify platform, we set the cloud storage client.
199+ if self .is_at_home ():
200+ service_locator .set_storage_client (self ._cloud_storage_client )
194201
195- service_container .set_event_manager (self ._event_manager )
202+ service_locator .set_event_manager (self .event_manager )
203+ service_locator .set_configuration (self .configuration )
196204
197- self ._is_exiting = False
198- self ._was_final_persist_state_emitted = False
205+ # The logging configuration has to be called after all service_locator set methods.
206+ if self ._configure_logging :
207+ _configure_logging ()
199208
200209 self .log .info ('Initializing Actor...' )
201210 self .log .info ('System info' , extra = get_system_info ())
@@ -245,7 +254,6 @@ async def finalize() -> None:
245254 await self ._event_manager .wait_for_all_listeners_to_complete (timeout = event_listeners_timeout )
246255
247256 await self ._event_manager .__aexit__ (None , None , None )
248- cast (dict , service_container ._services ).clear () # noqa: SLF001
249257
250258 await asyncio .wait_for (finalize (), cleanup_timeout .total_seconds ())
251259 self ._is_initialized = False
@@ -349,11 +357,13 @@ async def open_dataset(
349357 self ._raise_if_not_initialized ()
350358 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
351359
360+ storage_client = self ._cloud_storage_client if force_cloud else self ._local_storage_client
361+
352362 return await Dataset .open (
353363 id = id ,
354364 name = name ,
355365 configuration = self ._configuration ,
356- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
366+ storage_client = storage_client ,
357367 )
358368
359369 async def open_key_value_store (
@@ -381,12 +391,13 @@ async def open_key_value_store(
381391 """
382392 self ._raise_if_not_initialized ()
383393 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
394+ storage_client = self ._cloud_storage_client if force_cloud else self ._local_storage_client
384395
385396 return await KeyValueStore .open (
386397 id = id ,
387398 name = name ,
388399 configuration = self ._configuration ,
389- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
400+ storage_client = storage_client ,
390401 )
391402
392403 async def open_request_queue (
@@ -417,11 +428,13 @@ async def open_request_queue(
417428 self ._raise_if_not_initialized ()
418429 self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
419430
431+ storage_client = self ._cloud_storage_client if force_cloud else self ._local_storage_client
432+
420433 return await RequestQueue .open (
421434 id = id ,
422435 name = name ,
423436 configuration = self ._configuration ,
424- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
437+ storage_client = storage_client ,
425438 )
426439
427440 async def push_data (self , data : dict | list [dict ]) -> None :
@@ -963,7 +976,7 @@ async def create_proxy_configuration(
963976 password : str | None = None ,
964977 groups : list [str ] | None = None ,
965978 country_code : str | None = None ,
966- proxy_urls : list [str ] | None = None ,
979+ proxy_urls : list [str | None ] | None = None ,
967980 new_url_function : _NewUrlFunction | None = None ,
968981 ) -> ProxyConfiguration | None :
969982 """Create a ProxyConfiguration object with the passed proxy configuration.
0 commit comments