]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/hello: boilerplate for handling config options
authorSage Weil <sage@redhat.com>
Mon, 5 Aug 2019 19:46:40 +0000 (14:46 -0500)
committerSage Weil <sage@redhat.com>
Wed, 7 Aug 2019 14:27:19 +0000 (09:27 -0500)
Signed-off-by: Sage Weil <sage@redhat.com>
src/pybind/mgr/hello/module.py

index 323890b0a4ddfff5a64800772eddb7d41c0fb165..50eaa2e713326d568c0f16770ab6e0f42f807aff 100644 (file)
@@ -51,6 +51,11 @@ class Hello(MgrModule):
         },
     ]
 
+    # These are "native" Ceph options that this module cares about.
+    NATIVE_OPTIONS = [
+        'mgr_tick_period',
+    ]
+
     def __init__(self, *args, **kwargs):
         super(Hello, self).__init__(*args, **kwargs)
 
@@ -58,6 +63,30 @@ class Hello(MgrModule):
         self.run = True
         self.event = Event()
 
+        # ensure config options members are initialized; see config_notify()
+        self.config_notify()
+
+
+    def config_notify(self):
+        """
+        This method is called whenever one of our config options is changed.
+        """
+        # This is some boilerplate that stores MODULE_OPTIONS in a class
+        # member, so that, for instance, the 'emphatic' option is always
+        # available as 'self.emphatic'.
+        for opt in self.MODULE_OPTIONS:
+            setattr(self,
+                    opt['name'],
+                    self.get_module_option(opt['name']) or opt['default'])
+            self.log.debug(' mgr option %s = %s',
+                           opt['name'], getattr(self, opt['name']))
+        # Do the same for the native options.
+        for opt in self.NATIVE_OPTIONS:
+            setattr(self,
+                    opt,
+                    self.get_ceph_option(opt))
+            self.log.debug(' native option %s = %s', opt, getattr(self, opt))
+
     def handle_command(self, inbuf, cmd):
         self.log.info("hello_world_info")
         self.log.debug("hello_world_debug")
@@ -83,7 +112,12 @@ class Hello(MgrModule):
         """
         self.log.info("Starting")
         while self.run:
-            sleep_interval = 5
+            # Do some useful background work here.
+
+            # Use mgr_tick_period (default: 2) here just to illustrate
+            # consuming native ceph options.  Any real background work
+            # would presumably have some more appropriate frequency.
+            sleep_interval = int(self.mgr_tick_period)
             self.log.debug('Sleeping for %d seconds', sleep_interval)
             ret = self.event.wait(sleep_interval)
             self.event.clear()