Crates.io | config_watcher |
lib.rs | config_watcher |
version | |
source | src |
created_at | 2025-02-08 15:04:37.441506+00 |
updated_at | 2025-03-23 10:27:41.450288+00 |
description | Config Watcher is a Rust library for tracking configuration items from files, Kubernetes ConfigMaps, and MQTT topics, providing real-time updates and structured data parsing. |
homepage | |
repository | https://github.com/schaze/config_watcher |
max_upload_size | |
id | 1548094 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Config Watcher is a Rust library that provides a unified way to read and track configuration items from various sources, including files, Kubernetes ConfigMaps, and MQTT topics. It allows applications to receive real-time updates on configuration changes, ensuring dynamic and reactive behavior. The library supports structured data formats such as YAML and JSON, regardless of the backend source.
Install via cargo:
cargo add config_watcher
Config Watcher reads structured configuration data from supported backends and emits events when configuration items change. Users can define:
ConfigItemWatcher
.Each backend has its own parameters that must be provided when initializing a watcher:
Monitors local files for changes and updates the configuration dynamically.
use config_watcher::backend::run_config_file_watcher;
use std::time::Duration;
let watcher = run_config_file_watcher("/config", "*.yaml", Duration::from_secs(1));
Parameters:
watch_path: impl AsRef<Path>
– The directory or file path to watch.file_pattern: impl Into<String>
– The glob pattern to match files (e.g., *.yaml
).debounce: Duration
– The debounce interval for reducing redundant events.Tracks Kubernetes ConfigMaps and provides live updates when the configuration changes.
use config_watcher::backend::run_configmap_watcher;
let watcher = run_configmap_watcher("config-map-name".to_string(), "namespace".to_string());
Parameters:
configmap_name: String
– Name of the ConfigMap.namespace: String
– Kubernetes namespace containing the ConfigMap.Subscribes to an MQTT topic and listens for configuration updates. It uses rumqttc::MqttOptions
to configure the MQTT connection.
use config_watcher::backend::run_mqtt_watcher;
use rumqttc::MqttOptions;
let mut mqtt_options = MqttOptions::new("config-watcher-client", "mqtt-broker-host", 1883);
mqtt_options.set_credentials("user", "password");
let watcher = run_mqtt_watcher(mqtt_options, "config/topic", 1024).expect("Failed to start MQTT watcher");
Parameters:
mqttoptions: MqttOptions
– MQTT connection options.config_topic: &str
– MQTT topic to subscribe to.channel_size: usize
– Size of the message channel.Config Watcher uses content-based hashing to track configuration changes. Because of this, it does not provide traditional "update" events. Instead, when an item changes, it is reported as a removal followed by an addition with the updated content. This ensures that even minor changes are properly detected and processed.
Config Watcher provides real-time updates for configuration items by leveraging ConfigItemWatcher
, which manages backends and ensures a consistent interface for receiving updates.
ConfigItemEvent
Variantsu64
is an internal identifier used to track the document, and the String
represents the document path (filename in the filesystem, attribute in a ConfigMap, or topic in MQTT). This allows applications to map document IDs to paths and display relevant information.u64
identifier allows the system to properly correlate the deletion with previous content.ConfigItemHash
is a hash-based identifier ensuring unique tracking, and T
is the deserialized configuration object.run_config_item_watcher
The run_config_item_watcher
function is responsible for managing configuration watchers. To use it, you need to:
When executed, the function returns a handle to manage the watcher and a receiver that emits events when configurations change. The application can then react to these events dynamically.
// Initialize the configuration watcher
let (watcher_handle, mut receiver) = run_config_item_watcher(|| {
// Choose the backend: in this case, watching a directory for YAML files
backend::run_config_file_watcher("/config", "*.yaml", Duration::from_secs(1))
}, &YamlTokenizer, deserialize_my_config)?;
watcher_handle.start().await.unwrap();
// Continuously listen for configuration updates
while let Some(event) = receiver.recv().await {
match event {
// Handle newly added documents
ConfigItemEvent::NewDocument(id, path) => {
// `id` is an internal identifier for tracking the document
// `path` represents the document path (filename, attribute in a ConfigMap, or topic in MQTT)
// This allows the application to map file IDs to filenames and display relevant paths to users
println!("New document detected: {}", id);
// Process the new document
},
// Handle removed documents
ConfigItemEvent::RemoveDocument(id) => {
println!("Document removed: {}", id);
// Perform any necessary cleanup
},
// Handle newly added configuration items
ConfigItemEvent::New(hash, item) => {
println!("New item detected: {:?}", hash);
// Process the new configuration item
},
// Handle removed configuration items
ConfigItemEvent::Removed(hash) => {
println!("Configuration item removed: {:?}", hash);
// Perform any necessary cleanup
},
}
}
Contributions are welcome! Please submit a pull request or open an issue for discussion.
This project is licensed under the MIT License. See LICENSE for details.