|
Common System 0.2.0
Common interfaces and patterns for system integration
|
Monitors configuration files for changes and supports hot-reload. More...
#include <config_watcher.h>

Public Types | |
| using | change_callback |
| Callback type for configuration changes. | |
| using | error_callback = std::function<void(const std::string& error_message)> |
| Callback type for reload errors. | |
Public Member Functions | |
| config_watcher (const std::string &config_path, size_t max_history=10) | |
| Construct a config_watcher for the specified file. | |
| ~config_watcher () | |
| Destructor. Automatically stops watching if running. | |
| config_watcher (const config_watcher &)=delete | |
| config_watcher & | operator= (const config_watcher &)=delete |
| config_watcher (config_watcher &&)=delete | |
| config_watcher & | operator= (config_watcher &&)=delete |
| VoidResult | start () |
| Start watching the configuration file for changes. | |
| void | stop () |
| Stop watching the configuration file. | |
| bool | is_running () const |
| Check if the watcher is currently running. | |
| void | on_change (change_callback callback) |
| Register a callback for configuration changes. | |
| void | on_error (error_callback callback) |
| Register a callback for reload errors. | |
| VoidResult | reload () |
| Manually trigger a configuration reload. | |
| const unified_config & | current () const |
| Get the current configuration. | |
| uint64_t | version () const |
| Get the current configuration version. | |
| std::vector< config_snapshot > | history (size_t count=0) const |
| Get configuration history snapshots. | |
| VoidResult | rollback (uint64_t target_version) |
| Rollback to a previous configuration version. | |
| const std::string & | config_path () const |
| Get the path to the configuration file being watched. | |
| std::vector< config_change_event > | recent_events (size_t count=10) const |
| Get recent change events. | |
Private Member Functions | |
| VoidResult | init_platform_watcher () |
| Initialize platform-specific file watching. | |
| void | signal_watcher_shutdown () |
| Signal the watch thread to wake up and exit. | |
| void | cleanup_platform_watcher () |
| Cleanup platform-specific resources (call after thread join). | |
| void | watch_loop () |
| Main watch loop - dispatches to platform-specific implementation. | |
| VoidResult | do_reload () |
| Perform the actual configuration reload. | |
| void | add_to_history (const unified_config &config) |
| Add a configuration to history. | |
| void | add_event (const config_change_event &event) |
| Add a change event to the event log. | |
| void | notify_change (const unified_config &old_cfg, const unified_config &new_cfg) |
| Notify all registered change callbacks. | |
| void | notify_error (const std::string &message) |
| Notify all registered error callbacks. | |
Static Private Member Functions | |
| static std::vector< std::string > | get_changed_fields (const unified_config &old_cfg, const unified_config &new_cfg) |
| Compare two configurations and return changed field paths. | |
Private Attributes | |
| std::string | config_path_ |
| unified_config | current_config_ |
| std::shared_mutex | config_mutex_ |
| std::atomic< uint64_t > | version_ |
| size_t | max_history_ |
| std::mutex | history_mutex_ |
| std::deque< config_snapshot > | history_ |
| std::mutex | events_mutex_ |
| std::deque< config_change_event > | events_ |
| std::mutex | callbacks_mutex_ |
| std::vector< change_callback > | change_callbacks_ |
| std::vector< error_callback > | error_callbacks_ |
| std::atomic< bool > | running_ |
| std::thread | watch_thread_ |
Monitors configuration files for changes and supports hot-reload.
The config_watcher provides automatic configuration reloading when the configuration file is modified. It supports:
Usage Example:
Definition at line 143 of file config_watcher.h.
Callback type for configuration changes.
Definition at line 146 of file config_watcher.h.
| using kcenon::common::config::config_watcher::error_callback = std::function<void(const std::string& error_message)> |
Callback type for reload errors.
Definition at line 150 of file config_watcher.h.
|
inlineexplicit |
Construct a config_watcher for the specified file.
| config_path | Path to the YAML configuration file |
| max_history | Maximum number of configuration snapshots to keep (default: 10) |
Definition at line 157 of file config_watcher.h.
References add_to_history(), config_path_, current_config_, kcenon::common::config::config_loader::defaults(), and kcenon::common::config::config_loader::load().

|
inline |
Destructor. Automatically stops watching if running.
Definition at line 188 of file config_watcher.h.
References stop().

|
delete |
|
delete |
|
inlineprivate |
Add a change event to the event log.
Definition at line 974 of file config_watcher.h.
|
inlineprivate |
Add a configuration to history.
Definition at line 955 of file config_watcher.h.
References kcenon::common::config::config_snapshot::config, kcenon::common::config::config_snapshot::timestamp, and kcenon::common::config::config_snapshot::version.
Referenced by config_watcher().

|
inlineprivate |
Cleanup platform-specific resources (call after thread join).
Definition at line 424 of file config_watcher.h.
Referenced by stop().

|
inline |
Get the path to the configuration file being watched.
Definition at line 357 of file config_watcher.h.
References config_path_.
|
inline |
Get the current configuration.
Definition at line 289 of file config_watcher.h.
References config_mutex_, and current_config_.
|
inlineprivate |
Perform the actual configuration reload.
Definition at line 815 of file config_watcher.h.
References kcenon::common::config::is_hot_reloadable(), kcenon::common::config::config_loader::load(), kcenon::common::make_error(), kcenon::common::Result< T >::ok(), kcenon::common::config::config_loader::validate(), and kcenon::common::config::watcher_error_codes::validation_failed.
Referenced by reload().


