ansible: more docstrings and call_async()

This commit is contained in:
David Wilson 2018-04-09 03:20:48 +01:00
parent 6a4ce84c6b
commit 4a61527774
1 changed files with 14 additions and 11 deletions

View File

@ -56,7 +56,6 @@ import ansible_mitogen.services
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
NO_METHOD_MSG = 'Mitogen: no invocation method found for: ' NO_METHOD_MSG = 'Mitogen: no invocation method found for: '
CRASHED_MSG = 'Mitogen: internal error: '
NO_INTERPRETER_MSG = 'module (%s) is missing interpreter line' NO_INTERPRETER_MSG = 'module (%s) is missing interpreter line'
@ -148,6 +147,9 @@ class Planner(object):
raise NotImplementedError() raise NotImplementedError()
def get_should_fork(self, invocation): def get_should_fork(self, invocation):
"""
Asynchronous tasks must always be forked.
"""
return invocation.wrap_async return invocation.wrap_async
def plan(self, invocation, **kwargs): def plan(self, invocation, **kwargs):
@ -302,6 +304,10 @@ class NewStylePlanner(ScriptPlanner):
runner_name = 'NewStyleRunner' runner_name = 'NewStyleRunner'
def get_should_fork(self, invocation): def get_should_fork(self, invocation):
"""
In addition to asynchronous tasks, new-style modules should be forked
if mitogen_task_isolation=fork.
"""
return ( return (
super(NewStylePlanner, self).get_should_fork(invocation) or super(NewStylePlanner, self).get_should_fork(invocation) or
(invocation.task_vars.get('mitogen_task_isolation') == 'fork') (invocation.task_vars.get('mitogen_task_isolation') == 'fork')
@ -354,13 +360,10 @@ def _do_invoke(invocation):
else: else:
raise ansible.errors.AnsibleError(NO_METHOD_MSG + repr(invocation)) raise ansible.errors.AnsibleError(NO_METHOD_MSG + repr(invocation))
try: return invocation.connection.call_async(
kwargs = planner.plan(invocation) ansible_mitogen.target.run_module,
invocation.connection.call(ansible_mitogen.target.run_module, kwargs) planner.plan(invocation),
except mitogen.core.CallError as e: )
LOG.exception('invocation crashed: %r', invocation)
summary = str(e).splitlines()[0]
raise ansible.errors.AnsibleInternalError(CRASHED_MSG + summary)
def _invoke_async(invocation): def _invoke_async(invocation):
@ -377,18 +380,18 @@ def _invoke_async(invocation):
def _invoke_sync(invocation): def _invoke_sync(invocation):
recv = mitogen.core.Receiver(invocation.connection.router) result_recv = mitogen.core.Receiver(invocation.connection.router)
mitogen.service.call_async( mitogen.service.call_async(
context=invocation.connection.parent, context=invocation.connection.parent,
handle=ansible_mitogen.services.JobResultService.handle, handle=ansible_mitogen.services.JobResultService.handle,
method='listen', method='listen',
kwargs={ kwargs={
'job_id': invocation.job_id, 'job_id': invocation.job_id,
'sender': recv.to_sender(), 'sender': result_recv.to_sender(),
} }
) )
_do_invoke(invocation) _do_invoke(invocation)
return recv.get().unpickle() return result_recv.get().unpickle()
def invoke(invocation): def invoke(invocation):