rexor12 / holobot

A simple - but hopefully easily extensible - bot with Discord integration.
MIT License
4 stars 3 forks source link

[Core] Configuration caching and reloading #49

Open rexor12 opened 3 years ago

rexor12 commented 3 years ago

Instead of always going to the system environment variables or file configurations (I/O), it would be nice to have a lazy in-memory cache for the configurations. Due to them being lazy, they would be loaded on the first access - so, when they are actually needed.

The configuration provider could have a method for subscribing to configuration changes so that the various services could subscribe to them and re-initialize their internals when some configurations change.

For example, in the configuration provider: async def subscribe_changes(self, callback: Callable[[], None])

and then the service would invoke it as such: awaitconfiguration_provider.subscribe_changes(self.__on_configs_changed)

Since detection of configuration changes may or may not be trivial (and we don't want to implement it, for now), such a notification would need to be manually triggered. One example is a new development command: h!dev reload_configs

Note: Today, some services cache their own configurations to avoid going to the disk multiple times. With this ticket, that would need to be changed, too. One such example: https://github.com/rexor12/holobot/blob/f3fc1ce399c94d8712120a26a8632dd00da45101/holobot/extensions/weather/weather_client.py#L27

rexor12 commented 3 years ago

As it is possible to do now, it might be a better idea to implement a reactive listener for services that care about configuration changes. Other than that, after this change configurations shouldn't be retrieved once in the __init__ methods and never again; instead, they should be retrieved each time they are needed - as the configuration cache will take care of storing them anyway.

rexor12 commented 3 years ago

Do this after #74.