|
inlinestaticprivate |
Compare two configurations and return changed field paths.
Definition at line 882 of file config_watcher.h.
References kcenon::common::config::logger_config::async, kcenon::common::config::database_config::backend, kcenon::common::config::logger_config::buffer_size, kcenon::common::config::network_config::buffer_size, kcenon::common::config::network_config::compression, kcenon::common::config::database_config::connection_string, kcenon::common::config::unified_config::database, kcenon::common::config::monitoring_config::enabled, kcenon::common::config::tls_config::enabled, kcenon::common::config::tracing_config::enabled, kcenon::common::config::logger_config::file_path, kcenon::common::config::logger_config::level, kcenon::common::config::unified_config::logger, kcenon::common::config::thread_config::max_queue_size, kcenon::common::config::monitoring_config::metrics_interval, kcenon::common::config::unified_config::monitoring, kcenon::common::config::unified_config::network, kcenon::common::config::thread_config::pool_size, kcenon::common::config::thread_config::queue_type, kcenon::common::config::tracing_config::sampling_rate, kcenon::common::config::unified_config::thread, kcenon::common::config::network_config::tls, kcenon::common::config::monitoring_config::tracing, and kcenon::common::config::logger_config::writers.
|
inline |
Get configuration history snapshots.
| count | Maximum number of snapshots to return (default: all) |
Definition at line 307 of file config_watcher.h.
References history_, and history_mutex_.
|
inlineprivate |
Initialize platform-specific file watching.
Definition at line 386 of file config_watcher.h.
References kcenon::common::make_error(), and kcenon::common::config::watcher_error_codes::platform_not_supported.
Referenced by start().


|
inline |
Check if the watcher is currently running.
Definition at line 248 of file config_watcher.h.
References running_.
Referenced by main().

|
inlineprivate |
Notify all registered change callbacks.
Definition at line 987 of file config_watcher.h.
Referenced by rollback().

|
inlineprivate |
Notify all registered error callbacks.
Definition at line 1001 of file config_watcher.h.
|
inline |
Register a callback for configuration changes.
The callback will be invoked whenever the configuration is successfully reloaded. Multiple callbacks can be registered.
| callback | Function to call on configuration change |
Definition at line 260 of file config_watcher.h.
References callbacks_mutex_, and change_callbacks_.
Referenced by main().

|
inline |
Register a callback for reload errors.
The callback will be invoked when a reload attempt fails.
| callback | Function to call on reload error |
Definition at line 272 of file config_watcher.h.
References callbacks_mutex_, and error_callbacks_.
Referenced by main().

|
delete |
|
delete |
|
inline |
Get recent change events.
| count | Maximum number of events to return |
Definition at line 366 of file config_watcher.h.
References events_, and events_mutex_.
|
inline |
Manually trigger a configuration reload.
Definition at line 281 of file config_watcher.h.
References do_reload().
Referenced by main().


|
inline |
Rollback to a previous configuration version.
| target_version | Version number to rollback to |
Definition at line 328 of file config_watcher.h.
References config_mutex_, current_config_, history_, history_mutex_, kcenon::common::make_error(), notify_change(), kcenon::common::Result< T >::ok(), kcenon::common::config::watcher_error_codes::rollback_failed, and version_.

|
inlineprivate |
Signal the watch thread to wake up and exit.
Definition at line 405 of file config_watcher.h.
Referenced by stop().

|
inline |
Start watching the configuration file for changes.
Definition at line 202 of file config_watcher.h.
References kcenon::common::config::watcher_error_codes::already_running, init_platform_watcher(), kcenon::common::make_error(), kcenon::common::Result< T >::ok(), running_, watch_loop(), and watch_thread_.
Referenced by main().


|
inline |
Stop watching the configuration file.
Definition at line 225 of file config_watcher.h.
References cleanup_platform_watcher(), running_, signal_watcher_shutdown(), and watch_thread_.
Referenced by main(), and ~config_watcher().


|
inline |
Get the current configuration version.
Definition at line 298 of file config_watcher.h.
References version_.
|
inlineprivate |
Main watch loop - dispatches to platform-specific implementation.
Definition at line 802 of file config_watcher.h.
Referenced by start().

|
private |
Definition at line 1030 of file config_watcher.h.
Referenced by on_change(), and on_error().
|
private |
Definition at line 1031 of file config_watcher.h.
Referenced by on_change().
|
mutableprivate |
Definition at line 1015 of file config_watcher.h.
Referenced by current(), and rollback().
|
private |
Definition at line 1013 of file config_watcher.h.
Referenced by config_path(), and config_watcher().
|
private |
Definition at line 1014 of file config_watcher.h.
Referenced by config_watcher(), current(), and rollback().
|
private |
Definition at line 1032 of file config_watcher.h.
Referenced by on_error().
|
private |
Definition at line 1027 of file config_watcher.h.
Referenced by recent_events().
|
mutableprivate |
Definition at line 1026 of file config_watcher.h.
Referenced by recent_events().
|
private |
Definition at line 1023 of file config_watcher.h.
Referenced by history(), and rollback().
|
mutableprivate |
Definition at line 1022 of file config_watcher.h.
Referenced by history(), and rollback().
|
private |
Definition at line 1021 of file config_watcher.h.
|
private |
Definition at line 1035 of file config_watcher.h.
Referenced by is_running(), start(), and stop().
|
private |
Definition at line 1018 of file config_watcher.h.
Referenced by rollback(), and version().
|
private |
Definition at line 1036 of file config_watcher.h.