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

fix: Make sure that Actor instances with non-default configurations are also accessible through the global Actor proxy after initialization #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions 17 src/apify/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
vdusek marked this conversation as resolved.
Show resolved Hide resolved
_is_any_instance_initialized = False

def __init__(
self,
Expand All @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions 7 tests/unit/actor/test_actor_non_default_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions 1 tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.