Skip to content

Commit

Permalink
Merge pull request #24093 from msteed/issue-23464
Browse files Browse the repository at this point in the history
Make LocalClient.cmd_iter_no_block() not block
  • Loading branch information
Nicole Thomas committed May 25, 2015
2 parents 5bffd30 + fd35903 commit 39a8f30
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
7 changes: 1 addition & 6 deletions salt/cli/batch.py
Expand Up @@ -42,18 +42,13 @@ def __gather_minions(self):
else:
args.append(self.opts.get('expr_form', 'glob'))

ping_gen = self.local.cmd_iter_no_block(*args, **self.eauth)
wait_until = time.time() + self.opts['timeout']
ping_gen = self.local.cmd_iter(*args, **self.eauth)

fret = set()
for ret in ping_gen:
m = next(ret.iterkeys())
if m is not None:
fret.add(m)
if time.time() > wait_until:
break
if m is None:
time.sleep(0.1)
return (list(fret), ping_gen)

def get_bnum(self):
Expand Down
41 changes: 24 additions & 17 deletions salt/client/__init__.py
Expand Up @@ -385,8 +385,8 @@ def cmd_batch(
.. code-block:: python
>>> returns = local.cmd_batch('*', 'state.highstate', bat='10%')
>>> for return in returns:
... print return
>>> for ret in returns:
... print(ret)
{'jerry': {...}}
{'dave': {...}}
{'stewart': {...}}
Expand Down Expand Up @@ -611,13 +611,13 @@ def cmd_iter(
The function signature is the same as :py:meth:`cmd` with the
following exceptions.
:return: A generator
:return: A generator yielding the individual minion returns
.. code-block:: python
>>> ret = local.cmd_iter('*', 'test.ping')
>>> for i in ret:
... print i
... print(i)
{'jerry': {'ret': True}}
{'dave': {'ret': True}}
{'stewart': {'ret': True}}
Expand All @@ -637,9 +637,9 @@ def cmd_iter(
else:
for fn_ret in self.get_iter_returns(pub_data['jid'],
pub_data['minions'],
self._get_timeout(timeout),
tgt,
expr_form,
timeout=self._get_timeout(timeout),
tgt=tgt,
tgt_type=expr_form,
**kwargs):
if not fn_ret:
continue
Expand All @@ -656,19 +656,21 @@ def cmd_iter_no_block(
kwarg=None,
**kwargs):
'''
Blocks while waiting for individual minions to return.
Yields the individual minion returns as they come in, or None
when no returns are available.
The function signature is the same as :py:meth:`cmd` with the
following exceptions.
:returns: None until the next minion returns. This allows for actions
to be injected in between minion returns.
:returns: A generator yielding the individual minion returns, or None
when no returns are available. This allows for actions to be
injected in between minion returns.
.. code-block:: python
>>> ret = local.cmd_iter('*', 'test.ping')
>>> ret = local.cmd_iter_no_block('*', 'test.ping')
>>> for i in ret:
... print i
... print(i)
None
{'jerry': {'ret': True}}
{'dave': {'ret': True}}
Expand All @@ -690,9 +692,10 @@ def cmd_iter_no_block(
else:
for fn_ret in self.get_iter_returns(pub_data['jid'],
pub_data['minions'],
timeout,
tgt,
expr_form,
timeout=timeout,
tgt=tgt,
tgt_type=expr_form,
block=False,
**kwargs):
yield fn_ret

Expand Down Expand Up @@ -833,6 +836,7 @@ def get_iter_returns(
tgt='*',
tgt_type='glob',
expect_minions=False,
block=True,
**kwargs):
'''
Watch the event system and return job data as it comes in
Expand Down Expand Up @@ -995,7 +999,10 @@ def get_iter_returns(
break

# don't spin
time.sleep(0.01)
if block:
time.sleep(0.01)
else:
yield
if expect_minions:
for minion in list((minions - found)):
yield {minion: {'failed': True}}
Expand Down Expand Up @@ -1238,7 +1245,7 @@ def get_cli_event_returns(
connected_minions = salt.utils.minions.CkMinions(self.opts).connected_ids()
if connected_minions and id_ not in connected_minions:
yield {id_: {'out': 'no_return',
'ret': 'Minion did not return. [Not connected]'}}
'ret': 'Minion did not return. [Not connected]'}}
else:
yield({
id_: {
Expand Down

0 comments on commit 39a8f30

Please sign in to comment.