diff --git a/src/apify/_actor.py b/src/apify/_actor.py index d675f1bd..8c6969b0 100644 --- a/src/apify/_actor.py +++ b/src/apify/_actor.py @@ -55,10 +55,8 @@ class _ActorType: """The class of `Actor`. Only make a new instance if you're absolutely sure you need to.""" - _apify_client: ApifyClientAsync - _configuration: Configuration - _is_exiting = False _is_rebooting = False + _is_any_instance_initialized = False def __init__( self, @@ -76,6 +74,8 @@ def __init__( be created. configure_logging: Should the default logging configuration be configured? """ + self._is_exiting = False + self._configuration = configuration or Configuration.get_global_configuration() self._configure_logging = configure_logging self._apify_client = self.new_client() @@ -200,6 +200,12 @@ async def init(self) -> None: if self._is_initialized: raise RuntimeError('The Actor was already initialized!') + if _ActorType._is_any_instance_initialized: + self.log.warning('Repeated Actor initialization detected - this is non-standard usage, proceed with care') + + # Make sure that the currently initialized instance is also available through the global `Actor` proxy + cast(Proxy, Actor).__wrapped__ = self + self._is_exiting = False self._was_final_persist_state_emitted = False @@ -223,6 +229,7 @@ async def init(self) -> None: await self._event_manager.__aenter__() self._is_initialized = True + _ActorType._is_any_instance_initialized = True async def exit( self, @@ -898,11 +905,11 @@ async def reboot( self.log.error('Actor.reboot() is only supported when running on the Apify platform.') return - if self._is_rebooting: + if _ActorType._is_rebooting: self.log.debug('Actor is already rebooting, skipping the additional reboot call.') return - self._is_rebooting = True + _ActorType._is_rebooting = True if not custom_after_sleep: custom_after_sleep = self._configuration.metamorph_after_sleep diff --git a/tests/unit/actor/test_actor_non_default_instance.py b/tests/unit/actor/test_actor_non_default_instance.py index 6a51be23..3b3b5ccd 100644 --- a/tests/unit/actor/test_actor_non_default_instance.py +++ b/tests/unit/actor/test_actor_non_default_instance.py @@ -10,3 +10,10 @@ async def test_actor_with_non_default_config() -> None: async with Actor(config) as actor: assert actor.config.internal_timeout == timedelta(minutes=111) + + +async def test_actor_global_works() -> None: + non_default_configuration = Configuration(actor_full_name='Actor McActorson, esq.') + + async with Actor(configuration=non_default_configuration): + assert Actor.configuration is non_default_configuration diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 87a4dfb3..6f336cd6 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -39,6 +39,7 @@ def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callabl def _prepare_test_env() -> None: delattr(apify._actor.Actor, '__wrapped__') + apify._actor._ActorType._is_any_instance_initialized = False # Set the environment variable for the local storage directory to the temporary path. monkeypatch.setenv(ApifyEnvVars.LOCAL_STORAGE_DIR, str(tmp_path))