Merge pull request #29285 from cachedout/lint_29224

Implemented batch wait timer
This commit is contained in:
Mike Place 2015-11-30 15:00:50 -07:00
commit 4de97a681d
2 changed files with 28 additions and 2 deletions

View File

@ -8,6 +8,7 @@ from __future__ import absolute_import, print_function
import math
import time
import copy
from datetime import datetime, timedelta
# Import salt libs
import salt.client
@ -80,6 +81,14 @@ class Batch(object):
print_cli('Invalid batch data sent: {0}\nData must be in the '
'form of %10, 10% or 3'.format(self.opts['batch']))
def __update_wait(self, wait):
now = datetime.now()
i = 0
while i < len(wait) and wait[i] <= now:
i += 1
if i:
del wait[:i]
def run(self):
'''
Execute the batch run
@ -95,6 +104,9 @@ class Batch(object):
active = []
ret = {}
iters = []
# wait the specified time before decide a job is actually done
bwait = self.opts.get('batch_wait', 0)
wait = []
# the minion tracker keeps track of responses and iterators
# - it removes finished iterators from iters[]
@ -107,12 +119,14 @@ class Batch(object):
# Iterate while we still have things to execute
while len(ret) < len(self.minions):
next_ = []
if len(to_run) <= bnum and not active:
if bwait and wait:
self.__update_wait(wait)
if len(to_run) <= bnum - len(wait) and not active:
# last bit of them, add them all to next iterator
while to_run:
next_.append(to_run.pop())
else:
for i in range(bnum - len(active)):
for i in range(bnum - len(active) - len(wait)):
if to_run:
minion_id = to_run.pop()
if isinstance(minion_id, dict):
@ -190,6 +204,8 @@ class Batch(object):
for minion, data in six.iteritems(parts):
if minion in active:
active.remove(minion)
if bwait:
wait.append(datetime.now() + timedelta(seconds=bwait))
if self.opts.get('raw'):
yield data
else:
@ -216,3 +232,5 @@ class Batch(object):
for minion in minion_tracker[queue]['minions']:
if minion in active:
active.remove(minion)
if bwait:
wait.append(datetime.now() + timedelta(seconds=bwait))

View File

@ -1830,6 +1830,14 @@ class SaltCMDOptionParser(six.with_metaclass(OptionParserMeta,
'of minions to batch at a time, or the percentage of '
'minions to have running')
)
self.add_option(
'--batch-wait',
default=0,
dest='batch_wait',
type=float,
help=('Wait the specified time in seconds after each job is done '
'before freeing the slot in the batch for the next one')
)
self.add_option(
'-a', '--auth', '--eauth', '--external-auth',
default='',