13
13
from apify_client import ApifyClientAsync
14
14
from apify_shared .consts import ActorEnvVars , ActorExitCodes , ApifyEnvVars
15
15
from apify_shared .utils import ignore_docs , maybe_extract_enum_member_value
16
- from crawlee import service_container
16
+ from crawlee import service_locator
17
17
from crawlee .events ._types import Event , EventMigratingData , EventPersistStateData
18
+ from crawlee .memory_storage_client import MemoryStorageClient
18
19
19
20
from apify ._configuration import Configuration
20
21
from apify ._consts import EVENT_LISTENERS_TIMEOUT
@@ -71,17 +72,22 @@ def __init__(
71
72
self ._configure_logging = configure_logging
72
73
self ._apify_client = self .new_client ()
73
74
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 ,
79
84
)
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 ,
84
89
)
90
+ )
85
91
86
92
self ._is_initialized = False
87
93
@@ -95,7 +101,7 @@ async def __aenter__(self) -> Self:
95
101
executing the block code, the `Actor.fail` method is called.
96
102
"""
97
103
if self ._configure_logging :
98
- _configure_logging (self . _configuration )
104
+ _configure_logging ()
99
105
100
106
await self .init ()
101
107
return self
@@ -184,18 +190,17 @@ async def init(self) -> None:
184
190
if self ._is_initialized :
185
191
raise RuntimeError ('The Actor was already initialized!' )
186
192
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
189
195
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 )
192
199
else :
193
- service_container . set_default_storage_client_type ( 'local' )
200
+ service_locator . set_storage_client ( self . _local_storage_client )
194
201
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 )
199
204
200
205
self .log .info ('Initializing Actor...' )
201
206
self .log .info ('System info' , extra = get_system_info ())
@@ -245,7 +250,6 @@ async def finalize() -> None:
245
250
await self ._event_manager .wait_for_all_listeners_to_complete (timeout = event_listeners_timeout )
246
251
247
252
await self ._event_manager .__aexit__ (None , None , None )
248
- cast (dict , service_container ._services ).clear () # noqa: SLF001
249
253
250
254
await asyncio .wait_for (finalize (), cleanup_timeout .total_seconds ())
251
255
self ._is_initialized = False
@@ -349,11 +353,13 @@ async def open_dataset(
349
353
self ._raise_if_not_initialized ()
350
354
self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
351
355
356
+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
357
+
352
358
return await Dataset .open (
353
359
id = id ,
354
360
name = name ,
355
361
configuration = self ._configuration ,
356
- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
362
+ storage_client = storage_client ,
357
363
)
358
364
359
365
async def open_key_value_store (
@@ -381,12 +387,13 @@ async def open_key_value_store(
381
387
"""
382
388
self ._raise_if_not_initialized ()
383
389
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 ()
384
391
385
392
return await KeyValueStore .open (
386
393
id = id ,
387
394
name = name ,
388
395
configuration = self ._configuration ,
389
- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
396
+ storage_client = storage_client ,
390
397
)
391
398
392
399
async def open_request_queue (
@@ -417,11 +424,13 @@ async def open_request_queue(
417
424
self ._raise_if_not_initialized ()
418
425
self ._raise_if_cloud_requested_but_not_configured (force_cloud = force_cloud )
419
426
427
+ storage_client = self ._cloud_storage_client if force_cloud else service_locator .get_storage_client ()
428
+
420
429
return await RequestQueue .open (
421
430
id = id ,
422
431
name = name ,
423
432
configuration = self ._configuration ,
424
- storage_client = service_container . get_storage_client ( client_type = 'cloud' if force_cloud else None ) ,
433
+ storage_client = storage_client ,
425
434
)
426
435
427
436
async def push_data (self , data : dict | list [dict ]) -> None :
@@ -963,7 +972,7 @@ async def create_proxy_configuration(
963
972
password : str | None = None ,
964
973
groups : list [str ] | None = None ,
965
974
country_code : str | None = None ,
966
- proxy_urls : list [str ] | None = None ,
975
+ proxy_urls : list [str | None ] | None = None ,
967
976
new_url_function : _NewUrlFunction | None = None ,
968
977
) -> ProxyConfiguration | None :
969
978
"""Create a ProxyConfiguration object with the passed proxy configuration.
0 commit comments