Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b956a02

Browse filesBrowse files
authored
fix: Make sure that Actor instances with non-default configurations are also accessible through the global Actor proxy after initialization (#402)
- closes #397
1 parent 9706c94 commit b956a02
Copy full SHA for b956a02

File tree

Expand file treeCollapse file tree

3 files changed

+20
-5
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+20
-5
lines changed

‎src/apify/_actor.py

Copy file name to clipboardExpand all lines: src/apify/_actor.py
+12-5Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@
5555
class _ActorType:
5656
"""The class of `Actor`. Only make a new instance if you're absolutely sure you need to."""
5757

58-
_apify_client: ApifyClientAsync
59-
_configuration: Configuration
60-
_is_exiting = False
6158
_is_rebooting = False
59+
_is_any_instance_initialized = False
6260

6361
def __init__(
6462
self,
@@ -76,6 +74,8 @@ def __init__(
7674
be created.
7775
configure_logging: Should the default logging configuration be configured?
7876
"""
77+
self._is_exiting = False
78+
7979
self._configuration = configuration or Configuration.get_global_configuration()
8080
self._configure_logging = configure_logging
8181
self._apify_client = self.new_client()
@@ -200,6 +200,12 @@ async def init(self) -> None:
200200
if self._is_initialized:
201201
raise RuntimeError('The Actor was already initialized!')
202202

203+
if _ActorType._is_any_instance_initialized:
204+
self.log.warning('Repeated Actor initialization detected - this is non-standard usage, proceed with care')
205+
206+
# Make sure that the currently initialized instance is also available through the global `Actor` proxy
207+
cast(Proxy, Actor).__wrapped__ = self
208+
203209
self._is_exiting = False
204210
self._was_final_persist_state_emitted = False
205211

@@ -223,6 +229,7 @@ async def init(self) -> None:
223229
await self._event_manager.__aenter__()
224230

225231
self._is_initialized = True
232+
_ActorType._is_any_instance_initialized = True
226233

227234
async def exit(
228235
self,
@@ -898,11 +905,11 @@ async def reboot(
898905
self.log.error('Actor.reboot() is only supported when running on the Apify platform.')
899906
return
900907

901-
if self._is_rebooting:
908+
if _ActorType._is_rebooting:
902909
self.log.debug('Actor is already rebooting, skipping the additional reboot call.')
903910
return
904911

905-
self._is_rebooting = True
912+
_ActorType._is_rebooting = True
906913

907914
if not custom_after_sleep:
908915
custom_after_sleep = self._configuration.metamorph_after_sleep

‎tests/unit/actor/test_actor_non_default_instance.py

Copy file name to clipboardExpand all lines: tests/unit/actor/test_actor_non_default_instance.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ async def test_actor_with_non_default_config() -> None:
1010

1111
async with Actor(config) as actor:
1212
assert actor.config.internal_timeout == timedelta(minutes=111)
13+
14+
15+
async def test_actor_global_works() -> None:
16+
non_default_configuration = Configuration(actor_full_name='Actor McActorson, esq.')
17+
18+
async with Actor(configuration=non_default_configuration):
19+
assert Actor.configuration is non_default_configuration

‎tests/unit/conftest.py

Copy file name to clipboardExpand all lines: tests/unit/conftest.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callabl
3939

4040
def _prepare_test_env() -> None:
4141
delattr(apify._actor.Actor, '__wrapped__')
42+
apify._actor._ActorType._is_any_instance_initialized = False
4243

4344
# Set the environment variable for the local storage directory to the temporary path.
4445
monkeypatch.setenv(ApifyEnvVars.LOCAL_STORAGE_DIR, str(tmp_path))

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.