From: Sage Weil Date: Mon, 5 Aug 2019 19:46:40 +0000 (-0500) Subject: mgr/hello: boilerplate for handling config options X-Git-Tag: v15.1.0~1935^2~2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=9318fb0d66e4095bd8d28b276fe1acc366a8eec9;p=ceph-ci.git mgr/hello: boilerplate for handling config options Signed-off-by: Sage Weil --- diff --git a/src/pybind/mgr/hello/module.py b/src/pybind/mgr/hello/module.py index 323890b0a4d..50eaa2e7133 100644 --- a/src/pybind/mgr/hello/module.py +++ b/src/pybind/mgr/hello/module.py @@ -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()