Browse Source

Merge "Allow setting minimum polling interval for collect"

Jenkins 2 years ago
parent
commit
34624661aa
2 changed files with 25 additions and 10 deletions
  1. 12
    10
      os_collect_config/collect.py
  2. 13
    0
      os_collect_config/tests/test_collect.py

+ 12
- 10
os_collect_config/collect.py View File

@@ -64,6 +64,9 @@ opts = [
64 64
                 help='Pass this option to make os-collect-config exit after'
65 65
                 ' one execution of command. This behavior is implied if no'
66 66
                 ' command is specified.'),
67
+    cfg.FloatOpt('min-polling-interval', default=1,
68
+                 help='When running continuously, pause a minimum of this'
69
+                      ' many seconds between collecting data.'),
67 70
     cfg.FloatOpt('polling-interval', short='i', default=30,
68 71
                  help='When running continuously, pause a maximum of this'
69 72
                       ' many seconds between collecting data. If changes'
@@ -85,7 +88,7 @@ opts = [
85 88
                     default=['deployments'],
86 89
                     help='Key(s) to explode into multiple collected outputs. '
87 90
                     'Parsed according to the expected Metadata created by '
88
-                    'OS::Heat::StructuredDeployment. Only Exploded if seen at '
91
+                    'OS::Heat::StructuredDeployment. Only exploded if seen at '
89 92
                     'the root of the Metadata.')
90 93
 ]
91 94
 
@@ -253,8 +256,10 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
253 256
     exitval = 0
254 257
     config_files = CONF.config_file
255 258
     config_hash = getfilehash(config_files)
256
-    sleep_time = 1
259
+    exponential_sleep_time = CONF.min_polling_interval
257 260
     while True:
261
+        # shorter sleeps while changes are detected allows for faster
262
+        # software deployment dependency processing
258 263
         store_and_run = bool(CONF.command and not CONF.print_only)
259 264
         (changed_keys, content) = collect_all(
260 265
             cfg.CONF.collectors,
@@ -262,9 +267,6 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
262 267
             collector_kwargs_map=collector_kwargs_map)
263 268
         if store_and_run:
264 269
             if changed_keys or CONF.force:
265
-                # shorter sleeps while changes are detected allows for faster
266
-                # software deployment dependency processing
267
-                sleep_time = 1
268 270
                 # ignore HUP now since we will reexec after commit anyway
269 271
                 signal.signal(signal.SIGHUP, signal.SIG_IGN)
270 272
                 try:
@@ -285,12 +287,12 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
285 287
             if CONF.one_time:
286 288
                 break
287 289
             else:
288
-                logger.info("Sleeping %.2f seconds.", sleep_time)
289
-                time.sleep(sleep_time)
290
+                logger.info("Sleeping %.2f seconds.", exponential_sleep_time)
291
+                time.sleep(exponential_sleep_time)
290 292
 
291
-            sleep_time *= 2
292
-            if sleep_time > CONF.polling_interval:
293
-                sleep_time = CONF.polling_interval
293
+            exponential_sleep_time *= 2
294
+            if exponential_sleep_time > CONF.polling_interval:
295
+                exponential_sleep_time = CONF.polling_interval
294 296
         else:
295 297
             print(json.dumps(content, indent=1))
296 298
             break

+ 13
- 0
os_collect_config/tests/test_collect.py View File

@@ -331,6 +331,19 @@ class TestCollect(testtools.TestCase):
331 331
         collect.__main__(['os-collect-config', 'heat_local', '--config-file',
332 332
                           '/dev/null', '-i', '10'])
333 333
 
334
+    def test_main_min_polling_interval(self):
335
+        class ExpectedException(Exception):
336
+            pass
337
+
338
+        def fake_sleep(sleep_time):
339
+            if sleep_time == 20:
340
+                raise ExpectedException
341
+
342
+        self.useFixture(fixtures.MonkeyPatch('time.sleep', fake_sleep))
343
+        self.assertRaises(ExpectedException, collect.__main__,
344
+                          ['os-collect-config', 'heat_local', '-i', '10',
345
+                           '--min-polling-interval', '20', '-c', 'true'])
346
+
334 347
 
335 348
 class TestCollectAll(testtools.TestCase):
336 349
 

Loading…
Cancel
Save