Skip to content

Commit b956a02

Browse 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

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/apify/_actor.py

Lines 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

Lines 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

Lines 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)