Allow setting minimum polling interval for collect

Add `min_polling_interval` in `[default]` config.
This config option allow setting minimum polling interval,
default is 1 second.
Closes-Bug: #1656893
Depends-On: If863d2e190a4e1701ebf1fcb99a783075cda2556
Change-Id: I213afabeeb63f143ce359a3495a6d88b9b74ef4d
This commit is contained in:
ricolin 2017-01-16 23:20:30 +08:00 committed by Rico Lin
parent 8d038af700
commit d677da173b
2 changed files with 25 additions and 10 deletions

View File

@ -64,6 +64,9 @@ opts = [
help='Pass this option to make os-collect-config exit after' help='Pass this option to make os-collect-config exit after'
' one execution of command. This behavior is implied if no' ' one execution of command. This behavior is implied if no'
' command is specified.'), ' command is specified.'),
cfg.FloatOpt('min-polling-interval', default=1,
help='When running continuously, pause a minimum of this'
' many seconds between collecting data.'),
cfg.FloatOpt('polling-interval', short='i', default=30, cfg.FloatOpt('polling-interval', short='i', default=30,
help='When running continuously, pause a maximum of this' help='When running continuously, pause a maximum of this'
' many seconds between collecting data. If changes' ' many seconds between collecting data. If changes'
@ -85,7 +88,7 @@ opts = [
default=['deployments'], default=['deployments'],
help='Key(s) to explode into multiple collected outputs. ' help='Key(s) to explode into multiple collected outputs. '
'Parsed according to the expected Metadata created by ' 'Parsed according to the expected Metadata created by '
'OS::Heat::StructuredDeployment. Only Exploded if seen at ' 'OS::Heat::StructuredDeployment. Only exploded if seen at '
'the root of the Metadata.') 'the root of the Metadata.')
] ]
@ -253,8 +256,10 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
exitval = 0 exitval = 0
config_files = CONF.config_file config_files = CONF.config_file
config_hash = getfilehash(config_files) config_hash = getfilehash(config_files)
sleep_time = 1 exponential_sleep_time = CONF.min_polling_interval
while True: while True:
# shorter sleeps while changes are detected allows for faster
# software deployment dependency processing
store_and_run = bool(CONF.command and not CONF.print_only) store_and_run = bool(CONF.command and not CONF.print_only)
(changed_keys, content) = collect_all( (changed_keys, content) = collect_all(
cfg.CONF.collectors, cfg.CONF.collectors,
@ -262,9 +267,6 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
collector_kwargs_map=collector_kwargs_map) collector_kwargs_map=collector_kwargs_map)
if store_and_run: if store_and_run:
if changed_keys or CONF.force: if changed_keys or CONF.force:
# shorter sleeps while changes are detected allows for faster
# software deployment dependency processing
sleep_time = 1
# ignore HUP now since we will reexec after commit anyway # ignore HUP now since we will reexec after commit anyway
signal.signal(signal.SIGHUP, signal.SIG_IGN) signal.signal(signal.SIGHUP, signal.SIG_IGN)
try: try:
@ -285,12 +287,12 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
if CONF.one_time: if CONF.one_time:
break break
else: else:
logger.info("Sleeping %.2f seconds.", sleep_time) logger.info("Sleeping %.2f seconds.", exponential_sleep_time)
time.sleep(sleep_time) time.sleep(exponential_sleep_time)
sleep_time *= 2 exponential_sleep_time *= 2
if sleep_time > CONF.polling_interval: if exponential_sleep_time > CONF.polling_interval:
sleep_time = CONF.polling_interval exponential_sleep_time = CONF.polling_interval
else: else:
print(json.dumps(content, indent=1)) print(json.dumps(content, indent=1))
break break

View File

@ -331,6 +331,19 @@ class TestCollect(testtools.TestCase):
collect.__main__(['os-collect-config', 'heat_local', '--config-file', collect.__main__(['os-collect-config', 'heat_local', '--config-file',
'/dev/null', '-i', '10']) '/dev/null', '-i', '10'])
def test_main_min_polling_interval(self):
class ExpectedException(Exception):
pass
def fake_sleep(sleep_time):
if sleep_time == 20:
raise ExpectedException
self.useFixture(fixtures.MonkeyPatch('time.sleep', fake_sleep))
self.assertRaises(ExpectedException, collect.__main__,
['os-collect-config', 'heat_local', '-i', '10',
'--min-polling-interval', '20', '-c', 'true'])
class TestCollectAll(testtools.TestCase): class TestCollectAll(testtools.TestCase):