Skip to content
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

WIP: Do not fork actor execution with snactor fixture #408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 22 additions & 14 deletions leapp/repository/actor_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ActorCallContext(object):
"""
Wraps the actor execution into child process.
"""
def __init__(self, definition, logger, messaging):
def __init__(self, definition, logger, messaging, within_pytest=False):
"""
:param definition: Actor definition
:type definition: :py:class:`leapp.repository.actor_definition.ActorDefinition`
Expand All @@ -45,6 +45,7 @@ def __init__(self, definition, logger, messaging):
self.definition = definition
self.logger = logger
self.messaging = messaging
self.within_pytest = within_pytest

@staticmethod
def _do_run(stdin, logger, messaging, definition, args, kwargs):
Expand All @@ -53,6 +54,10 @@ def _do_run(stdin, logger, messaging, definition, args, kwargs):
sys.stdin = os.fdopen(stdin)
except OSError:
pass
self._do_run_impl(logger, messaging, definition, args, kwargs)

@staticmethod
def _do_run_impl(logger, messaging, definition, args, kwargs):
definition.load()
with definition.injected_context():
target_actor = [actor for actor in get_actors() if actor.name == definition.name][0]
Expand All @@ -62,17 +67,20 @@ def run(self, *args, **kwargs):
"""
Performs the actor execution in the child process.
"""
try:
stdin = sys.stdin.fileno()
except UnsupportedOperation:
stdin = None
p = Process(target=self._do_run, args=(stdin, self.logger, self.messaging, self.definition, args, kwargs))
p.start()
p.join()
if p.exitcode != 0:
raise LeappRuntimeError(
'Actor {actorname} unexpectedly terminated with exit code: {exitcode}'
.format(actorname=self.definition.name, exitcode=p.exitcode))
if self.within_pytest:
self._do_run_impl(self.logger, self.messaging, self.definition, args, kwargs)
else:
try:
stdin = sys.stdin.fileno()
except UnsupportedOperation:
stdin = None
p = Process(target=self._do_run, args=(stdin, self.logger, self.messaging, self.definition, args, kwargs))
p.start()
p.join()
if p.exitcode != 0:
raise LeappRuntimeError(
'Actor {actorname} unexpectedly terminated with exit code: {exitcode}'
.format(actorname=self.definition.name, exitcode=p.exitcode))


class ActorDefinition(object):
Expand Down Expand Up @@ -168,8 +176,8 @@ def discover(self):
tag.actors += (self,)
return self._discovery

def __call__(self, messaging=None, logger=None):
return ActorCallContext(definition=self, messaging=messaging, logger=logger)
def __call__(self, messaging=None, logger=None, within_pytest=False):
return ActorCallContext(definition=self, messaging=messaging, logger=logger, within_pytest=within_pytest)

@property
def dialogs(self):
Expand Down
2 changes: 1 addition & 1 deletion leapp/snactor/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def run(self):

:return: None
"""
self._actor(messaging=self._messaging).run()
self._actor(messaging=self._messaging, within_pytest=True).run()

def messages(self):
"""
Expand Down