]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/prometheus: use threading.Event instead of sleep
authorBoris Ranto <branto@redhat.com>
Wed, 25 Nov 2020 19:49:48 +0000 (20:49 +0100)
committerBoris Ranto <branto@redhat.com>
Wed, 25 Nov 2020 19:49:50 +0000 (20:49 +0100)
This allows us to avoid waiting for the sleep to finish when waiting for
the thread to finish.

Signed-off-by: Boris Ranto <branto@redhat.com>
src/pybind/mgr/prometheus/module.py

index c5ec065853b257ba8d0bfecab1210cd4e77179d4..359578e5415b3e55ab1357dedb396c0196bdb667 100644 (file)
@@ -188,6 +188,7 @@ class MetricCollectionThread(threading.Thread):
         # type: (Module) -> None
         self.mod = module
         self.active = True
+        self.event = threading.Event()
         super(MetricCollectionThread, self).__init__(target=self.collect)
 
     def collect(self):
@@ -202,7 +203,7 @@ class MetricCollectionThread(threading.Thread):
                 except Exception as e:
                     # Log any issues encountered during the data collection and continue
                     self.mod.log.exception("failed to collect metrics:")
-                    time.sleep(self.mod.scrape_interval)
+                    self.event.wait(self.mod.scrape_interval)
                     continue
 
                 duration = time.time() - start_time
@@ -227,13 +228,14 @@ class MetricCollectionThread(threading.Thread):
                     self.mod.collect_cache = data
                     self.mod.collect_time = duration
 
-                time.sleep(sleep_time)
+                self.event.wait(sleep_time)
             else:
                 self.mod.log.error('No MON connection')
-                time.sleep(self.mod.scrape_interval)
+                self.event.wait(self.mod.scrape_interval)
 
     def stop(self):
         self.active = False
+        self.event.set()
 
 class Module(MgrModule):
     COMMANDS = [